Completed
Push — master ( b1b849...bfd903 )
by Elf
02:44
created

Agent::version()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Agent::__construct() 0 5 1
1
<?php
2
3
namespace ElfSundae\Agent;
4
5
use ArrayAccess;
6
use JsonSerializable;
7
use Illuminate\Support\Str;
8
use Jenssegers\Agent\Agent as BaseAgent;
9
use Illuminate\Contracts\Support\Jsonable;
10
use Illuminate\Contracts\Support\Arrayable;
11
12
class Agent extends BaseAgent implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
13
{
14
    use Concerns\HasAttributes;
15
16
    /**
17
     * The additional properties.
18
     *
19
     * @var array
20
     */
21
    protected static $addedProperties = [
22
        'WeChat' => 'MicroMessenger/[VER]',
23
        'QQ' => 'QQ/[VER]',
24
        'NetType' => 'NetType/[VER]',
25
        'Language' => 'Language/[VER]',
26
    ];
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 28
    public function __construct(array $headers = null, $userAgent = null)
32
    {
33 28
        parent::__construct($headers, $userAgent);
34
35 28
        $this->addProperties();
36 28
    }
37
38
    /**
39
     * Add the additional properties.
40
     */
41 28
    protected function addProperties()
42
    {
43 28
        if (! empty(static::$addedProperties)) {
44 4
            parent::$properties = array_merge(
45 4
                parent::$properties,
46 2
                static::$addedProperties
47 2
            );
48
49 4
            static::$addedProperties = [];
50 2
        }
51 28
    }
52
53
    /**
54
     * Get the version number of the given property in the User-Agent.
55
     *
56
     * @param  string  $propertyName
57
     * @return float
58
     */
59 4
    public function versionNumber($propertyName)
60
    {
61 4
        return $this->version($propertyName, self::VERSION_TYPE_FLOAT);
62
    }
63
64
    /**
65
     * Get the operating system name.
66
     *
67
     * @return string
68
     */
69 8
    public function os()
70
    {
71 8
        return $this->platform() ?: null;
72
    }
73
74
    /**
75
     * Get the operating system version.
76
     *
77
     * @return string
78
     */
79 4
    public function osVersion()
80
    {
81 4
        if ($version = $this->version($this->os())) {
82 4
            return str_replace('_', '.', $version);
83
        }
84
    }
85
86
    /**
87
     * Get the accept language.
88
     *
89
     * @param  string|null  $preferLanguage
90
     * @return string|bool
91
     */
92 4
    public function language($preferLanguage = null)
93
    {
94 4
        $languages = $this->languages();
95
96 4
        if (is_null($preferLanguage)) {
97 4
            return reset($languages);
98
        }
99
100 4
        $preferLanguage = strtolower($preferLanguage);
101
102 4
        foreach ($languages as $lang) {
103 4
            if (Str::startsWith($lang, $preferLanguage)) {
104 4
                return true;
105
            }
106 2
        }
107
108 4
        return false;
109
    }
110
}
111