Completed
Push — master ( ad3d01...71bbde )
by ARCANEDEV
02:06
created

DeviceDetector::clientVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Agent\Detectors;
6
7
use Arcanedev\Agent\Contracts\Detector;
8
use DeviceDetector\DeviceDetector as BaseDetector;
9
use Illuminate\Http\Request;
10
11
/**
12
 * Class     DeviceDetector
13
 *
14
 * @package  Arcanedev\Agent\Detectors
15
 * @author   ARCANEDEV <[email protected]>
16
 *
17
 * @mixin  \DeviceDetector\DeviceDetector
18
 */
19
class DeviceDetector implements Detector
20
{
21
    /* -----------------------------------------------------------------
22
     |  Properties
23
     | -----------------------------------------------------------------
24
     */
25
26
    /** @var  \DeviceDetector\DeviceDetector */
27
    protected $detector;
28
29
    /* -----------------------------------------------------------------
30
     |  Main Methods
31
     | -----------------------------------------------------------------
32
     */
33
34
    /**
35
     * Handle the given request.
36
     *
37
     * @param  \Illuminate\Http\Request  $request
38
     *
39
     * @return $this
40
     */
41 372
    public function handle(Request $request): Detector
42
    {
43 372
        $userAgent = $request->server('HTTP_USER_AGENT');
44
45 248
        $this->detector = tap(new BaseDetector($userAgent), function (BaseDetector $detector) {
0 ignored issues
show
Bug introduced by
It seems like $userAgent defined by $request->server('HTTP_USER_AGENT') on line 43 can also be of type array or null; however, DeviceDetector\DeviceDetector::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
46 372
            $detector->parse();
47 372
        });
48
49 372
        return $this;
50
    }
51
52
    /* -----------------------------------------------------------------
53
     |  Accessors
54
     | -----------------------------------------------------------------
55
     */
56
57
    /**
58
     * Get the OS's name.
59
     *
60
     * @return string
61
     */
62 42
    public function osName(): string
63
    {
64 42
        return $this->getOs('name');
0 ignored issues
show
Documentation Bug introduced by
The method getOs does not exist on object<Arcanedev\Agent\Detectors\DeviceDetector>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
65
    }
66
67
    /**
68
     * Get the OS's short name.
69
     *
70
     * @return string
71
     */
72 42
    public function osShortName(): string
73
    {
74 42
        return $this->getOs('short_name');
0 ignored issues
show
Documentation Bug introduced by
The method getOs does not exist on object<Arcanedev\Agent\Detectors\DeviceDetector>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75
    }
76
77
    /**
78
     * Get the OS's version.
79
     *
80
     * @return string
81
     */
82 36
    public function osVersion(): string
83
    {
84 36
        return $this->getOs('version');
0 ignored issues
show
Documentation Bug introduced by
The method getOs does not exist on object<Arcanedev\Agent\Detectors\DeviceDetector>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
85
    }
86
87
    /**
88
     * Get the client name.
89
     *
90
     * @return string
91
     */
92 72
    public function clientName()
93
    {
94 72
        return $this->getClient('name');
0 ignored issues
show
Documentation Bug introduced by
The method getClient does not exist on object<Arcanedev\Agent\Detectors\DeviceDetector>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
95
    }
96
97
    /**
98
     * Get the client name.
99
     *
100
     * @return string
101
     */
102 72
    public function clientShortName()
103
    {
104 72
        return $this->getClient('short_name');
0 ignored issues
show
Documentation Bug introduced by
The method getClient does not exist on object<Arcanedev\Agent\Detectors\DeviceDetector>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
105
    }
106
107
    /**
108
     * Get the client's version.
109
     *
110
     * @return string
111
     */
112 66
    public function clientVersion(): string
113
    {
114 66
        return $this->getClient('version');
0 ignored issues
show
Documentation Bug introduced by
The method getClient does not exist on object<Arcanedev\Agent\Detectors\DeviceDetector>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
115
    }
116
117
    /**
118
     * Get the bot's name.
119
     *
120
     * @return string
121
     */
122 30
    public function botName(): string
123
    {
124 30
        return $this->getBot()['name'] ?? BaseDetector::UNKNOWN;
0 ignored issues
show
Documentation Bug introduced by
The method getBot does not exist on object<Arcanedev\Agent\Detectors\DeviceDetector>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
125
    }
126
127
    /**
128
     * Check if it's a visitor.
129
     *
130
     * @return bool
131
     */
132 30
    public function isVisitor(): bool
133
    {
134 30
        return ! $this->isBot();
0 ignored issues
show
Documentation Bug introduced by
The method isBot does not exist on object<Arcanedev\Agent\Detectors\DeviceDetector>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
135
    }
136
137
    /* -----------------------------------------------------------------
138
     |  Check Methods
139
     | -----------------------------------------------------------------
140
     */
141
142
    /**
143
     * Check the given name matches client's name.
144
     *
145
     * @param  string  $name
146
     *
147
     * @return bool
148
     */
149 72
    public function isClientName($name): bool
150
    {
151 72
        return in_array($name, [
152 72
            $this->clientName(),
153 72
            $this->clientShortName(),
154
        ]);
155
    }
156
157
    /**
158
     * Check the given name matches Operating system's name.
159
     *
160
     * @param  string  $name
161
     *
162
     * @return bool
163
     */
164 42
    public function isOsName($name): bool
165
    {
166 42
        return in_array($name, [
167 42
            $this->osName(),
168 42
            $this->osShortName(),
169
        ]);
170
    }
171
172
    /* -----------------------------------------------------------------
173
     |  Other Methods
174
     | -----------------------------------------------------------------
175
     */
176
177
    /**
178
     * @param  string  $name
179
     * @param  array   $params
180
     *
181
     * @return mixed
182
     */
183 354
    public function __call($name, $params)
184
    {
185 354
        return call_user_func_array([$this->detector, $name], $params);
186
    }
187
}
188