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 ( 0b4676...ecfcbd )
by joseph
17s queued 14s
created

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