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

PromotionDateRangeValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 30
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 21 5
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