Completed
Push — master ( d898e2...4cc160 )
by Kamil
19:55
created

PromotionDateRangeValidator::validate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
rs 8.7624
cc 5
eloc 12
nc 4
nop 2
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\Model\PromotionInterface;
15
use Symfony\Component\Validator\Constraint;
16
use Symfony\Component\Validator\ConstraintValidator;
17
use Webmozart\Assert\Assert;
18
19
/**
20
 * @author Łukasz Chruściel <[email protected]>
21
 */
22
class PromotionDateRangeValidator extends ConstraintValidator
23
{
24
    /**
25
     * {@inheritdoc}
26
     *
27
     * @param mixed $value
28
     * @param Constraint $constraint
29
     */
30
    public function validate($value, Constraint $constraint)
31
    {
32
        if (null === $value) {
33
            return;
34
        }
35
36
        Assert::isInstanceOf(
37
            $value,
38
            PromotionInterface::class
39
        );
40
41
        if (null === $value->getStartsAt() || null === $value->getEndsAt()) {
42
            return;
43
        }
44
45
        if ($value->getStartsAt()->getTimestamp() > $value->getEndsAt()->getTimestamp()) {
46
            $this->context->buildViolation($constraint->message)
47
                ->atPath('endsAt')
48
                ->addViolation();
49
        }
50
    }
51
}
52