Completed
Push — master ( b2620d...cacd0e )
by Gabriel
02:27
created

Product::setImages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1

1 Method

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