Request   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 222
Duplicated Lines 15.32 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 3
dl 34
loc 222
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 23 3
B validate() 0 8 5
A getUsername() 0 4 1
A setUsername() 0 6 1
A getApiKey() 0 4 1
A setApiKey() 0 6 1
A yieldBins() 0 6 2
A yieldItems() 0 6 2
A getItem() 0 4 1
A getBin() 0 4 1
A addItem() 17 17 3
A addBin() 17 17 3
A getBins() 0 4 1
A setBins() 0 6 1
A getItems() 0 4 1
A setItems() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace BinPacking3d\Entity;
2
3
use BinPacking3d\EntityInterface;
4
use BinPacking3d\Exception\CriticalException;
5
6
/**
7
 * Class Request
8
 * @package BinPacking3d\Entity
9
 */
10
class Request implements EntityInterface
11
{
12
13
    /**
14
     * @var
15
     */
16
    private $username;
17
    /**
18
     * @var
19
     */
20
    private $apiKey;
21
22
    /**
23
     * @var array
24
     */
25
    private $bins = array();
26
27
    /**
28
     * @var array
29
     */
30
    private $items = array();
31
32
    /**
33
     * @return array
34
     * @throws \Exception
35
     */
36
    public function render()
37
    {
38
        $this->validate();
39
40
        // Render bins
41
        $bins = [];
42
        foreach ($this->yieldBins() as $bin) {
43
            $bins[] = $bin->render();
44
        }
45
46
        // Render items
47
        $items = [];
48
        foreach ($this->yieldItems() as $item) {
49
            $items[] = $item->render();
50
        }
51
52
        return [
53
            'username' => $this->getUsername(),
54
            'api_key' => $this->getApiKey(),
55
            'bins' => $bins,
56
            'items' => $items,
57
        ];
58
    }
59
60
    /**
61
     * @return bool
62
     * @throws \Exception
63
     */
64
    public function validate()
65
    {
66
        if ($this->getUsername() === null || $this->getApiKey() === null || empty($this->bins) || empty($this->items)) {
67
            throw new CriticalException('Not all required variables entered for rendering.');
68
        }
69
70
        return true;
71
    }
72
73
    /**
74
     * @return mixed
75
     */
76
    public function getUsername()
77
    {
78
        return $this->username;
79
    }
80
81
    /**
82
     * @param mixed $username
83
     * @return Request
84
     */
85
    public function setUsername($username)
86
    {
87
        $this->username = $username;
88
89
        return $this;
90
    }
91
92
    /**
93
     * @return mixed
94
     */
95
    public function getApiKey()
96
    {
97
        return $this->apiKey;
98
    }
99
100
    /**
101
     * @param mixed $apiKey
102
     * @return Request
103
     */
104
    public function setApiKey($apiKey)
105
    {
106
        $this->apiKey = $apiKey;
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return \Generator
113
     */
114
    private function yieldBins()
115
    {
116
        foreach ($this->bins as $bin) {
117
            yield $bin;
118
        }
119
    }
120
121
    /**
122
     * @return \Generator
123
     */
124
    private function yieldItems()
125
    {
126
        foreach ($this->items as $item) {
127
            yield $item;
128
        }
129
    }
130
131
    /**
132
     * @param $identifier
133
     * @return mixed
134
     */
135
    public function getItem($identifier)
136
    {
137
        return $this->items[$identifier];
138
    }
139
140
    /**
141
     * @param $identifier
142
     * @return mixed
143
     */
144
    public function getBin($identifier)
145
    {
146
        return $this->bins[$identifier];
147
    }
148
149
    /**
150
     * @param Item $item
151
     * @return $this
152
     */
153 View Code Duplication
    public function addItem(Item $item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        // Check for unique identifier
156
        if (array_key_exists($item->getItemIdentifier(), $this->items)) {
157
            throw new CriticalException('Identifier already exists');
158
        }
159
160
        // Check if we can validate it
161
        if (!$item->validate()) {
162
            throw new CriticalException('Cannot validate item settings, item: ' . print_r($item->render(), true));
163
        }
164
165
        // Add to store
166
        $this->items[$item->getItemIdentifier()] = $item;
167
168
        return $this;
169
    }
170
171
    /**
172
     * @param Bin $bin
173
     * @return $this
174
     */
175 View Code Duplication
    public function addBin(Bin $bin)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
176
    {
177
        // Check for unique identifier
178
        if (array_key_exists($bin->getIdentifier(), $this->bins)) {
179
            throw new CriticalException('Identifier already exists');
180
        }
181
182
        // Check if we can validate it
183
        if (!$bin->validate()) {
184
            throw new CriticalException('Cannot validate bin settings, bin: ' . print_r($bin->render(), true));
185
        }
186
187
        // Add to store
188
        $this->bins[$bin->getIdentifier()] = $bin;
189
190
        return $this;
191
    }
192
193
    /**
194
     * @return array
195
     */
196
    public function getBins()
197
    {
198
        return $this->bins;
199
    }
200
201
    /**
202
     * @param array $bins
203
     * @return Request
204
     */
205
    public function setBins($bins)
206
    {
207
        $this->bins = $bins;
208
209
        return $this;
210
    }
211
212
    /**
213
     * @return array
214
     */
215
    public function getItems()
216
    {
217
        return $this->items;
218
    }
219
220
    /**
221
     * @param array $items
222
     * @return Request
223
     */
224
    public function setItems($items)
225
    {
226
        $this->items = $items;
227
228
        return $this;
229
    }
230
231
}
232