OffsetValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 24
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 19 5
1
<?php declare(strict_types=1);
2
3
namespace Mgid\Component\Pagination\Validator;
4
5
use Symfony\Component\Validator\Exception;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
use Mgid\Component\Pagination\InputInterface;
9
10
final class OffsetValidator extends ConstraintValidator
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function validate($input, Constraint $constraint): void
16
    {
17
        if (!$constraint instanceof Offset) {
18
            throw new Exception\UnexpectedTypeException($constraint, Offset::class); // @codeCoverageIgnore
19
        }
20
21
        if (!$input instanceof InputInterface) {
22
            throw new Exception\UnexpectedTypeException($input, InputInterface::class); // @codeCoverageIgnore
23
        }
24
25
        if ($input->getOffset() >= $constraint->min && $input->getOffset() <= $constraint->max) {
26
            return;
27
        }
28
29
        $this->context
30
            ->buildViolation($constraint->message)
31
            ->atPath('offset')
32
            ->setParameter('{{ value }}', (string) $input->getOffset())
33
            ->addViolation();
34
    }
35
}
36