Support   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 68
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getUserAgent() 0 16 4
A cleanParam() 0 4 1
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