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.
Completed
Push — master ( a48bd8...83d092 )
by joseph
16:18 queued 18s
created

EntityDataValidator::getErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Validation;
4
5
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\DataTransferObjectInterface;
6
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
7
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\Validation\EntityDataValidatorInterface;
8
use EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException;
9
use Symfony\Component\Validator\ConstraintViolationListInterface;
10
use Symfony\Component\Validator\Validator\ValidatorInterface;
11
12
class EntityDataValidator implements EntityDataValidatorInterface
13
{
14
    /**
15
     * @var ValidatorInterface
16
     */
17
    protected $validator;
18
19
    /**
20
     * @var EntityInterface|DataTransferObjectInterface
21
     */
22
    protected $dataObject;
23
24
    public function __construct(ValidatorInterface $validator)
25
    {
26
        $this->validator = $validator;
27
    }
28
29
    /**
30
     * Set an Entity to be validated
31
     *
32
     * @param EntityInterface $entity
33
     *
34
     * @return EntityDataValidatorInterface
35
     */
36
    public function setEntity(EntityInterface $entity): EntityDataValidatorInterface
37
    {
38
        $this->dataObject = $entity;
39
40
        return $this;
41
    }
42
43
    /**
44
     * Set an data Transfer Object to be validated
45
     *
46
     * @param DataTransferObjectInterface $dto
47
     *
48
     * @return EntityDataValidatorInterface
49
     */
50
    public function setDto(DataTransferObjectInterface $dto): EntityDataValidatorInterface
51
    {
52
        $this->dataObject = $dto;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Simply check for validity as a boolean, with no error reporting
59
     *
60
     * @return bool
61
     */
62
    public function isValid(): bool
63
    {
64
        return $this->getErrors()->count() === 0;
65
    }
66
67
    /**
68
     * Perform validation and return any error that are present
69
     *
70
     * @return ConstraintViolationListInterface
71
     */
72
    public function getErrors(): ConstraintViolationListInterface
73
    {
74
        return $this->validator->validate($this->dataObject);
75
    }
76
77
    /**
78
     * Perform validation and then return a message that details the number and the details of the errors
79
     *
80
     * Will return an empty string if there are no errors
81
     *
82
     * @return string
83
     */
84
    public function getErrorsAsString(): string
85
    {
86
        $errors = $this->getErrors();
87
        if (0 === $errors->count()) {
88
            return '';
89
        }
90
        $message = 'found ' . $errors->count() . ' errors validating '
91
                   . \get_class($this->dataObject);
92
        foreach ($errors as $error) {
93
            $message .= "\n\n" . $error->getPropertyPath() . ': ' . $error->getMessage();
94
        }
95
96
        return $message;
97
    }
98
99
    /**
100
     * Validate the whole entity and provide a verbose error report
101
     *
102
     * @throws ValidationException
103
     */
104
    public function validate(): void
105
    {
106
        $errors = $this->getErrors();
107
        $this->throwExceptionIfErrors($errors);
108
    }
109
110
    /**
111
     * @param ConstraintViolationListInterface $errors
112
     *
113
     * @throws ValidationException
114
     */
115
    private function throwExceptionIfErrors(ConstraintViolationListInterface $errors): void
116
    {
117
        if (0 === $errors->count()) {
118
            return;
119
        }
120
        throw new ValidationException($errors, $this->dataObject);
121
    }
122
123
    /**
124
     * Validate a single entity property
125
     *
126
     * @param string $propertyName
127
     *
128
     * @throws ValidationException
129
     */
130
    public function validateProperty(string $propertyName): void
131
    {
132
        $errors = $this->validator->validateProperty($this->dataObject, $propertyName);
133
        $this->throwExceptionIfErrors($errors);
134
    }
135
}
136