Device   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 23
c 3
b 0
f 2
lcom 2
cbo 1
dl 0
loc 235
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 1
A getType() 0 4 1
A getModel() 0 4 1
A getModelVersion() 0 4 1
A getOperatingSystem() 0 4 1
A getOperatingSystemVersion() 0 4 1
A getBrowser() 0 4 1
A getBrowserVersion() 0 4 1
A getVendor() 0 4 1
A getUserAgent() 0 4 1
A isMobile() 0 4 2
A isDesktop() 0 4 1
A isTablet() 0 4 1
A isBot() 0 4 1
A toArray() 0 18 1
C create() 0 29 7
1
<?php
2
namespace MobileDetect\Device;
3
4
use MobileDetect\Exception\InvalidDeviceSpecificationException;
5
6
/**
7
 * Class Device
8
 *
9
 * Defines properties for a particular device.
10
 */
11
class Device implements DeviceInterface
12
{
13
14
    /**
15
     * A list of required keys for the factory method.
16
     *
17
     * @var array
18
     */
19
    public static $requiredFields = array(
20
        'deviceType',
21
        'userAgent'
22
    );    
23
    
24
    /**
25
     * One of the TYPE_* constants.
26
     *
27
     * @var int
28
     */
29
    protected $type;
30
31
    /**
32
     * The 'User-Agent' header as a string.
33
     *
34
     * @var string
35
     */
36
    protected $userAgent;
37
38
    /**
39
     * The model of this device, such as Samsung, Chromebook, iPhone, or ...
40
     *
41
     * @var string
42
     */
43
    protected $model;
44
45
    /**
46
     * Version of the particular model, such as 5S for the iPhone 5S.
47
     *
48
     * @var string
49
     */
50
    protected $modelVersion;
51
52
    /**
53
     * The operating system, such as 'Windows' or 'iOS'.
54
     *
55
     * @var string
56
     */
57
    protected $operatingSystem;
58
59
    /**
60
     * The OS version.
61
     *
62
     * @var string
63
     */
64
    protected $operatingSystemVersion;
65
66
    /**
67
     * The browser. If this is a bot, then this value is populated with the bot identifier, such as
68
     * "facebookexternalhit" for Facebook crawling or "GoogleBot" for the Google.
69
     *
70
     * @var string
71
     */
72
    protected $browser;
73
74
    /**
75
     * The version of the browser (or bot crawler).
76
     *
77
     * @var string
78
     */
79
    protected $browserVersion;
80
81
    /**
82
     * The vendor name for this device.
83
     *
84
     * @var string
85
     */
86
    protected $vendor;
87
88
    /**
89
     * @param $userAgent
90
     * @param $deviceType
91
     * @param $deviceModel
92
     * @param $deviceModelVersion
93
     * @param $operatingSystemModel
94
     * @param $operatingSystemVersion
95
     * @param $browserModel
96
     * @param $browserVersion
97
     * @param $vendor
98
     */
99
    public function __construct(
100
        $userAgent,
101
        $deviceType,
102
        $deviceModel,
103
        $deviceModelVersion,
104
        $operatingSystemModel,
105
        $operatingSystemVersion,
106
        $browserModel,
107
        $browserVersion,
108
        $vendor
109
    ) {
110
        $this->userAgent = $userAgent;
111
        $this->type = $deviceType;
112
        $this->model = $deviceModel;
113
        $this->modelVersion = $deviceModelVersion;
114
        $this->operatingSystem = $operatingSystemModel;
115
        $this->operatingSystemVersion = $operatingSystemVersion;
116
        $this->browser = $browserModel;
117
        $this->browserVersion = $browserVersion;
118
        $this->vendor = $vendor;
119
    }
120
121
    /**
122
     * Retrieves the type of device.
123
     *
124
     * @return int
125
     */
126
    public function getType()
127
    {
128
        return $this->type;
129
    }
130
131
    public function getModel()
132
    {
133
        return $this->model;
134
    }
135
136
    public function getModelVersion()
137
    {
138
        return $this->modelVersion;
139
    }
140
141
    public function getOperatingSystem()
142
    {
143
        return $this->operatingSystem;
144
    }
145
146
    public function getOperatingSystemVersion()
147
    {
148
        return $this->operatingSystemVersion;
149
    }
150
151
    public function getBrowser()
152
    {
153
        return $this->browser;
154
    }
155
156
    public function getBrowserVersion()
157
    {
158
        return $this->browserVersion;
159
    }
160
161
    public function getVendor()
162
    {
163
        return $this->vendor;
164
    }
165
166
    public function getUserAgent()
167
    {
168
        return $this->userAgent;
169
    }
170
171
    public function isMobile()
172
    {
173
        return $this->type == DeviceType::MOBILE || $this->type == DeviceType::TABLET;
174
    }
175
176
    public function isDesktop()
177
    {
178
        return $this->type == DeviceType::DESKTOP;
179
    }
180
181
    public function isTablet()
182
    {
183
        return $this->type == DeviceType::TABLET;
184
    }
185
186
    public function isBot()
187
    {
188
        return $this->type == DeviceType::BOT;
189
    }
190
191
    public function toArray()
192
    {
193
        return array(
194
            'type'                      => $this->getType(),
195
            'isMobile'                  => $this->isMobile(),
196
            'isTablet'                  => $this->isTablet(),
197
            'isDesktop'                 => $this->isDesktop(),
198
            'isBot'                     => $this->isBot(),
199
            'browser'                   => $this->getBrowser(),
200
            'browserVersion'            => $this->getBrowserVersion(),
201
            'model'                     => $this->getModel(),
202
            'modelVersion'              => $this->getModelVersion(),
203
            'operatingSystem'           => $this->getOperatingSystem(),
204
            'operatingSystemVersion'    => $this->getOperatingSystemVersion(),
205
            'userAgent'                 => $this->getUserAgent(),
206
            'vendor'                    => $this->getVendor(),
207
        );
208
    }
209
210
    /**
211
     * @param array $prop   An array of properties to create this device from.
212
     * @return Device The device instance.
213
     * @throws InvalidDeviceSpecificationException When insufficient properties are present to
214
     *                                                       identify this device.
215
     */
216
    public static function create(array $prop)
217
    {
218
        // ensure that all the required keys are present
219
        foreach (self::$requiredFields as $key) {
220
            if (!isset($prop[$key])) {
221
                throw new InvalidDeviceSpecificationException(sprintf("The '%s' property is required.", $key));
222
            }
223
        }
224
225
        // check that a valid type was passed
226
        if ($prop['deviceType'] != DeviceType::DESKTOP
227
            && $prop['deviceType'] != DeviceType::MOBILE
228
            && $prop['deviceType'] != DeviceType::TABLET
229
            && $prop['deviceType'] != DeviceType::BOT
230
        ) {
231
            throw new InvalidDeviceSpecificationException(
232
                sprintf("Unrecognized type: '%s'", $prop['deviceType'])
233
            );
234
        }
235
236
        // create a new instance
237
        return new static(
238
            $prop['userAgent'],
239
            $prop['deviceType'], $prop['deviceModel'], $prop['deviceModelVersion'],
240
            $prop['operatingSystemModel'], $prop['operatingSystemVersion'],
241
            $prop['browserModel'], $prop['browserVersion'],
242
            $prop['vendor']
243
        );
244
    }
245
}