UserAgentParser::getDevice()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace Arcanedev\LaravelTracker\Parsers;
2
3
use Arcanedev\LaravelTracker\Contracts\Parsers\UserAgentParser as UserAgentParserContract;
4
5
/**
6
 * Class     UserAgentParser
7
 *
8
 * @package  Arcanedev\LaravelTracker\Parsers
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class UserAgentParser implements UserAgentParserContract
12
{
13
    /* -----------------------------------------------------------------
14
     |  Properties
15
     | -----------------------------------------------------------------
16
     */
17
18
    /** @var  \UAParser\Parser */
19
    protected $parser;
20
21
    /** @var string */
22
    public $basePath;
23
24
    /* -----------------------------------------------------------------
25
     |  Constructor
26
     | -----------------------------------------------------------------
27
     */
28
29
    /**
30
     * UserAgentParser constructor.
31
     *
32
     * @param  \UAParser\Parser  $parser
33
     * @param  string            $basePath
34
     * @param  null              $userAgent
35
     */
36 15
    public function __construct($parser, $basePath, $userAgent = null)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
37
    {
38 15
        if ( ! $userAgent && isset($_SERVER['HTTP_USER_AGENT'])) {
39
            $userAgent = $_SERVER['HTTP_USER_AGENT'];
40
        }
41
42 15
        $this->parser   = $parser->parse($userAgent);
0 ignored issues
show
Documentation Bug introduced by
It seems like $parser->parse($userAgent) of type object<UAParser\Result\Client> is incompatible with the declared type object<UAParser\Parser> of property $parser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43 15
        $this->basePath = $basePath;
44 15
    }
45
46
    /* -----------------------------------------------------------------
47
     |  Main Methods
48
     | -----------------------------------------------------------------
49
     */
50
51
    /**
52
     * Get the OS version.
53
     *
54
     * @return string
55
     */
56 9
    public function getOperatingSystemVersion()
57
    {
58 9
        $os = $this->getOperatingSystem();
59
60 9
        return $this->prepareVersion([$os->major, $os->minor, $os->patch]);
61
    }
62
63
    /**
64
     * Get the OS Family.
65
     *
66
     * @return string|null
67
     */
68 9
    public function getOperatingSystemFamily()
69
    {
70
        try {
71 9
            return $this->parser->os->family;
0 ignored issues
show
Bug introduced by
The property os does not seem to exist in UAParser\Parser.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
72
        }
73
        catch (\Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { return null; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
74
            return null;
75
        }
76
    }
77
78
    /**
79
     * Get the browser.
80
     *
81
     * @return string
82
     */
83 9
    public function getBrowser()
84
    {
85 9
        return $this->getUserAgent()->family;
86
    }
87
88
    /**
89
     * Get the user agent version.
90
     *
91
     * @return string
92
     */
93 9
    public function getUserAgentVersion()
94
    {
95 9
        $ua = $this->getUserAgent();
96
97 9
        return $this->prepareVersion([$ua->major, $ua->minor, $ua->patch]);
98
    }
99
100
    /**
101
     * Get the original user agent.
102
     *
103
     * @return string
104
     */
105 9
    public function getOriginalUserAgent()
106
    {
107 9
        return $this->parser->originalUserAgent;
0 ignored issues
show
Bug introduced by
The property originalUserAgent does not seem to exist in UAParser\Parser.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
108
    }
109
110
    /**
111
     * Get the user agent.
112
     *
113
     * @return \UAParser\Result\UserAgent
114
     */
115 9
    public function getUserAgent()
116
    {
117 9
        return $this->parser->ua;
0 ignored issues
show
Bug introduced by
The property ua does not seem to exist in UAParser\Parser.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
118
    }
119
120
    /**
121
     * Get the operating system.
122
     *
123
     * @return \UAParser\Result\OperatingSystem
124
     */
125 9
    public function getOperatingSystem()
126
    {
127 9
        return $this->parser->os;
0 ignored issues
show
Bug introduced by
The property os does not seem to exist in UAParser\Parser.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
128
    }
129
130
    /**
131
     * Get the device.
132
     *
133
     * @return \UAParser\Result\Device
134
     */
135
    public function getDevice()
136
    {
137
        return $this->parser->device;
0 ignored issues
show
Bug introduced by
The property device does not seem to exist. Did you mean deviceParser?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
138
    }
139
140
    /* -----------------------------------------------------------------
141
     |  Other Methods
142
     | -----------------------------------------------------------------
143
     */
144
145
    /**
146
     * Prepare the version.
147
     *
148
     * @param  array  $version
149
     *
150
     * @return string
151
     */
152 12
    protected function prepareVersion(array $version)
153
    {
154 12
        return implode('.', array_filter($version));
155
    }
156
}
157