Completed
Push — master ( 0f3f6d...5694f4 )
by ARCANEDEV
14s
created

src/Parsers/UserAgentParser.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    /** @var  \UAParser\Parser */
18
    protected $parser;
19
20
    /** @var string */
21
    public $basePath;
22
23
    /* ------------------------------------------------------------------------------------------------
24
     |  Constructor
25
     | ------------------------------------------------------------------------------------------------
26
     */
27
    /**
28
     * UserAgentParser constructor.
29
     *
30
     * @param  \UAParser\Parser  $parser
31
     * @param  string            $basePath
32
     * @param  null              $userAgent
33
     */
34 15
    public function __construct($parser, $basePath, $userAgent = null)
0 ignored issues
show
__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...
35
    {
36 15
        if ( ! $userAgent && isset($_SERVER['HTTP_USER_AGENT'])) {
37
            $userAgent = $_SERVER['HTTP_USER_AGENT'];
38
        }
39
40 15
        $this->parser   = $parser->parse($userAgent);
41 15
        $this->basePath = $basePath;
42 15
    }
43
44
    /* ------------------------------------------------------------------------------------------------
45
     |  Main Functions
46
     | ------------------------------------------------------------------------------------------------
47
     */
48
    /**
49
     * Get the OS version.
50
     *
51
     * @return string
52
     */
53 9
    public function getOperatingSystemVersion()
54
    {
55 9
        $os = $this->getOperatingSystem();
56
57 9
        return $this->prepareVersion([$os->major, $os->minor, $os->patch]);
58
    }
59
60
    /**
61
     * Get the OS Family.
62
     *
63
     * @return string|null
64
     */
65 9
    public function getOperatingSystemFamily()
66
    {
67
        try {
68 9
            return $this->parser->os->family;
69
        }
70
        catch (\Exception $e) {
71
            return null;
72
        }
73
    }
74
75
    /**
76
     * Get the browser.
77
     *
78
     * @return string
79
     */
80 9
    public function getBrowser()
81
    {
82 9
        return $this->getUserAgent()->family;
83
    }
84
85
    /**
86
     * Get the user agent version.
87
     *
88
     * @return string
89
     */
90 9
    public function getUserAgentVersion()
91
    {
92 9
        $ua = $this->getUserAgent();
93
94 9
        return $this->prepareVersion([$ua->major, $ua->minor, $ua->patch]);
95
    }
96
97
    /**
98
     * Get the original user agent.
99
     *
100
     * @return string
101
     */
102 9
    public function getOriginalUserAgent()
103
    {
104 9
        return $this->parser->originalUserAgent;
105
    }
106
107
    /**
108
     * Get the user agent.
109
     *
110
     * @return \UAParser\Result\UserAgent
111
     */
112 9
    public function getUserAgent()
113
    {
114 9
        return $this->parser->ua;
0 ignored issues
show
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...
115
    }
116
117
    /**
118
     * Get the operating system.
119
     *
120
     * @return \UAParser\Result\OperatingSystem
121
     */
122 9
    public function getOperatingSystem()
123
    {
124 9
        return $this->parser->os;
125
    }
126
127
    /**
128
     * Get the device.
129
     *
130
     * @return \UAParser\Result\Device
131
     */
132
    public function getDevice()
133
    {
134
        return $this->parser->device;
0 ignored issues
show
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...
135
    }
136
137
    /* ------------------------------------------------------------------------------------------------
138
     |  Other Functions
139
     | ------------------------------------------------------------------------------------------------
140
     */
141
    /**
142
     * Prepare the version.
143
     *
144
     * @param  array  $version
145
     *
146
     * @return string
147
     */
148 12
    protected function prepareVersion(array $version)
149
    {
150 12
        return implode('.', array_filter($version));
151
    }
152
}
153