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
Pull Request — master (#174)
by joseph
25:27
created

EntityDataValidator::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

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