LengthFactory::supports()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 1
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 3
rs 10
1
<?php
2
3
namespace JBen87\ParsleyBundle\Constraint\Factory;
4
5
use JBen87\ParsleyBundle\Constraint\Constraint;
6
use JBen87\ParsleyBundle\Constraint\Constraints as ParsleyAssert;
7
use Symfony\Component\Validator\Constraint as SymfonyConstraint;
8
use Symfony\Component\Validator\Constraints as Assert;
9
10
class LengthFactory implements TranslatableFactoryInterface
11
{
12
    use FactoryTrait;
13
14
    /**
15
     * @inheritdoc
16
     */
17 2
    public function create(SymfonyConstraint $constraint): Constraint
18
    {
19
        /** @var Assert\Length $constraint */
20
21
        $options = [
22 2
            'min' => $constraint->min,
23 2
            'max' => $constraint->max,
24 2
            'message' => $this->trans(
25 2
                'This value should have {{ min }} to {{ max }} characters.',
26 2
                ['{{ min }}' => $constraint->min, '{{ max }}' => $constraint->max]
27
            ),
28
        ];
29
30 2
        if ($constraint->min === $constraint->max) {
31 1
            $options['message'] = $this->transChoice(
32 1
                $constraint->exactMessage,
33 1
                (int) $constraint->min,
34 1
                ['{{ limit }}' => $constraint->min]
35
            );
36
        }
37
38 2
        return new ParsleyAssert\Length($options);
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 1
    public function supports(SymfonyConstraint $constraint): bool
45
    {
46 1
        return $constraint instanceof Assert\Length && null !== $constraint->min && null !== $constraint->max;
47
    }
48
}
49