Support::getUserAgent()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP\Helper;
5
6
/**
7
 * class to help getting the user agent
8
 */
9
final class Support implements SupportInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    private $source = [];
15
16
    /**
17
     * The HTTP Headers that this application will look through to find the best
18
     * User Agent, if one is not specified
19
     *
20
     * @var array
21
     */
22
    private $userAgentHeaders = [
23
        'HTTP_X_DEVICE_USER_AGENT',
24
        'HTTP_X_ORIGINAL_USER_AGENT',
25
        'HTTP_X_OPERAMINI_PHONE_UA',
26
        'HTTP_X_SKYFIRE_PHONE',
27
        'HTTP_X_BOLT_PHONE_UA',
28
        'HTTP_USER_AGENT',
29
    ];
30
31
    /**
32
     * @param array|null $source
33
     */
34 2
    public function __construct(?array $source = null)
35
    {
36 2
        if (null === $source) {
37 1
            $source = [];
38
        }
39
40 2
        $this->source = $source;
41 2
    }
42
43
    /**
44
     * detect the useragent
45
     *
46
     * @return string
47
     */
48 2
    public function getUserAgent() : string
49
    {
50 2
        $userAgent = '';
51
52 2
        foreach ($this->userAgentHeaders as $header) {
53 2
            if (array_key_exists($header, $this->source)
54 1
                && $this->source[$header]
55
            ) {
56 1
                $userAgent = $this->cleanParam($this->source[$header]);
57
58 1
                break;
59
            }
60
        }
61
62 2
        return $userAgent;
63
    }
64
65
    /**
66
     * clean Parameters taken from GET or POST Variables
67
     *
68
     * @param string $param the value to be cleaned
69
     *
70
     * @return string
71
     */
72 1
    private function cleanParam(string $param) : string
73
    {
74 1
        return strip_tags(trim(urldecode($param)));
75
    }
76
}
77