Completed
Push — master ( cb44f9...1fdaf4 )
by Gabriel
07:02
created

Product::deleteImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Waredesk\Models;
4
5
use DateTime;
6
use Waredesk\Collections\Products\Variants;
7
use JsonSerializable;
8
use Waredesk\Entity;
9
use Waredesk\Image;
10
use Waredesk\ReplaceableEntity;
11
12
class Product implements Entity, ReplaceableEntity, JsonSerializable
13
{
14
    private $id;
15
    private $images;
16
    private $variants;
17
    private $name;
18
    private $description;
19
    private $notes;
20
    private $creation;
21
    private $modification;
22
23
    /**
24
     * @var Image
25
     */
26
    private $pendingImage;
27
28
    /**
29
     * @var bool
30
     */
31
    private $deleteImage = false;
32
33 4
    public function __construct(array $data = null)
34
    {
35 4
        $this->variants = new Variants();
36 4
        $this->reset($data);
37 4
    }
38
39
    public function __clone()
40
    {
41
        $this->variants = clone $this->variants;
42
    }
43
44 3
    public function getId(): ? string
45
    {
46 3
        return $this->id;
47
    }
48
49 1
    public function getImages(): ? array
50
    {
51 1
        return $this->images;
52
    }
53
54 3
    public function getVariants(): ? Variants
55
    {
56 3
        return $this->variants;
57
    }
58
59 3
    public function getName(): ? string
60
    {
61 3
        return $this->name;
62
    }
63
64 3
    public function getDescription(): ? string
65
    {
66 3
        return $this->description;
67
    }
68
69 3
    public function getNotes(): ? string
70
    {
71 3
        return $this->notes;
72
    }
73
74
    public function getCreation(): ?DateTime
75
    {
76
        return $this->creation;
77
    }
78
79
    public function getModification(): ? DateTime
80
    {
81
        return $this->modification;
82
    }
83
84 4
    public function reset(array $data = null)
85
    {
86 4
        if ($data) {
87 3
            foreach ($data as $key => $value) {
88
                switch ($key) {
89 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...
90 3
                        $this->id = $value;
91 3
                        break;
92 3
                    case 'images':
93 3
                        $this->deleteImage = false;
94 3
                        $this->pendingImage = null;
95 3
                        $this->images = $value;
96 3
                        break;
97 3
                    case 'variants':
98 3
                        $this->variants = $value;
99 3
                        break;
100 3
                    case 'name':
101 3
                        $this->name = $value;
102 3
                        break;
103 3
                    case 'description':
104 3
                        $this->description = $value;
105 3
                        break;
106 3
                    case 'notes':
107 3
                        $this->notes = $value;
108 3
                        break;
109 3
                    case 'creation':
110 3
                        $this->creation = $value;
111 3
                        break;
112 3
                    case 'modification':
113 3
                        $this->modification = $value;
114 3
                        break;
115
                }
116
            }
117
        }
118 4
    }
119
120
    public function deleteImage()
121
    {
122
        $this->deleteImage = true;
123
    }
124
125 1
    public function setImage(Image $image = null)
126
    {
127 1
        $this->pendingImage = $image;
128 1
    }
129
130 3
    public function setName(string $name = null)
131
    {
132 3
        $this->name = $name;
133 3
    }
134
135 3
    public function setDescription(string $description = null)
136
    {
137 3
        $this->description = $description;
138 3
    }
139
140
    public function setNotes(string $notes = null)
141
    {
142
        $this->notes = $notes;
143
    }
144
145 3
    public function jsonSerialize(): array
146
    {
147
        $returnValue = [
148 3
            'variants' => $this->getVariants()->jsonSerialize(),
149 3
            'name' => $this->getName(),
150 3
            'description' => $this->getDescription(),
151 3
            'notes' => $this->getNotes(),
152
        ];
153 3
        if ($this->pendingImage) {
154 1
            $returnValue['image'] = $this->pendingImage->toBase64();
155 2
        } elseif ($this->deleteImage) {
156
            $returnValue['image'] = null;
157
        }
158 3
        if ($this->getId()) {
159
            $returnValue = array_merge(['id' => $this->getId()], $returnValue);
160
        }
161 3
        return $returnValue;
162
    }
163
}
164