LengthFactory::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 2
rs 9.8666
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