Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new provider SinergiBrowserDetector #20

Merged
merged 1 commit into from
Nov 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
"piwik/device-detector": "^3.4",
"doctrine/cache": "^1.5",

"sinergi/browser-detector": "^5.0",

"ua-parser/uap-php": "^3.4",

"whichbrowser/parser": ">=1.0.1044,<1.1",
Expand Down
204 changes: 204 additions & 0 deletions src/Provider/SinergiBrowserDetector.php
Original file line number Diff line number Diff line change
@@ -1 +1,205 @@
<?php
namespace UserAgentParser\Provider;

use Sinergi\BrowserDetector;
use UserAgentParser\Exception;
use UserAgentParser\Model;

class SinergiBrowserDetector extends AbstractProvider
{
/**
* Used for unitTests mocking
*
* @var BrowserDetector\Browser
*/
private $browserParser;

/**
* Used for unitTests mocking
*
* @var BrowserDetector\Os
*/
private $osParser;

/**
* Used for unitTests mocking
*
* @var BrowserDetector\Device
*/
private $deviceParser;

public function getName()
{
return 'SinergiBrowserDetector';
}

public function getComposerPackageName()
{
return 'sinergi/browser-detector';
}

/**
*
* @param string $userAgent
* @return BrowserDetector\Browser
*/
private function getBrowserParser($userAgent)
{
if ($this->browserParser !== null) {
return $this->browserParser;
}

return new BrowserDetector\Browser($userAgent);
}

/**
*
* @param string $userAgent
* @return BrowserDetector\Os
*/
private function getOperatingSystemParser($userAgent)
{
if ($this->osParser !== null) {
return $this->osParser;
}

return new BrowserDetector\Os($userAgent);
}

/**
*
* @param string $userAgent
* @return BrowserDetector\Device
*/
private function getDeviceParser($userAgent)
{
if ($this->deviceParser !== null) {
return $this->deviceParser;
}

return new BrowserDetector\Device($userAgent);
}

/**
*
* @param BrowserDetector\Browser $browserRaw
* @param BrowserDetector\Os $osRaw
* @param BrowserDetector\Device $deviceRaw
*
* @return boolean
*/
private function hasResult(BrowserDetector\Browser $browserRaw, BrowserDetector\Os $osRaw, BrowserDetector\Device $deviceRaw)
{
if ($this->isRealResult($browserRaw->getName())) {
return true;
}

if ($this->isRealResult($osRaw->getName())) {
return true;
}

if ($this->isRealResult($deviceRaw->getName())) {
return true;
}

if ($browserRaw->isRobot() === true) {
return true;
}

return false;
}

/**
*
* @param mixed $value
*
* @return bool
*/
private function isRealResult($value)
{
if ($value === '' || $value === null) {
return false;
}

if ($value === BrowserDetector\Browser::UNKNOWN) {
return false;
}

return true;
}

public function parse($userAgent, array $headers = [])
{
$browserRaw = $this->getBrowserParser($userAgent);
$osRaw = $this->getOperatingSystemParser($userAgent);
$deviceRaw = $this->getDeviceParser($userAgent);

/*
* No result found?
*/
if ($this->hasResult($browserRaw, $osRaw, $deviceRaw) !== true) {
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
}

/*
* Hydrate the model
*/
$result = new Model\UserAgent();
$result->setProviderResultRaw([
'browser' => $browserRaw,
'operatingSystem' => $osRaw,
'device' => $deviceRaw,
]);

/*
* Bot detection
*/
if ($browserRaw->isRobot() === true) {
$bot = $result->getBot();
$bot->setIsBot(true);

return $result;
}

/*
* Browser
*/
$browser = $result->getBrowser();

if ($this->isRealResult($browserRaw->getName()) === true) {
$browser->setName($browserRaw->getName());
}

if ($this->isRealResult($browserRaw->getVersion()) === true) {
$browser->getVersion()->setComplete($browserRaw->getVersion());
}

/*
* operatingSystem
*/
$operatingSystem = $result->getOperatingSystem();

if ($this->isRealResult($osRaw->getName()) === true) {
$operatingSystem->setName($osRaw->getName());
}

if ($this->isRealResult($osRaw->getVersion()) === true) {
$operatingSystem->getVersion()->setComplete($osRaw->getVersion());
}

/*
* device
*/
$device = $result->getDevice();

if ($this->isRealResult($deviceRaw->getName()) === true) {
$device->setModel($deviceRaw->getName());
}

if ($osRaw->isMobile() === true) {
$device->setIsMobile(true);
}

return $result;
}
}
2 changes: 1 addition & 1 deletion tests/unit/Provider/PiwikDeviceDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testParseBot()
}

/**
* Browser small
* Browser only
*/
public function testParseBrowser()
{
Expand Down
Loading