Passed
Push — master ( 846394...b53815 )
by Christian
02:51
created

BatchTimeAfterValidator::validate()   C

Complexity

Conditions 17
Paths 12

Size

Total Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
nc 12
nop 2
dl 0
loc 77
rs 5.2166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
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 Core23\FormExtensions\Validator\Constraints;
13
14
use Core23\FormExtensions\Form\Model\BatchTime;
15
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
16
use Symfony\Component\PropertyAccess\PropertyAccess;
17
use Symfony\Component\Validator\Constraint;
18
use Symfony\Component\Validator\ConstraintValidator;
19
use Symfony\Component\Validator\Exception\InvalidArgumentException;
20
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
21
22
final class BatchTimeAfterValidator extends ConstraintValidator
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function validate($value, Constraint $constraint): void
28
    {
29
        if (!$constraint instanceof BatchTimeAfter) {
30
            throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\BatchTimeAfter');
31
        }
32
33
        if (!is_object($value)) {
34
            return;
35
        }
36
37
        try {
38
            $firstDate = $this->getFieldValue($value, $constraint->firstField);
39
        } catch (NoSuchPropertyException $e) {
40
            throw new InvalidArgumentException($e->getMessage());
41
        }
42
43
        try {
44
            $secondDate = $this->getFieldValue($value, $constraint->secondField);
45
        } catch (NoSuchPropertyException $e) {
46
            throw new InvalidArgumentException($e->getMessage());
47
        }
48
49
        if (!$constraint->required && !$firstDate && !$secondDate) {
50
            return;
51
        }
52
53
        if (!$firstDate && $secondDate) {
54
            $this->context
55
                ->buildViolation($constraint->emptyMessage)
56
                ->setParameter('%emptyField%', $constraint->firstField)
57
                ->setParameter('%field%', $constraint->secondField)
58
                ->atPath($constraint->firstField)
59
                ->addViolation();
60
61
            return;
62
        } elseif ($firstDate && !$secondDate) {
63
            $this->context
64
                ->buildViolation($constraint->emptyMessage)
65
                ->setParameter('%emptyField%', $constraint->secondField)
66
                ->setParameter('%field%', $constraint->firstField)
67
                ->atPath($constraint->secondField)
68
                ->addViolation();
69
70
            return;
71
        }
72
73
        if (!$firstDate instanceof BatchTime || !$secondDate instanceof BatchTime) {
74
            throw new UnexpectedTypeException($value, BatchTime::class);
75
        }
76
77
        if (!$firstDate->getTime()) {
78
            $this->context
79
                ->buildViolation($constraint->emptyMessage)
80
                ->setParameter('%emptyField%', $constraint->firstField)
81
                ->setParameter('%field%', $constraint->secondField)
82
                ->atPath($constraint->firstField)
83
                ->addViolation();
84
85
            return;
86
        } elseif (!$secondDate->getTime()) {
87
            $this->context
88
                ->buildViolation($constraint->emptyMessage)
89
                ->setParameter('%emptyField%', $constraint->secondField)
90
                ->setParameter('%field%', $constraint->firstField)
91
                ->atPath($constraint->secondField)
92
                ->addViolation();
93
94
            return;
95
        }
96
97
        if ($firstDate->getSeconds() > $secondDate->getSeconds()) {
98
            $this->context
99
                ->buildViolation($constraint->message)
100
                ->setParameter('%firstField%', $constraint->firstField)
101
                ->setParameter('%secondField%', $constraint->secondField)
102
                ->atPath($constraint->secondField)
103
                ->addViolation();
104
        }
105
    }
106
107
    /**
108
     * @param mixed  $object
109
     * @param string $field
110
     *
111
     * @return mixed
112
     */
113
    private function getFieldValue($object, string $field)
114
    {
115
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
116
117
        return $propertyAccessor->getValue($object, $field);
118
    }
119
}
120