Test Failed
Push — develop ( d382d0...28e0cd )
by Daniel
10:59
created

SortableValidator::validate()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 8
nop 2
dl 0
loc 28
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Silverback\ApiComponentBundle\Validator\Constraints;
6
7
use Silverback\ApiComponentBundle\Entity\SortableInterface;
8
use Silverback\ApiComponentBundle\Repository\ComponentLocationRepository;
9
use Symfony\Component\Validator\Constraint;
10
use Symfony\Component\Validator\ConstraintValidator;
11
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
12
use Silverback\ApiComponentBundle\Entity\Component\ComponentLocation as ComponentLocationEntity;
13
14
/**
15
 * THIS WAS CREATED TO SORT ABSTRACT PAGES - IT DOES NOT DO THAT AND APPEARS REDUNDANT - NEEDS CHECKING AND REMOVING
16
 * Class SortableValidator
17
 * @package Silverback\ApiComponentBundle\Validator\Constraints
18
 */
19
class SortableValidator extends ConstraintValidator
20
{
21
    private $componentLocationRepository;
22
23
    public function __construct(ComponentLocationRepository $componentLocationRepository)
24
    {
25
        $this->componentLocationRepository = $componentLocationRepository;
26
    }
27
28
    public function validate($sortable, Constraint $constraint): void
29
    {
30
        if (!$constraint instanceof Sortable) {
31
            throw new UnexpectedTypeException($constraint, Sortable::class);
32
        }
33
34
        if (!$sortable instanceof SortableInterface) {
35
            throw new UnexpectedTypeException($sortable, SortableInterface::class);
36
        }
37
38
        if ($sortable->getSort() === null) {
39
            $collection = $sortable->getSortCollection();
40
            if (
41
                $collection === null &&
42
                $sortable instanceof ComponentLocationEntity &&
43
                ($dynamicPageClass = $sortable->getDynamicPageClass())
44
            ) {
45
                $collection = $this->componentLocationRepository->findBy([
46
                    'dynamicPageClass' => $dynamicPageClass
47
                ]);
48
            }
49
            $sortable->setSort($sortable->calculateSort(true, $collection));
0 ignored issues
show
Bug introduced by
It seems like $collection can also be of type Silverback\ApiComponentB...ent\ComponentLocation[]; however, parameter $sortCollection of Silverback\ApiComponentB...erface::calculateSort() does only seem to accept Doctrine\Common\Collections\Collection|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            $sortable->setSort($sortable->calculateSort(true, /** @scrutinizer ignore-type */ $collection));
Loading history...
Bug introduced by
It seems like $collection can also be of type Silverback\ApiComponentB...ent\ComponentLocation[]; however, parameter $sortCollection of Silverback\ApiComponentB...cation::calculateSort() does only seem to accept Doctrine\Common\Collections\Collection|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            $sortable->setSort($sortable->calculateSort(true, /** @scrutinizer ignore-type */ $collection));
Loading history...
50
        }
51
52
        if ($sortable->getSort() === null) {
53
            $this->context->buildViolation($constraint->message)
54
                ->atPath('sort')
55
                ->addViolation();
56
        }
57
    }
58
}
59