LengthFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 37
ccs 14
cts 14
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 22 2
A supports() 0 3 3
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