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

ValidationException::entityExceptionMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 9
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\Exception;
4
5
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\DataTransferObjectInterface;
6
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityData;
7
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
8
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityDebugDumper;
9
use EdmondsCommerce\DoctrineStaticMeta\Exception\Traits\RelativePathTraceTrait;
10
use Symfony\Component\Validator\ConstraintViolationInterface;
11
use Symfony\Component\Validator\ConstraintViolationListInterface;
12
use ts\Reflection\ReflectionClass;
13
14
class ValidationException extends DoctrineStaticMetaException
15
{
16
    use RelativePathTraceTrait;
17
18
    protected $dataObject;
19
20
    protected $errors;
21
22
    /**
23
     * @param ConstraintViolationListInterface|ConstraintViolationInterface[] $errors
24
     * @param DataTransferObjectInterface|EntityInterface                     $dataObject
25
     * @param int                                                             $code
26
     * @param \Exception|null                                                 $previous
27
     *
28 2
     * @return ValidationException
29
     */
30
    public static function create(
31
        ConstraintViolationListInterface $errors,
32
        $dataObject,
33
        $code = 0,
34 2
        \Exception $previous = null
35 2
    ) {
36
        switch (true) {
37 2
            case $dataObject instanceof EntityInterface:
38 2
                $message = self::entityExceptionMessage($errors, $dataObject);
39 2
                break;
40
            case $dataObject instanceof DataTransferObjectInterface:
41
                $message = self::dtoExceptionMessage($errors, $dataObject);
42
                break;
43
            default:
44
                $message = 'Unexpected datObject passed to ValidationException: ' . print_r($dataObject, true);
45
        }
46 2
        $exception             = new self($message, $code, $previous);
47 2
        $exception->errors     = $errors;
48
        $exception->dataObject = $dataObject;
49 2
50
        return $exception;
51
    }
52
53 2
    private static function entityExceptionMessage(
54 2
        ConstraintViolationListInterface $errors,
55
        EntityInterface $entity
56 2
    ): string {
57
        $message =
58
            self::getErrorsSummary($errors, $entity::getDoctrineStaticMeta()->getReflectionClass()->getName(), $entity);
59
        $message .= "\n\nFull Data Object Dump:" . (new EntityDebugDumper())->dump($entity);
60
61
        return $message;
62
    }
63
64
    /**
65 2
     * @param ConstraintViolationListInterface|ConstraintViolationInterface[] $errors
66
     * @param string                                                          $className
67 2
     *
68 2
     * @param EntityData                                                      $dataObject
69
     *
70
     * @return string
71
     */
72
    private static function getErrorsSummary(
73
        ConstraintViolationListInterface $errors,
74
        string $className,
75
        EntityData $dataObject
76
    ): string {
77
        $message = "\nFound " . $errors->count() . " errors validating\n" . $className;
78
        foreach ($errors as $error) {
79
            $property = $error->getPropertyPath();
80
            $getter   = 'get' . $property;
81
            if (method_exists($dataObject, $getter)) {
82
                try {
83
                    $value = $dataObject->$getter();
84
                } catch (\TypeError $e) {
85
                    $message .= "\n\n$property has TypeError: " . $e->getMessage();
86
                    continue;
87 2
                }
88
                if (is_object($value) === true) {
89
                    $value = get_class($value);
90
                }
91
                $message .= "\n\n$property [$value]: " . $error->getMessage() . ' (code: ' . $error->getCode() . ')';
92
                continue;
93
            }
94
            $message .= "\n\n$property: " . $error->getMessage();
95
        }
96
97 1
        return $message;
98
    }
99 1
100
    private static function dtoExceptionMessage(
101
        ConstraintViolationListInterface $errors,
102 1
        DataTransferObjectInterface $dto
103
    ): string {
104 1
        return self::getErrorsSummary($errors, (new \ReflectionClass($dto))->getShortName(), $dto);
105
    }
106
107
    public function getInvalidDataObject(): object
108
    {
109
        return $this->dataObject;
110
    }
111
112
    public function getValidationErrors(): ConstraintViolationListInterface
113
    {
114
        return $this->errors;
115
    }
116
}
117