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 ( 84e592...894b2c )
by joseph
127:56 queued 125:09
created

Initialiser::setAsVisited()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
crap 1.037
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Validation;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\PersistentCollection;
7
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
8
use Symfony\Component\Validator\ObjectInitializerInterface;
9
10
class Initialiser implements ObjectInitializerInterface
11
{
12
    /**
13
     * @var EntityManagerInterface
14
     */
15
    private $entityManager;
16
17
    private $visited = [];
18
19 2
    public function __construct(EntityManagerInterface $entityManager)
20
    {
21 2
        $this->entityManager = $entityManager;
22 2
    }
23
24
    /**
25
     * Initializes an object just before validation.
26
     *
27
     * @param object $object The object to validate
28
     */
29 2
    public function initialize($object): void
30
    {
31 2
        $this->initialise($object);
32 2
    }
33
34 2
    public function initialise(object $object): void
35
    {
36 2
        $this->visited = [];
37 2
        $this->initialiseObject($object);
38 2
    }
39
40 2
    private function initialiseObject(object $object): void
41
    {
42 2
        if (true === $this->isVisited($object)) {
43 2
            return;
44
        }
45 2
        $this->setAsVisited($object);
46 2
        $this->entityManager->initializeObject($object);
47 2
        if ($object instanceof EntityInterface) {
48 2
            $this->initialiseProperties($object);
49
        }
50 2
    }
51
52 2
    private function isVisited(object $object): bool
53
    {
54 2
        return isset($this->visited[spl_object_hash($object)]);
55
    }
56
57 2
    private function setAsVisited(object $object): void
58
    {
59 2
        $this->visited[spl_object_hash($object)] = true;
60 2
    }
61
62 2
    private function initialiseProperties(EntityInterface $entity): void
63
    {
64 2
        $getters = $entity::getDoctrineStaticMeta()->getGetters();
65 2
        foreach ($getters as $getter) {
66
            try {
67 2
                $got = $entity->$getter();
68
            } catch (\TypeError $e) {
69
                //getters for things that have not yet been set will return null
70
                //but they might be required. This should be caught by the validation, not cause a type error here
71
                continue;
72
            }
73 2
            if (false === is_object($got)) {
74 2
                continue;
75
            }
76 2
            if ($got instanceof \Doctrine\ORM\Proxy\Proxy) {
77 1
                $this->initialiseObject($got);
78 1
                continue;
79
            }
80 2
            if ($got instanceof EntityInterface) {
81 2
                $this->initialiseObject($got);
82 2
                continue;
83
            }
84 2
            if ($got instanceof PersistentCollection) {
85 1
                $this->initialiseObject($got);
86 1
                continue;
87
            }
88
        }
89 2
    }
90
}
91