Completed
Push — master ( 99e244...bf2eb3 )
by Paweł
204:07 queued 189:10
created

CouponGenerationAmountValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\PromotionBundle\Validator;
13
14
use Sylius\Component\Promotion\Generator\GenerationPolicyInterface;
15
use Symfony\Component\Validator\Constraint;
16
use Symfony\Component\Validator\ConstraintValidator;
17
18
/**
19
 * @author Arkadiusz Krakowiak <[email protected]>
20
 */
21
class CouponGenerationAmountValidator extends ConstraintValidator
22
{
23
    /**
24
     * @var GenerationPolicyInterface
25
     */
26
    private $generationPolicy;
27
28
    /**
29
     * @param GenerationPolicyInterface $generationPolicy
30
     */
31
    public function __construct(GenerationPolicyInterface $generationPolicy)
32
    {
33
        $this->generationPolicy = $generationPolicy;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function validate($instruction, Constraint $constraint)
40
    {
41
        if (null === $instruction->getCodeLength() || null === $instruction->getAmount()) {
42
            return;
43
        }
44
45
        if (!$this->generationPolicy->isGenerationPossible($instruction)) {
46
            $this->context->addViolation(
47
                $constraint->message,
48
                array(
49
                    '%expectedAmount%' => $instruction->getAmount(),
50
                    '%codeLength%' => $instruction->getCodeLength(),
51
                    '%possibleAmount%' => $this->generationPolicy->getPossibleGenerationAmount($instruction)
52
                )
53
            );
54
55
        }
56
    }
57
}
58