Completed
Push — master ( af15ca...956bc7 )
by ARCANEDEV
16:24 queued 15:26
created

DeviceDetector::fromUserAgent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
 * @author   ARCANEDEV <[email protected]>
15
 *
16
 * @mixin  \DeviceDetector\DeviceDetector
17
 */
18
class DeviceDetector implements Detector
19
{
20
    /* -----------------------------------------------------------------
21
     |  Properties
22
     | -----------------------------------------------------------------
23
     */
24
25
    /** @var  \DeviceDetector\DeviceDetector */
26
    protected $detector;
27
28
    /* -----------------------------------------------------------------
29
     |  Main Methods
30
     | -----------------------------------------------------------------
31
     */
32
33
    /**
34
     * Handle the given request.
35
     *
36
     * @param  \Illuminate\Http\Request  $request
37
     *
38
     * @return $this
39
     */
40 372
    public function handle(Request $request): Detector
41
    {
42 372
        return $this->fromUserAgent(
43 372
            $request->server('HTTP_USER_AGENT')
0 ignored issues
show
Bug introduced by
It seems like $request->server('HTTP_USER_AGENT') targeting Illuminate\Http\Concerns...actsWithInput::server() can also be of type array or null; however, Arcanedev\Agent\Detector...tector::fromUserAgent() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
44
        );
45
    }
46
47
    /**
48
     * Handle from the given user agent.
49
     *
50
     * @param  string  $userAgent
51
     *
52
     * @return $this
53
     */
54 372
    public function fromUserAgent(string $userAgent): Detector
55
    {
56 372
        $this->detector = tap(new BaseDetector($userAgent), function (BaseDetector $detector) {
57 372
            $detector->parse();
58 372
        });
59
60 372
        return $this;
61
    }
62
63
    /* -----------------------------------------------------------------
64
     |  Accessors
65
     | -----------------------------------------------------------------
66
     */
67
68
    /**
69
     * Get the OS's name.
70
     *
71
     * @return string
72
     */
73 42
    public function osName(): string
74
    {
75 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...
76
    }
77
78
    /**
79
     * Get the OS's short name.
80
     *
81
     * @return string
82
     */
83 42
    public function osShortName(): string
84
    {
85 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...
86
    }
87
88
    /**
89
     * Get the OS's version.
90
     *
91
     * @return string
92
     */
93 36
    public function osVersion(): string
94
    {
95 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...
96
    }
97
98
    /**
99
     * Get the client name.
100
     *
101
     * @return string
102
     */
103 72
    public function clientName()
104
    {
105 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...
106
    }
107
108
    /**
109
     * Get the client name.
110
     *
111
     * @return string
112
     */
113 72
    public function clientShortName()
114
    {
115 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...
116
    }
117
118
    /**
119
     * Get the client's version.
120
     *
121
     * @return string
122
     */
123 66
    public function clientVersion(): string
124
    {
125 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...
126
    }
127
128
    /**
129
     * Get the bot's name.
130
     *
131
     * @return string
132
     */
133 30
    public function botName(): string
134
    {
135 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...
136
    }
137
138
    /**
139
     * Check if it's a visitor.
140
     *
141
     * @return bool
142
     */
143 30
    public function isVisitor(): bool
144
    {
145 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...
146
    }
147
148
    /* -----------------------------------------------------------------
149
     |  Check Methods
150
     | -----------------------------------------------------------------
151
     */
152
153
    /**
154
     * Check the given name matches client's name.
155
     *
156
     * @param  string  $name
157
     *
158
     * @return bool
159
     */
160 72
    public function isClientName(string $name): bool
161
    {
162 72
        return in_array($name, [
163 72
            $this->clientName(),
164 72
            $this->clientShortName(),
165
        ]);
166
    }
167
168
    /**
169
     * Check the given name matches Operating system's name.
170
     *
171
     * @param  string  $name
172
     *
173
     * @return bool
174
     */
175 42
    public function isOsName(string $name): bool
176
    {
177 42
        return in_array($name, [
178 42
            $this->osName(),
179 42
            $this->osShortName(),
180
        ]);
181
    }
182
183
    /* -----------------------------------------------------------------
184
     |  Other Methods
185
     | -----------------------------------------------------------------
186
     */
187
188
    /**
189
     * @param  string  $name
190
     * @param  array   $params
191
     *
192
     * @return mixed
193
     */
194 354
    public function __call(string $name, array $params)
195
    {
196 354
        return call_user_func_array([$this->detector, $name], $params);
197
    }
198
}
199