Skip to content

Commit

Permalink
Fix detector channel
Browse files Browse the repository at this point in the history
  • Loading branch information
MoamenEltouny authored Mar 23, 2024
1 parent 50a9ef1 commit 63457a3
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/Classes/AgentDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DeviceDetector\Parser\Client\Browser as ClientBrowser;
use DeviceDetector\Parser\Device\AbstractDeviceParser;
use DeviceDetector\Parser\OperatingSystem;
use Illuminate\Http\Request;
use Pharaonic\Laravel\Agents\Models\{
Agent,
Bot,
Expand Down Expand Up @@ -55,26 +56,36 @@ function __construct($user_agent = null, string $ip = null, string $accept_lang
$this->agent = $user_agent ?? $request->server('HTTP_USER_AGENT');
$this->ip = $ip ?? $request->ip();
$this->langs($accept_langauge ?? $request->server('HTTP_ACCEPT_LANGUAGE') ?? null);

if ($request->expectsJson()) {
$agent = Agent::findByAgent($this->agent);
$agent = $this->load($agent);
} else {
if ($request->session()->has('agent')) {
$agent = $request->session()->get('agent');
} else {
$agent = Agent::findByAgent($this->agent);
$agent = $this->load($agent);
$request->session()->put('agent', $agent);
}
}
$agent = $this->getAgent($request);

foreach ($agent as $name => $value)
$this->$name = $value;

return $this;
}

/**
* Get Agent Info.
*
* @param Request $request
* @return array
*/
private function getAgent(Request $request)
{
// Get Agent from API or Session if not started
if ($request->expectsJson() || !$request->session()->isStarted()) {
return $this->load(Agent::findByAgent($this->agent));
}

// Get Agent from Session
if (!($agent = $request->session()->get('agent'))) {
$agent = $this->load(Agent::findByAgent($this->agent));
$request->session()->put('agent', $agent);
}

return $agent;
}

/**
* Get Language information
*
Expand Down

0 comments on commit 63457a3

Please sign in to comment.