GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 846394...b53815 )
by Christian
02:51
created

BatchTimeAfterValidator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 18

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldValue() 0 5 1
C validate() 0 77 17
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