Item::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 11
dl 0
loc 26
ccs 13
cts 13
cp 1
crap 1
rs 9.8666

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace App\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Gedmo\Mapping\Annotation as Gedmo;
7
use Assert\Assertion;
8
use Assert\AssertionFailedException;
9
use App\Traits\HasTimestamps;
10
use Ramsey\Uuid\UuidInterface;
11
use Ramsey\Uuid\Uuid;
12
use Swagger\Annotations as SWG;
13
use Doctrine\Common\Collections\ArrayCollection;
14
15
/**
16
 * @ORM\Table(name="item")
17
 * @ORM\HasLifecycleCallbacks()
18
 * @ORM\Entity(repositoryClass="App\Repository\ItemRepository")
19
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", hardDelete=false)
20
 */
21
class Item implements \JsonSerializable, UserAwareInterface
22
{
23
    use HasTimestamps;
24
    
25
    /**
26
     * @var UuidInterface
27
     * @ORM\Id()
28
     * @ORM\Column(type="uuid", unique=true)
29
     * @ORM\GeneratedValue(strategy="NONE")
30
     * @SWG\Property(description="UUID", type="string", readOnly=true)
31
     */
32
    private $id;
33
34
    /**
35
     * @var string
36
     * @ORM\Column(type="string", length=255)
37
     */
38
    private $name;
39
40
    /**
41
     * @var ?int
42
     * @ORM\Column(type="integer", nullable=true)
43
     */
44
    private $year;
45
46
    /**
47
     * @var ?string
48
     * @ORM\Column(type="string", length=255, nullable=true)
49
     */
50
    private $format;
51
52
    /**
53
     * @var ?string
54
     * @ORM\Column(type="string", length=255, nullable=true)
55
     */
56
    private $author;
57
58
    /**
59
     * @var ?string
60
     * @ORM\Column(type="string", length=255, nullable=true)
61
     */
62
    private $publisher;
63
64
    /**
65
     * @var ?string
66
     * @ORM\Column(type="string", length=255, nullable=true)
67
     */
68
    private $description;
69
70
    /**
71
     * @var ?string
72
     * @ORM\Column(type="string", length=255, nullable=true)
73
     */
74
    private $store;
75
76
    /**
77
     * @var ?string
78
     * @ORM\Column(type="string", length=255, nullable=true)
79
     */
80
    private $url;
81
82
    /**
83
     * @Gedmo\Slug(fields={"name"})
84
     * @ORM\Column(length=128, unique=true)
85
     */
86
    private $slug;
87
88
    /**
89
     * @var ?\DateTime
90
     * @ORM\Column(type="datetime", nullable=true)
91
     */
92
    private $deletedAt;
0 ignored issues
show
introduced by
The private property $deletedAt is not used, and could be removed.
Loading history...
93
94
    /**
95
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="items")
96
     */
97
    private $category;
98
99
    /** 
100
     * @ORM\OneToMany(targetEntity="App\Entity\ItemCollection", mappedBy="item", orphanRemoval=true, cascade={"persist", "remove"}) 
101
     */
102
    protected $collections;
103
104
    /** 
105
     * @ORM\OneToOne(targetEntity="App\Entity\Loan", mappedBy="item") 
106
     */
107
    protected $loaned;
108
109
    /**
110
     * @ORM\ManyToOne(targetEntity="User", inversedBy="items")
111
     */
112
    private $user;
113
    
114
    /**
115
     * @param UuidInterface $id
116
     * @param string $name
117
     * @param Category $category
118
     * @param ?int $year
119
     * @param ?string $format
120
     * @param ?string $author
121
     * @param ?string $publisher
122
     * @param ?string $description
123
     * @param ?string $store
124
     * @param ?string $url
125
     * @param User $user
126
     * @throws \Assert\AssertionFailedException
127
     */
128 19
    public function __construct(
129
        UuidInterface $id,
130
        string $name,
131
        Category $category,
132
        ?int $year,
133
        ?string $format,
134
        ?string $author,
135
        ?string $publisher,
136
        ?string $description,
137
        ?string $store,
138
        ?string $url,
139
        User $user
140
        )
141
    {
142 19
        $this->setId($id);
143 19
        $this->setName($name);
144 19
        $this->setCategory($category);
145 19
        $this->setYear($year);
146 19
        $this->setFormat($format);
147 19
        $this->setAuthor($author);
148 19
        $this->setPublisher($publisher);
149 19
        $this->setDescription($description);
150 19
        $this->setStore($store);
151 19
        $this->setUrl($url);
152 19
        $this->setUser($user);
153 19
        $this->collections = new ArrayCollection();
154 19
    }
155
    
156
    /**
157
     * @param UuidInterface $id
158
     * @return Item
159
     */
160 19
    public function setId(UuidInterface $id): self
161
    {
162 19
        $this->id = $id;
163
        
164 19
        return $this;
165
    }
166
167
    /**
168
     * @return UuidInterface
169
     */
170 15
    public function getId(): UuidInterface
171
    {
172 15
        return $this->id;
173
    }
174
175
    /**
176
     * @param Category $category
177
     * @return Item
178
     */
179 19
    public function setCategory(Category $category): self
180
    {
181 19
        $this->category = $category;
182
        
183 19
        return $this;
184
    }
185
186
    /**
187
     * @return Category
188
     */
189 7
    public function getCategory(): Category
190
    {
191 7
        return $this->category;
192
    }
193
194
    /**
195
     * @param User $user
196
     * @return Item
197
     */
198 19
    public function setUser(User $user): self
199
    {
200 19
        $this->user = $user;
201
        
202 19
        return $this;
203
    }
204
205
    /**
206
     * @return User
207
     */
208
    public function getUser(): User
209
    {
210
        return $this->user;
211
    }
212
213
    /**
214
     * @return string
215
     */
216 7
    public function getName(): string
217
    {
218 7
        return $this->name;
219
    }
220
221
    /**
222
     * @param string $name
223
     * @return Item
224
     */
225 19
    public function setName(string $name): self
226
    {
227 19
        Assertion::notEmpty($name, 'Item name is required');
228 19
        Assertion::maxLength($name, 255, 'Item name must less than 255 characters');
229 19
        $this->name = $name;
230
231 19
        return $this;
232
    }
233
234
    /**
235
     * @return ?int
236
     */
237 7
    public function getYear(): ?int
238
    {
239 7
        return $this->year;
240
    }
241
242
    /**
243
     * @param ?int $year
244
     * @return Item
245
     */
246 19
    public function setYear(?int $year): self
247
    {
248 19
        $this->year = $year;
249
250 19
        return $this;
251
    }
252
253
    /**
254
     * @return ?string
255
     */
256 7
    public function getFormat(): ?string
257
    {
258 7
        return $this->format;
259
    }
260
261
    /**
262
     * @param ?string $format
263
     * @return Item
264
     */
265 19
    public function setFormat(?string $format): self
266
    {
267 19
        $this->format = $format;
268
269 19
        return $this;
270
    }
271
272
    /**
273
     * @return ?string
274
     */
275 7
    public function getAuthor(): ?string
276
    {
277 7
        return $this->author;
278
    }
279
280
    /**
281
     * @param ?string $author
282
     * @return Item
283
     */
284 19
    public function setAuthor(?string $author): self
285
    {
286 19
        $this->author = $author;
287
288 19
        return $this;
289
    }
290
291
    /**
292
     * @return ?string
293
     */
294 7
    public function getPublisher(): ?string
295
    {
296 7
        return $this->publisher;
297
    }
298
299
    /**
300
     * @param ?string $publisher
301
     * @return Item
302
     */
303 19
    public function setPublisher(?string $publisher): self
304
    {
305 19
        $this->publisher = $publisher;
306
307 19
        return $this;
308
    }
309
310
    /**
311
     * @return ?string
312
     */
313 7
    public function getDescription(): ?string
314
    {
315 7
        return $this->description;
316
    }
317
318
    /**
319
     * @param ?string $description
320
     * @return Item
321
     */
322 19
    public function setDescription(?string $description): self
323
    {
324 19
        $this->description = $description;
325
326 19
        return $this;
327
    }
328
329
    /**
330
     * @return ?string
331
     */
332 7
    public function getStore(): ?string
333
    {
334 7
        return $this->store;
335
    }
336
337
    /**
338
     * @param ?string $store
339
     * @return Item
340
     */
341 19
    public function setStore(?string $store): self
342
    {
343 19
        $this->store = $store;
344
345 19
        return $this;
346
    }
347
348
    /**
349
     * @return ?string
350
     */
351 7
    public function getUrl(): ?string
352
    {
353 7
        return $this->url;
354
    }
355
356
    /**
357
     * @param ?string $url
358
     * @return Item
359
     */
360 19
    public function setUrl(?string $url): self
361
    {
362 19
        $this->url = $url;
363
364 19
        return $this;
365
    }
366
    
367
    /**
368
     * @return ArrayCollection
369
     */
370 2
    public function getCollections()
371
    {
372 2
        return $this->collections;
373
    }
374
    
375
    /**
376
     * @return Loan[]
377
     */
378 1
    public function getLoaned()
379
    {
380 1
        return $this->loaned;
381
    }
382
383 1
    public function addCollection(ItemCollection $itemCollection): void
384
    {
385 1
        $this->collections->add($itemCollection);
386 1
    }
387
388
    /**
389
     * @param Collection $collection
390
     * @return bool
391
     */
392 2
    public function isInCollection(Collection $collection): bool
393
    {
394
        return $this->getCollections()->exists(function($key, $element) use ($collection){
395 1
            return $collection->getId()->equals($element->getCollection()->getId());
396 2
        });
397
    }
398
    
399
    /**
400
     *
401
     * @return array
402
     */
403
    public function getItemCollections(): array
404
    {
405
        $collections = [];
406
        foreach ($this->getCollections() as $itemCollection) {
407
            $collections[] = $itemCollection->getCollection();
408
        }
409
        return $collections;
410
    }
411
    
412
    /**
413
     * @return string
414
     */
415
    public function getSlug(): string
416
    {
417
        return $this->slug;
418
    }
419
420
    /**
421
     * @return array
422
     */
423
    public function jsonSerialize()
424
    {
425
        return [
426
            'id' => $this->getId()->toString(),
427
            'name' => $this->getName(),
428
            'categoryId' => $this->getCategory()->getId()->toString(),
429
            'year' => $this->getYear(),
430
            'format' => $this->getFormat(),
431
            'author' => $this->getAuthor(),
432
            'publisher' => $this->getPublisher(),
433
            'description' => $this->getDescription(),
434
            'store' => $this->getStore(),
435
            'url' => $this->getUrl(),
436
            'collections' => $this->getItemCollections(),
437
            'loaned' => $this->getLoaned(),
438
            'userId' => $this->getUser()->getId()->toString(),
439
            'slug' => $this->getSlug(),
440
        ];
441
    }
442
}
443