Device::setCreated()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PHPushbullet;
4
5
class Device
6
{
7
    /**
8
     * The fields that we want to retrieve for the device
9
     *
10
     * @var array $fields
11
     */
12
13
    protected $fields = [
14
        'nickname',
15
        'iden',
16
        'model',
17
        'type',
18
        'active',
19
        'pushable',
20
        'manufacturer',
21
        'created',
22
    ];
23
24 12
    public function __construct(array $attr)
25
    {
26 12
        foreach ($this->fields as $field) {
27 12
            $this->{$field} = $this->getFieldValue($attr, $field);
28 12
        }
29 12
    }
30
31
    /**
32
     * @param array $attr
33
     * @param string $field
34
     *
35
     * @return string
36
     */
37 12
    protected function getFieldValue(array $attr, $field)
38
    {
39 12
        $method = 'set' . ucwords($field);
40
41 12
        if (method_exists($this, $method)) {
42
            // If there is a setter for this field, use that
43 12
            return $this->{$method}($attr[$field]);
44
        }
45
46
        // Otherwise just set the property
47 12
        return (isset($attr[$field])) ? $attr[$field] : null;
48
    }
49
50
    /**
51
     * Format the date so that it's readable
52
     *
53
     * @param integer $date
54
     * @return string
55
     */
56
57 12
    protected function setCreated($date)
58
    {
59 12
        return date('Y-m-d h:i:sA T', $date);
60
    }
61
}
62