Service::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sheldon
5
 * Date: 18-6-6
6
 * Time: 下午6:25.
7
 */
8
9
namespace MiotApi\Contract\Instance;
10
11
use MiotApi\Contract\Specification\ServiceSpecification;
12
use MiotApi\Contract\Specification\Specification;
13
use MiotApi\Contract\Urn;
14
use MiotApi\Util\Collection\Collection;
15
16
class Service extends Specification
17
{
18
    protected $data;
19
20
    protected $propertiesNode;
21
22
    /**
23
     * 实例ID(Instance ID,简称iid).
24
     *
25
     * @var
26
     */
27
    protected $iid;
28
29
    /**
30
     * type对象
31
     *
32
     * @var
33
     */
34
    protected $specification;
35
36
    public function __construct($data = [])
37
    {
38
        $this->data = $data;
39
        $this->init();
40
    }
41
42
    /**
43
     * @throws \MiotApi\Exception\SpecificationErrorException
44
     */
45
    public function init()
46
    {
47
        $this->collection = new Collection($this->data);
48
        $this->iid = $this->collection->get('iid');
49
        $this->urn = new Urn($this->collection->get('type'));
50
51
        $this->specification = new ServiceSpecification($this->urn->getBaseUrn());
52
        $this->initProperties();
53
    }
54
55
    /**
56
     * @throws \MiotApi\Exception\SpecificationErrorException
57
     */
58
    protected function initProperties()
59
    {
60
        if ($this->has('properties')) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on MiotApi\Contract\Instance\Service. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        if ($this->/** @scrutinizer ignore-call */ has('properties')) {
Loading history...
61
            $properties = $this->get('properties');
0 ignored issues
show
Bug introduced by
The method get() does not exist on MiotApi\Contract\Instance\Service. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            /** @scrutinizer ignore-call */ 
62
            $properties = $this->get('properties');
Loading history...
62
            if (!empty($properties)) {
63
                foreach ($properties as $property) {
64
                    $this->propertiesNode[$property['iid']] = new Property($property);
65
                }
66
            }
67
        }
68
    }
69
70
    public function getIid()
71
    {
72
        return $this->iid;
73
    }
74
75
    public function property($piid)
76
    {
77
        return $this->propertiesNode[$piid];
78
    }
79
80
    public function getPropertiesNode()
81
    {
82
        return $this->propertiesNode;
83
    }
84
85
    public function getSpecification()
86
    {
87
        return $this->specification;
88
    }
89
90
    public function getSpecificationContext()
91
    {
92
        return $this->specification->toContext();
93
    }
94
}
95