GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#60)
by
unknown
07:31
created

DonatjUAParser::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2.0625
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UserAgentParser\Exception\NoResultFoundException;
5
use UserAgentParser\Exception\PackageNotLoadedException;
6
use UserAgentParser\Model;
7
8
class DonatjUAParser extends AbstractProvider
9
{
10
    /**
11
     * Name of the provider
12
     *
13
     * @var string
14
     */
15
    protected $name = 'DonatjUAParser';
16
17
    /**
18
     * Homepage of the provider
19
     *
20
     * @var string
21
     */
22
    protected $homepage = 'https://github.com/donatj/PhpUserAgent';
23
24
    /**
25
     * Composer package name
26
     *
27
     * @var string
28
     */
29
    protected $packageName = 'donatj/phpuseragentparser';
30
31
    protected $detectionCapabilities = [
32
33
        'browser' => [
34
            'name'    => true,
35
            'version' => true,
36
        ],
37
38
        'renderingEngine' => [
39
            'name'    => false,
40
            'version' => false,
41
        ],
42
43
        'operatingSystem' => [
44
            'name'    => false,
45
            'version' => false,
46
        ],
47
48
        'device' => [
49
            'model'    => false,
50
            'brand'    => false,
51
            'type'     => false,
52
            'isMobile' => false,
53
            'isTouch'  => false,
54
        ],
55
56
        'bot' => [
57
            'isBot' => false,
58
            'name'  => false,
59
            'type'  => false,
60
        ],
61
    ];
62
63 9
    public function __construct()
64
    {
65 9
        if (! function_exists('parse_user_agent')) {
66
            throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider');
67
        }
68 9
    }
69
70
    /**
71
     *
72
     * @param array $resultRaw
73
     *
74
     * @return bool
75
     */
76 2
    private function hasResult(array $resultRaw)
77
    {
78 2
        if ($this->isRealResult($resultRaw['browser'])) {
79 1
            return true;
80
        }
81
82 1
        return false;
83
    }
84
85
    /**
86
     *
87
     * @param Model\Browser $browser
88
     * @param array         $resultRaw
89
     */
90 1
    private function hydrateBrowser(Model\Browser $browser, array $resultRaw)
91
    {
92 1
        if ($this->isRealResult($resultRaw['browser']) === true) {
93 1
            $browser->setName($resultRaw['browser']);
94 1
        }
95
96 1
        if ($this->isRealResult($resultRaw['version']) === true) {
97 1
            $browser->getVersion()->setComplete($resultRaw['version']);
98 1
        }
99 1
    }
100
101 2
    public function parse($userAgent, array $headers = [])
102
    {
103 2
        $resultRaw = parse_user_agent($userAgent);
104
105 2
        if ($this->hasResult($resultRaw) !== true) {
106 1
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
107
        }
108
109
        /*
110
         * Hydrate the model
111
         */
112 1
        $result = new Model\UserAgent();
113 1
        $result->setProviderResultRaw($resultRaw);
114
115
        /*
116
         * Bot detection - is currently not possible!
117
         */
118
119
        /*
120
         * hydrate the result
121
         */
122 1
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
123
        // renderingEngine not available
124
        // os is mixed with device informations
125
        // device is mixed with os
126
127 1
        return $result;
128
    }
129
}
130