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 ( d68f0e...658fac )
by Ross
14s queued 12s
created

Initialiser   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 83
ccs 0
cts 57
cp 0
rs 10
c 0
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 3 1
A __construct() 0 4 1
A isVisited() 0 3 1
A initialise() 0 4 1
A initialiseObject() 0 10 3
A setAsVisited() 0 3 1
B initialiseProperties() 0 25 7
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
    public function __construct(EntityManagerInterface $entityManager, EntityFactoryInterface $entityFactory)
25
    {
26
        $this->entityManager = $entityManager;
27
        $this->entityFactory = $entityFactory;
28
    }
29
30
    /**
31
     * Initializes an object just before validation.
32
     *
33
     * @param object $object The object to validate
34
     */
35
    public function initialize($object): void
36
    {
37
        $this->initialise($object);
38
    }
39
40
    public function initialise(object $object): void
41
    {
42
        $this->visited = [];
43
        $this->initialiseObject($object);
44
    }
45
46
    private function initialiseObject(object $object): void
47
    {
48
        if (true === $this->isVisited($object)) {
49
            return;
50
        }
51
        $this->setAsVisited($object);
52
        $this->entityManager->initializeObject($object);
53
        if ($object instanceof EntityInterface) {
54
            $this->entityFactory->initialiseEntity($object);
55
            $this->initialiseProperties($object);
56
        }
57
    }
58
59
    private function isVisited(object $object): bool
60
    {
61
        return isset($this->visited[spl_object_hash($object)]);
62
    }
63
64
    private function setAsVisited(object $object): void
65
    {
66
        $this->visited[spl_object_hash($object)] = true;
67
    }
68
69
    private function initialiseProperties(EntityInterface $entity): void
70
    {
71
        $getters = $entity::getDoctrineStaticMeta()->getGetters();
72
        foreach ($getters as $getter) {
73
            try {
74
                $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
            if (false === is_object($got)) {
81
                continue;
82
            }
83
            if ($got instanceof \Doctrine\ORM\Proxy\Proxy) {
84
                $this->initialiseObject($got);
85
                continue;
86
            }
87
            if ($got instanceof EntityInterface) {
88
                $this->initialiseObject($got);
89
                continue;
90
            }
91
            if ($got instanceof PersistentCollection) {
92
                $this->initialiseObject($got);
93
                continue;
94
            }
95
        }
96
    }
97
}
98