Completed
Push — master ( b4ecb3...bd41e0 )
by Gabriel
04:57
created

Code   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 67.57%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 5
dl 0
loc 127
ccs 50
cts 74
cp 0.6757
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __clone() 0 4 1
B toVariantCode() 0 25 2
A getId() 0 4 1
A getName() 0 4 1
A getQuantity() 0 4 1
A getElements() 0 4 1
A getCreation() 0 4 1
A getModification() 0 4 1
D reset() 0 27 9
A setName() 0 4 1
A setQuantity() 0 4 1
A jsonSerialize() 0 12 2
1
<?php
2
3
namespace Waredesk\Models;
4
5
use DateTime;
6
use Waredesk\Collections\Codes\Elements;
7
use JsonSerializable;
8
use Waredesk\Entity;
9
use Waredesk\ReplaceableEntity;
10
use Waredesk\Models\Product\Variant\Code as VariantCode;
11
12
class Code implements Entity, ReplaceableEntity, JsonSerializable
13
{
14
    private $id;
15
    private $name;
16
    private $quantity;
17
    private $elements;
18
    private $creation;
19
    private $modification;
20
21 4
    public function __construct(array $data = null)
22
    {
23 4
        $this->elements = new Elements();
24 4
        $this->reset($data);
25 4
    }
26
27 1
    public function __clone()
28
    {
29 1
        $this->elements = clone $this->elements;
30 1
    }
31
32
    public function toVariantCode(): VariantCode
33
    {
34
        $code = new VariantCode();
35
        $code->reset([
36
            'code' => $this->getId(),
37
            'name' => $this->getName(),
38
            'quantity' => $this->getQuantity(),
39
            'creation' => $this->getCreation(),
40
            'modification' => $this->getModification(),
41
        ]);
42
        foreach ($this->getElements() as $element) {
43
            $nextElement = new VariantCode\Element();
44
            $nextElement->reset([
45
                'element' => $element->getId(),
46
                'type' => $element->getType(),
47
                'value' => $element->getValue(),
48
                'auto_increment' => $element->getAutoIncrement(),
49
                'pad_direction' => $element->getPadDirection(),
50
                'pad_char' => $element->getPadChar(),
51
                'pad_length' => $element->getPadLength(),
52
            ]);
53
            $code->getElements()->add($nextElement);
54
        }
55
        return $code;
56
    }
57
58 2
    public function getId(): ? string
59
    {
60 2
        return $this->id;
61
    }
62
63 2
    public function getName(): ? string
64
    {
65 2
        return $this->name;
66
    }
67
68 2
    public function getQuantity(): ? int
69
    {
70 2
        return $this->quantity;
71
    }
72
73 4
    public function getElements(): ? Elements
74
    {
75 4
        return $this->elements;
76
    }
77
78
    public function getCreation(): ?DateTime
79
    {
80
        return $this->creation;
81
    }
82
83
    public function getModification(): ? DateTime
84
    {
85
        return $this->modification;
86
    }
87
88 4
    public function reset(array $data = null)
89
    {
90 4
        if ($data) {
91 3
            foreach ($data as $key => $value) {
92
                switch ($key) {
93 3
                    case 'id':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
94 3
                        $this->id = $value;
95 3
                        break;
96 3
                    case 'name':
97 3
                        $this->name = $value;
98 3
                        break;
99 3
                    case 'quantity':
100 2
                        $this->quantity = $value;
101 2
                        break;
102 3
                    case 'elements':
103 2
                        $this->elements = $value;
104 2
                        break;
105 3
                    case 'creation':
106 3
                        $this->creation = $value;
107 3
                        break;
108 3
                    case 'modification':
109 3
                        $this->modification = $value;
110 3
                        break;
111
                }
112
            }
113
        }
114 4
    }
115
116 1
    public function setName(string $name = null)
117
    {
118 1
        $this->name = $name;
119 1
    }
120
121 1
    public function setQuantity(int $quantity = null)
122
    {
123 1
        $this->quantity = $quantity;
124 1
    }
125
126 2
    public function jsonSerialize(): array
127
    {
128
        $returnValue = [
129 2
            'name' => $this->getName(),
130 2
            'quantity' => $this->getQuantity(),
131 2
            'elements' => $this->getElements()->jsonSerialize(),
132
        ];
133 2
        if ($this->getId()) {
134 1
            $returnValue = array_merge(['id' => $this->getId()], $returnValue);
135
        }
136 2
        return $returnValue;
137
    }
138
}
139