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 (#138)
by joseph
96:08 queued 65:06
created

loadPropertyValidatorMetaData()   A

Complexity

Conditions 5
Paths 14

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 20
rs 9.4888
c 0
b 0
f 0
ccs 0
cts 13
cp 0
cc 5
nc 14
nop 1
crap 30
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Traits;
4
5
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityDependencyInjector;
6
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
7
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\Validation\EntityValidatorInterface;
8
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory;
9
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
10
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData;
11
12
/**
13
 * Trait ValidatedEntityTrait
14
 *
15
 * @package EdmondsCommerce\DoctrineStaticMeta\Entity\Traits
16
 *
17
 * Implements ValidatedEntityInterface
18
 */
19
trait ValidatedEntityTrait
20
{
21
    /**
22
     * @var EntityValidatorInterface
23
     */
24
    protected $validator;
25
26
    /**
27
     * @param ValidatorClassMetaData $metadata
28
     *
29
     * @throws DoctrineStaticMetaException
30
     */
31
    public static function loadValidatorMetaData(ValidatorClassMetaData $metadata): void
32
    {
33
        static::loadPropertyValidatorMetaData($metadata);
34
    }
35
36
    /**
37
     * @param ValidatorClassMetaData $metadata
38
     *
39
     * @throws DoctrineStaticMetaException
40
     */
41
    protected static function loadPropertyValidatorMetaData(ValidatorClassMetaData $metadata): void
42
    {
43
        $methodName = '__no_method__';
44
        try {
45
            $staticMethods = self::getDoctrineStaticMeta()->getStaticMethods();
46
            //now loop through and call them
47
            foreach ($staticMethods as $method) {
48
                $methodName = $method->getName();
49
                if ($methodName === EntityInterface::METHOD_PREFIX_GET_PROPERTY_VALIDATOR_META) {
50
                    continue;
51
                }
52
                if (0 === stripos($methodName, EntityInterface::METHOD_PREFIX_GET_PROPERTY_VALIDATOR_META)) {
53
                    static::$methodName($metadata);
54
                }
55
            }
56
        } catch (\Exception $e) {
57
            throw new DoctrineStaticMetaException(
58
                'Exception in ' . __METHOD__ . 'for '
59
                . self::class . "::$methodName\n\n"
60
                . $e->getMessage()
61
            );
62
        }
63
    }
64
65
    /**
66
     * Called as part of the Entity Dependency Injection in the Entity Factory
67
     *
68
     * @see EntityDependencyInjector
69
     *
70
     * @param EntityValidatorFactory $factory
71
     */
72
    public function injectValidator(EntityValidatorFactory $factory): void
73
    {
74
        $this->validator = $factory->getEntityValidator();
75
        $this->validator->setEntity($this);
0 ignored issues
show
Bug introduced by
$this of type EdmondsCommerce\Doctrine...ts\ValidatedEntityTrait is incompatible with the type EdmondsCommerce\Doctrine...erfaces\EntityInterface expected by parameter $entity of EdmondsCommerce\Doctrine...yValidator::setEntity(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        $this->validator->setEntity(/** @scrutinizer ignore-type */ $this);
Loading history...
76
    }
77
78
    /**
79
     * Is the current Entity valid
80
     *
81
     * @return bool
82
     */
83
    public function isValid(): bool
84
    {
85
        $validator = $this->getValidator();
86
        if ($validator === false) {
87
            return true;
88
        }
89
90
        return $validator->isValid();
91
    }
92
93
    private function getValidator()
94
    {
95
        if (null === $this->validator) {
96
            return false;
97
        }
98
99
        return $this->validator;
100
    }
101
102
    /**
103
     * Validate the current Entity
104
     *
105
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException
106
     */
107
    public function validate(): void
108
    {
109
        $validator = $this->getValidator();
110
        if ($validator === false) {
111
            return;
112
        }
113
114
        $validator->validate();
115
    }
116
117
    /**
118
     * Validate a named property in the current Entity
119
     *
120
     * @param string $propertyName
121
     *
122
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException
123
     */
124
    public function validateProperty(string $propertyName): void
125
    {
126
        $validator = $this->getValidator();
127
        if ($validator === false) {
128
            return;
129
        }
130
        $validator->validateProperty($propertyName);
131
    }
132
}
133