Skip to content

Commit

Permalink
feat: Add getter and adder for UAs to UserAgentCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
secondtruth committed May 17, 2024
1 parent 1b224e5 commit 1e16212
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions lib/Check/UserAgentCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,16 @@ class UserAgentCheck extends AbstractCheck
*
* @param UserAgentInterface[] $userAgents The UserAgent objects to use
*
* @throws \InvalidArgumentException If an unsupported user agent type is given.
* @throws \InvalidArgumentException If an unsupported type is given.
*/
public function __construct(array $userAgents = [])
{
foreach ($userAgents as $userAgent) {
if ($userAgent instanceof BrowserInterface) {
$this->browsers[] = $userAgent;
} elseif ($userAgent instanceof BotInterface) {
$this->bots[] = $userAgent;
} else {
throw new \InvalidArgumentException('Unsupported user agent type');
if (!$userAgent instanceof UserAgentInterface) {
throw new \InvalidArgumentException('Unsupported type');
}

$this->addUserAgent($userAgent);
}
}

Expand All @@ -58,7 +56,7 @@ public function __construct(array $userAgents = [])
public function checkVisitor(Visitor $visitor)
{
/** @var UserAgentInterface[] $userAgents */
$userAgents = array_merge($this->bots, $this->browsers);
$userAgents = $this->getUserAgents();

foreach ($userAgents as $userAgent) {
if ($userAgent->is($visitor->getUserAgent())) {
Expand All @@ -69,6 +67,24 @@ public function checkVisitor(Visitor $visitor)
return CheckInterface::RESULT_OKAY;
}

/**
* Adds the given user agent.
*
* @param UserAgentInterface $userAgent The UserAgent object to add
*
* @throws \InvalidArgumentException If an unsupported user agent type is given.
*/
public function addUserAgent(UserAgentInterface $userAgent)
{
if ($userAgent instanceof BrowserInterface) {
$this->browsers[] = $userAgent;
} elseif ($userAgent instanceof BotInterface) {
$this->bots[] = $userAgent;
} else {
throw new \InvalidArgumentException('Unsupported user agent type');
}
}

/**
* Adds the given browser.
*
Expand All @@ -90,7 +106,17 @@ public function addBot(BotInterface $userAgent)
}

/**
* Gets list of browsers.
* Gets the list of user agents.
*
* @return UserAgentInterface[]
*/
public function getUserAgents()
{
return array_merge($this->bots, $this->browsers);
}

/**
* Gets the list of browsers.
*
* @return BrowserInterface[]
*/
Expand All @@ -100,12 +126,10 @@ public function getBrowsers()
}

/**
* Gets list of bots.
* Gets the list of bots.
*
* @return BotInterface[]
*/
public function getBots()
{
return $this->bots;
}
}

0 comments on commit 1e16212

Please sign in to comment.