Category   B
last analyzed

Complexity

Total Complexity 46

Size/Duplication

Total Lines 570
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 99
c 4
b 0
f 1
dl 0
loc 570
ccs 211
cts 211
cp 1
rs 8.72
wmc 46

42 Methods

Rating   Name   Duplication   Size   Complexity  
A getB2bGroupId() 0 3 1
A getParentIdList() 0 3 1
A getCreatedDate() 0 3 1
A getSegmentIdList() 0 3 1
A hydrate() 0 11 3
A getProducts() 0 3 1
A getEditedDate() 0 3 1
A addParentCategory() 0 7 2
A getChildrenCategories() 0 3 1
A getId() 0 3 1
A getListLayout() 0 3 1
A getDefaultParentId() 0 3 1
A getNumber() 0 3 1
A getModified() 0 3 1
A clearParentCategories() 0 5 1
A getCustomListLayout() 0 3 1
A getInfoLayout() 0 3 1
A setSegments() 0 5 1
A getCustomInfoLayout() 0 3 1
A setId() 0 5 1
A __construct() 0 6 1
A getSegments() 0 3 1
A removeParentCategory() 0 5 1
A getParentCategories() 0 3 1
A getExternalId() 0 3 1
A hasParentCategory() 0 8 2
A setExternalId() 0 5 1
A setListLayout() 0 5 1
A setCreatedDate() 0 5 1
A setEditedDate() 0 5 1
A setParentCategories() 0 5 1
A setInfoLayout() 0 5 1
A setCustomInfoLayout() 0 5 1
A setB2bGroupId() 0 5 1
A setParentIdList() 0 5 1
A setProducts() 0 5 1
A setCustomListLayout() 0 5 1
A setDefaultParentId() 0 5 1
A setModified() 0 5 1
A setNumber() 0 5 1
A setChildrenCategories() 0 5 1
A setSegmentIdList() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like Category often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Category, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Loevgaard\DandomainFoundation\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletable;
8
use Knp\DoctrineBehaviors\Model\Timestampable\Timestampable;
9
use Knp\DoctrineBehaviors\Model\Translatable\Translatable;
10
use Loevgaard\DandomainFoundation\Entity\Generated\CategoryInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...rated\CategoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Loevgaard\DandomainFoundation\Entity\Generated\CategoryTrait;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...Generated\CategoryTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Loevgaard\DandomainFoundation\Entity\Generated\CategoryTranslationInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...oryTranslationInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
/**
15
 * @ORM\Entity()
16
 * @ORM\Table(name="ldf_categories")
17
 *
18
 * @method CategoryTranslationInterface translate(string $locale = null, bool $fallbackToDefault = true)
19
 */
20
class Category extends AbstractEntity implements CategoryInterface
21
{
22
    use CategoryTrait;
23
    use Timestampable;
24
    use SoftDeletable;
25
    use Translatable;
26
27
    protected $hydrateConversions = [
28
        'internalId' => 'externalId',
29
    ];
30
31
    /**
32
     * @var int
33
     *
34
     * @ORM\Id
35
     * @ORM\GeneratedValue
36
     * @ORM\Column(type="integer")
37
     **/
38
    protected $id;
39
40
    /**
41
     * This is the internal id in the API.
42
     *
43
     * @var int
44
     *
45
     * @ORM\Column(type="bigint", unique=true)
46
     */
47
    protected $externalId;
48
49
    /**
50
     * @var string
51
     *
52
     * @ORM\Column(type="string", unique=true, length=191)
53
     */
54
    protected $number;
55
56
    /**
57
     * @var string|null
58
     *
59
     * @ORM\Column(nullable=true, type="string", length=191)
60
     */
61
    protected $b2bGroupId;
62
63
    /**
64
     * @var \DateTimeImmutable|null
65
     *
66
     * @ORM\Column(nullable=true, type="datetime_immutable")
67
     */
68
    protected $createdDate;
69
70
    /**
71
     * @var int|null
72
     *
73
     * @ORM\Column(nullable=true, type="integer")
74
     */
75
    protected $customInfoLayout;
76
77
    /**
78
     * @var int|null
79
     *
80
     * @ORM\Column(nullable=true, type="integer")
81
     */
82
    protected $customListLayout;
83
84
    /**
85
     * @var int|null
86
     *
87
     * @ORM\Column(nullable=true, type="integer")
88
     */
89
    protected $defaultParentId;
90
91
    /**
92
     * @var \DateTimeImmutable|null
93
     *
94
     * @ORM\Column(nullable=true, type="datetime_immutable")
95
     */
96
    protected $editedDate;
97
98
    /**
99
     * @var int|null
100
     *
101
     * @ORM\Column(nullable=true, type="integer")
102
     */
103
    protected $infoLayout;
104
105
    /**
106
     * @var int|null
107
     *
108
     * @ORM\Column(nullable=true, type="integer")
109
     */
110
    protected $listLayout;
111
112
    /**
113
     * @var bool|null
114
     *
115
     * @ORM\Column(nullable=true, type="boolean")
116
     */
117
    protected $modified;
118
119
    /**
120
     * The parent 'id' really refers to the parent number.
121
     *
122
     * @var array|null
123
     *
124
     * @ORM\Column(nullable=true, type="json")
125
     */
126
    protected $parentIdList;
127
128
    /**
129
     * @var array|null
130
     *
131
     * @ORM\Column(nullable=true, type="json")
132
     */
133
    protected $segmentIdList;
134
135
    /**
136
     * @var Category[]|ArrayCollection
137
     *
138
     * @ORM\ManyToMany(mappedBy="parentCategories", targetEntity="Category")
139
     */
140
    protected $childrenCategories;
141
142
    /**
143
     * @var Category[]|ArrayCollection
144
     *
145
     * @ORM\JoinTable(name="ldf_category_parents")
146
     * @ORM\ManyToMany(inversedBy="childrenCategories", targetEntity="Category")
147
     */
148
    protected $parentCategories;
149
150
    /**
151
     * @var Product[]|ArrayCollection
152
     *
153
     * @ORM\ManyToMany(mappedBy="categories", targetEntity="Product", fetch="EXTRA_LAZY")
154
     */
155
    protected $products;
156
157
    /**
158
     * @var Segment[]|ArrayCollection
159
     *
160
     * @ORM\JoinTable(name="ldf_category_segments")
161
     * @ORM\ManyToMany(cascade={"persist"}, inversedBy="categories", targetEntity="Segment")
162
     */
163
    protected $segments;
164
165 24
    public function __construct()
166
    {
167 24
        $this->childrenCategories = new ArrayCollection();
168 24
        $this->parentCategories = new ArrayCollection();
169 24
        $this->products = new ArrayCollection();
170 24
        $this->segments = new ArrayCollection();
171 24
    }
172
173 3
    public function hydrate(array $data, bool $useConversions = false, $scalarsOnly = true)
174
    {
175 3
        if (isset($data['createdDate'])) {
176 3
            $data['createdDate'] = $this->getDateTimeFromJson($data['createdDate']);
177
        }
178
179 3
        if (isset($data['editedDate'])) {
180 3
            $data['editedDate'] = $this->getDateTimeFromJson($data['editedDate']);
181
        }
182
183 3
        parent::hydrate($data, $useConversions, $scalarsOnly);
184 3
    }
185
186
    /*
187
     * Collection methods
188
     */
189 15
    public function addParentCategory(CategoryInterface $category): CategoryInterface
190
    {
191 15
        if (!$this->hasParentCategory($category)) {
192 15
            $this->parentCategories->add($category);
193
        }
194
195 15
        return $this;
196
    }
197
198
    /**
199
     * @param CategoryInterface|int $category
200
     *
201 10
     * @return bool
202
     */
203 15
    public function hasParentCategory($category): bool
204 10
    {
205 5
        if ($category instanceof CategoryInterface) {
206 5
            $category = $category->getExternalId();
207 10
        }
208 6
209 15
        return $this->parentCategories->exists(function ($key, CategoryInterface $element) use ($category) {
210 3
            return $element->getExternalId() === $category;
211 5
        });
212 2
    }
213
214 3
    public function removeParentCategory(CategoryInterface $category): CategoryInterface
215
    {
216 3
        $this->parentCategories->removeElement($category);
217
218 1
        return $this;
219 2
    }
220
221 3
    public function clearParentCategories(): CategoryInterface
222
    {
223 3
        $this->parentCategories->clear();
224
225 1
        return $this;
226
    }
227
228
    /*
229
     * Getters / Setters
230 4
     */
231
232 4
    /**
233
     * @return int
234
     */
235 2
    public function getId(): int
236
    {
237 2
        return (int) $this->id;
238 2
    }
239
240 2
    /**
241
     * @param int $id
242 2
     *
243
     * @return CategoryInterface
244
     */
245 15
    public function setId(int $id)
246
    {
247 15
        $this->id = $id;
248
249 1
        return $this;
250
    }
251
252
    /**
253 4
     * @return int
254
     */
255 11
    public function getExternalId(): int
256
    {
257 11
        return (int) $this->externalId;
258
    }
259
260 2
    /**
261
     * @param int $externalId
262 2
     *
263
     * @return CategoryInterface
264
     */
265 2
    public function setExternalId(int $externalId)
266
    {
267 2
        $this->externalId = $externalId;
268 2
269 2
        return $this;
270 2
    }
271
272 2
    /**
273
     * @return string
274
     */
275 1
    public function getNumber(): string
276
    {
277 1
        return (string) $this->number;
278 2
    }
279
280 2
    /**
281
     * @param string $number
282
     *
283
     * @return CategoryInterface
284
     */
285 1
    public function setNumber(string $number)
286
    {
287 1
        $this->number = $number;
288 2
289 1
        return $this;
290 2
    }
291
292 2
    /**
293
     * @return null|string
294
     */
295 1
    public function getB2bGroupId()
296
    {
297 1
        return $this->b2bGroupId;
298 2
    }
299
300 2
    /**
301
     * @param null|string $b2bGroupId
302
     *
303
     * @return CategoryInterface
304
     */
305 1
    public function setB2bGroupId($b2bGroupId)
306
    {
307 1
        $this->b2bGroupId = $b2bGroupId;
308 2
309 1
        return $this;
310 2
    }
311
312 2
    /**
313
     * @return \DateTimeImmutable|null
314
     */
315 1
    public function getCreatedDate()
316
    {
317 1
        return $this->createdDate;
318 2
    }
319
320 2
    /**
321
     * @param \DateTimeImmutable|null $createdDate
322
     *
323
     * @return CategoryInterface
324
     */
325 1
    public function setCreatedDate($createdDate)
326
    {
327 1
        $this->createdDate = $createdDate;
328 2
329 1
        return $this;
330 2
    }
331
332 2
    /**
333
     * @return int|null
334
     */
335 1
    public function getCustomInfoLayout()
336
    {
337 1
        return $this->customInfoLayout;
338 2
    }
339
340 2
    /**
341
     * @param int|null $customInfoLayout
342
     *
343
     * @return CategoryInterface
344
     */
345 1
    public function setCustomInfoLayout($customInfoLayout)
346
    {
347 1
        $this->customInfoLayout = $customInfoLayout;
348 2
349 1
        return $this;
350 2
    }
351
352 2
    /**
353
     * @return int|null
354
     */
355 1
    public function getCustomListLayout()
356
    {
357 1
        return $this->customListLayout;
358 2
    }
359
360 2
    /**
361
     * @param int|null $customListLayout
362
     *
363
     * @return CategoryInterface
364
     */
365 1
    public function setCustomListLayout($customListLayout)
366
    {
367 1
        $this->customListLayout = $customListLayout;
368 2
369 1
        return $this;
370 2
    }
371
372 2
    /**
373
     * @return int|null
374
     */
375 1
    public function getDefaultParentId()
376
    {
377 1
        return $this->defaultParentId;
378 2
    }
379
380 2
    /**
381
     * @param int|null $defaultParentId
382
     *
383
     * @return CategoryInterface
384
     */
385 1
    public function setDefaultParentId($defaultParentId)
386
    {
387 1
        $this->defaultParentId = $defaultParentId;
388 2
389 1
        return $this;
390 2
    }
391
392 2
    /**
393
     * @return \DateTimeImmutable|null
394
     */
395 1
    public function getEditedDate()
396
    {
397 1
        return $this->editedDate;
398 2
    }
399
400 2
    /**
401
     * @param \DateTimeImmutable|null $editedDate
402
     *
403
     * @return CategoryInterface
404
     */
405 1
    public function setEditedDate($editedDate)
406
    {
407 1
        $this->editedDate = $editedDate;
408 2
409 1
        return $this;
410 2
    }
411
412 2
    /**
413
     * @return int|null
414
     */
415 1
    public function getInfoLayout()
416
    {
417 1
        return $this->infoLayout;
418 2
    }
419
420 2
    /**
421
     * @param int|null $infoLayout
422
     *
423
     * @return CategoryInterface
424
     */
425 1
    public function setInfoLayout($infoLayout)
426
    {
427 1
        $this->infoLayout = $infoLayout;
428 2
429 1
        return $this;
430 2
    }
431
432 2
    /**
433
     * @return int|null
434
     */
435 1
    public function getListLayout()
436
    {
437 1
        return $this->listLayout;
438 2
    }
439
440 2
    /**
441
     * @param int|null $listLayout
442
     *
443
     * @return CategoryInterface
444
     */
445 1
    public function setListLayout($listLayout)
446
    {
447 1
        $this->listLayout = $listLayout;
448 2
449 1
        return $this;
450 2
    }
451
452 2
    /**
453
     * @return bool|null
454
     */
455 1
    public function getModified()
456
    {
457 1
        return $this->modified;
458 2
    }
459
460 2
    /**
461
     * @param bool|null $modified
462
     *
463
     * @return CategoryInterface
464
     */
465 1
    public function setModified($modified)
466
    {
467 1
        $this->modified = $modified;
468 2
469 1
        return $this;
470 2
    }
471
472 2
    /**
473
     * @return array|null
474
     */
475 1
    public function getParentIdList()
476
    {
477 1
        return $this->parentIdList;
478 2
    }
479
480 2
    /**
481
     * @param array|null $parentIdList
482
     *
483
     * @return CategoryInterface
484
     */
485 1
    public function setParentIdList($parentIdList)
486
    {
487 1
        $this->parentIdList = $parentIdList;
488 2
489 1
        return $this;
490 2
    }
491
492 2
    /**
493
     * @return array|null
494
     */
495 1
    public function getSegmentIdList()
496
    {
497 1
        return $this->segmentIdList;
498 2
    }
499
500 2
    /**
501
     * @param array|null $segmentIdList
502
     *
503
     * @return CategoryInterface
504
     */
505 1
    public function setSegmentIdList($segmentIdList)
506
    {
507 1
        $this->segmentIdList = $segmentIdList;
508 2
509 1
        return $this;
510 2
    }
511
512 2
    /**
513
     * @return ArrayCollection|Category[]
514
     */
515 1
    public function getChildrenCategories()
516
    {
517 1
        return $this->childrenCategories;
518 8
    }
519
520 8
    /**
521
     * @param ArrayCollection|Category[] $childrenCategories
522
     *
523
     * @return CategoryInterface
524
     */
525 1
    public function setChildrenCategories($childrenCategories)
526
    {
527 1
        $this->childrenCategories = $childrenCategories;
528 2
529 1
        return $this;
530 2
    }
531
532 2
    /**
533
     * @return ArrayCollection|Category[]
534
     */
535 4
    public function getParentCategories()
536
    {
537 4
        return $this->parentCategories;
538 2
    }
539
540 2
    /**
541
     * @param ArrayCollection|Category[] $parentCategories
542
     *
543
     * @return CategoryInterface
544
     */
545 1
    public function setParentCategories($parentCategories)
546
    {
547 1
        $this->parentCategories = $parentCategories;
548 2
549 1
        return $this;
550 2
    }
551
552 2
    /**
553
     * @return Product[]|ArrayCollection
554
     */
555 1
    public function getProducts()
556
    {
557 1
        return $this->products;
558 2
    }
559
560 2
    /**
561
     * @param Product[] $products
562
     *
563
     * @return CategoryInterface
564
     */
565 1
    public function setProducts($products)
566
    {
567 1
        $this->products = $products;
568 2
569 1
        return $this;
570 2
    }
571
572 2
    /**
573
     * @return ArrayCollection|Segment[]
574
     */
575 1
    public function getSegments()
576
    {
577 1
        return $this->segments;
578
    }
579
580
    /**
581
     * @param ArrayCollection|Segment[] $segments
582
     *
583
     * @return CategoryInterface
584
     */
585 1
    public function setSegments($segments)
586
    {
587 1
        $this->segments = $segments;
588
589 1
        return $this;
590
    }
591
}
592