DateRangeValidator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 23.26 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 20
loc 86
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addViolation() 0 15 3
A formatDate() 0 12 1
A processDate() 0 4 1
C validate() 20 36 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Created by Michaël Perrin
4
 *
5
 * Source: http://www.michaelperrin.fr/2013/03/19/range-date-validator-for-symfony2/
6
 *
7
 * Modificaciones por Luis Ramón López López
8
 */
9
10
namespace AppBundle\Validator\Constraints;
11
12
use Symfony\Component\Process\Exception\LogicException;
13
use Symfony\Component\Validator\Constraint;
14
use Symfony\Component\Validator\ConstraintValidator;
15
use Symfony\Component\Validator\Context\ExecutionContext;
16
17
class DateRangeValidator extends ConstraintValidator
18
{
19
20
    /**
21
     * @param string $message Violation message
22
     * @param array $parameters Message parameters
23
     */
24
    protected function addViolation($message, $parameters)
25
    {
26
        if ($this->context instanceof ExecutionContext) {
27
            $context = $this->context->buildViolation($message);
28
29
            foreach ($parameters as $name => $value) {
30
                $context = $context->setParameter($name, $value);
31
            };
32
33
            $context->addViolation();
34
        }
35
        else {
36
            throw new LogicException('Context API not 2.5 compatible');
37
        }
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function validate($value, Constraint $constraint)
44
    {
45
        if (null === $value) {
46
            return;
47
        }
48
49
        if (!($value instanceof \DateTime)) {
50
            $this->addViolation($constraint->invalidMessage,
51
                array('{{ value }}' => $value));
52
53
            return;
54
        }
55
56
        // evaluate now
57 View Code Duplication
        if (null !== $constraint->min) {
58
            $dateMin = new \DateTime($constraint->min);
59
60
            if ($value < $dateMin) {
61
                $this->addViolation($constraint->minMessage, array(
62
                    '{{ value }}' => $this->formatDate($value),
63
                    '{{ limit }}' => $this->formatDate($dateMin)
64
                ));
65
            }
66
        }
67
68 View Code Duplication
        if (null !== $constraint->max) {
69
            $dateMax = new \DateTime($constraint->max);
70
71
            if ($value > $dateMax) {
72
                $this->addViolation($constraint->maxMessage, array(
73
                    '{{ value }}' => $this->formatDate($value),
74
                    '{{ limit }}' => $this->formatDate($dateMax)
75
                ));
76
            }
77
        }
78
    }
79
80
    protected function formatDate($date)
81
    {
82
        $formatter = new \IntlDateFormatter(
83
            null,
84
            \IntlDateFormatter::SHORT,
85
            \IntlDateFormatter::NONE,
86
            date_default_timezone_get(),
87
            \IntlDateFormatter::GREGORIAN
88
        );
89
90
        return $this->processDate($formatter, $date);
91
    }
92
93
    /**
94
     * @param  \IntlDateFormatter $formatter
95
     * @param  \Datetime          $date
96
     * @return string
97
     */
98
    protected function processDate(\IntlDateFormatter $formatter, \Datetime $date)
99
    {
100
        return $formatter->format((int) $date->format('U'));
101
    }
102
}
103