AbstractValidator::getValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\Validation\Validators;
4
5
use ByTIC\MediaLibrary\Collections\Collection;
6
use ByTIC\MediaLibrary\Exceptions\UnexpectedTypeException;
7
use ByTIC\MediaLibrary\Media\Media;
8
use ByTIC\MediaLibrary\Validation\Constraints\AbstractConstraint;
9
use ByTIC\MediaLibrary\Validation\Constraints\ConstraintInterface;
10
use ByTIC\MediaLibrary\Validation\Traits\HasValidatorTrait;
11
use ByTIC\MediaLibrary\Validation\Violations\Violation;
12
use ByTIC\MediaLibrary\Validation\Violations\ViolationsBag;
13
use Nip\Utility\Traits\NameWorksTrait;
14
15
/**
16
 * Class ImageValidator.
17
 */
18
abstract class AbstractValidator implements ValidatorInterface
19
{
20 1
    use NameWorksTrait;
21
22
    /**
23
     * @var mixed
24
     */
25
    protected $value;
26
27
    /**
28
     * @var mixed
29
     */
30
    protected $constraint;
31
32
    /**
33
     * @var Collection
34
     */
35
    protected $collection;
36
37
    /**
38
     * @var ViolationsBag
39
     */
40
    protected $violations;
41
42
    /**
43
     * @param $value
44
     * @param $constraint
45
     *
46
     * @return ViolationsBag
47
     */
48 2
    public function validate($value, ConstraintInterface $constraint = null)
49
    {
50 2
        $constraint = $constraint ? $constraint : $this->getCollection()->getConstraint();
51 2
        $this->setValue($value);
52 2
        $this->setConstraint($constraint);
53 2
        $this->isValidConstraint();
54
55 2
        $this->violations = new ViolationsBag();
56
57 2
        if ($this->contraintNeedsValidation()) {
58 2
            $this->doValidation();
59
        }
60
61 2
        return $this->violations;
62
    }
63
64
    /**
65
     * @return Collection
66
     */
67
    public function getCollection(): Collection
68
    {
69
        return $this->collection;
70
    }
71
72
    /**
73
     * @param Collection|HasValidatorTrait $collection
74
     */
75 6
    public function setCollection(Collection $collection)
76
    {
77 6
        $this->collection = $collection;
78 6
    }
79
80 2
    protected function isValidConstraint()
81
    {
82 2
        $className = $this->getConstraintClassName();
83 2
        $constraint = $this->getConstraint();
84
85 2
        if (!$constraint instanceof $className) {
86
            throw new UnexpectedTypeException($constraint, $className);
0 ignored issues
show
Bug introduced by
$className of type string is incompatible with the type integer expected by parameter $expectedType of ByTIC\MediaLibrary\Excep...xception::__construct(). ( Ignorable by Annotation )

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

86
            throw new UnexpectedTypeException($constraint, /** @scrutinizer ignore-type */ $className);
Loading history...
87
        }
88 2
    }
89
90
    /**
91
     * @return mixed
92
     */
93 3
    public function getConstraintClassName()
94
    {
95 3
        $className = $this->getClassName();
96 3
        $firstName = $this->getClassFirstName();
97 3
        $contraintFirstName = str_replace('Validator', 'Constraint', $firstName);
98
99 3
        return str_replace('\Validators\\' . $firstName, '\Constraints\\' . $contraintFirstName, $className);
100
    }
101
102
    /**
103
     * @return mixed
104
     */
105 2
    public function getConstraint()
106
    {
107 2
        return $this->constraint;
108
    }
109
110
    /**
111
     * @param mixed $constraint
112
     */
113 2
    public function setConstraint($constraint)
114
    {
115 2
        $this->constraint = $constraint;
116 2
    }
117
118
    /**
119
     * @return bool
120
     */
121
    abstract protected function contraintNeedsValidation(): bool;
122
123
    /**
124
     * @return void
125
     */
126
    abstract protected function doValidation();
127
128
    /**
129
     * @return Media
130
     */
131
    public function getValue()
132
    {
133
        return $this->value;
134
    }
135
136
    /**
137
     * @param mixed $value
138
     */
139 2
    public function setValue($value)
140
    {
141 2
        $this->value = $value;
142 2
    }
143
144
    /**
145
     * @param AbstractConstraint $constraint
146
     * @param $code
147
     * @param $parameters
148
     */
149
    protected function addViolation($constraint, $code, $parameters)
150
    {
151
        $violation = new Violation();
152
        $violation->setCode($code);
153
        $violation->setMessage($constraint->getErrorMessage($code));
154
        $violation->setParameters($parameters);
155
156
        $this->violations->add(
157
            $violation
158
        );
159
    }
160
}
161