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 ( b8d566...1d13d3 )
by joseph
22:13
created

EntityFactory::createEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Factory;
4
5
use Doctrine\Common\NotifyPropertyChanged;
6
use Doctrine\ORM\EntityManagerInterface;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
8
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
9
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory;
10
use EdmondsCommerce\DoctrineStaticMeta\EntityManager\Mapping\GenericFactoryInterface;
11
12
class EntityFactory implements GenericFactoryInterface
13
{
14
    /**
15
     * @var NamespaceHelper
16
     */
17
    protected $namespaceHelper;
18
    /**
19
     * @var EntityValidatorFactory
20
     */
21
    private $entityValidatorFactory;
22
    /**
23
     * @var EntityManagerInterface
24
     */
25
    private $entityManager;
26
27
    public function __construct(EntityValidatorFactory $entityValidatorFactory, NamespaceHelper $namespaceHelper)
28
    {
29
        $this->entityValidatorFactory = $entityValidatorFactory;
30
        $this->namespaceHelper        = $namespaceHelper;
31
    }
32
33
    public function setEntityManager(EntityManagerInterface $entityManager): void
34
    {
35
        $this->entityManager = $entityManager;
36
    }
37
38
    /**
39
     * Get an instance of the specific Entity Factory for a specified Entity
40
     *
41
     * Not type hinting the return because the whole point of this is to have an entity specific method, which we
42
     * can't hint for
43
     *
44
     * @param string $entityFqn
45
     *
46
     * @return mixed
47
     */
48 1
    public function createFactoryForEntity(string $entityFqn)
49
    {
50 1
        $this->assertEntityManagerSet();
51 1
        $factoryFqn = $this->namespaceHelper->getFactoryFqnFromEntityFqn($entityFqn);
52
53 1
        return new $factoryFqn($this, $this->entityManager);
54
    }
55
56
    private function assertEntityManagerSet()
57
    {
58
        if (!$this->entityManager instanceof EntityManagerInterface) {
0 ignored issues
show
introduced by
$this->entityManager is always a sub-type of Doctrine\ORM\EntityManagerInterface. If $this->entityManager can have other possible types, add them to src/Entity/Factory/EntityFactory.php:23.
Loading history...
59
            throw new \RuntimeException(
60
                'No EntityManager set, this must be set first using setEntityManager()'
61
            );
62
        }
63
    }
64
65
    public function getEntity(string $className)
66
    {
67
        return $this->create($className);
68
    }
69
70
    /**
71
     * Build a new entity with the validator factory preloaded
72
     *
73
     * Optionally pass in an array of property=>value
74
     *
75
     * @param string $entityFqn
76
     *
77
     * @param array  $values
78
     *
79
     * @return mixed
80
     */
81 3
    public function create(string $entityFqn, array $values = [])
82
    {
83 3
        $this->assertEntityManagerSet();
84 3
        $entity = $this->createEntity($entityFqn);
85 3
        $entity->ensureMetaDataIsSet($this->entityManager);
86 3
        $this->addListenerToEntityIfRequired($entity);
87 3
        $this->setEntityValues($entity, $values);
88
89 2
        return $entity;
90
    }
91
92
    /**
93
     * Create the Entity
94
     *
95
     * @param string $entityFqn
96
     *
97
     * @return EntityInterface
98
     */
99
    private function createEntity(string $entityFqn): EntityInterface
100
    {
101
        return new $entityFqn($this->entityValidatorFactory);
102
    }
103
104
    /**
105
     * Generally DSM Entities are using the Notify change tracking policy.
106
     * This ensures that they are fully set up for that
107
     *
108
     * @param EntityInterface $entity
109
     */
110
    private function addListenerToEntityIfRequired(EntityInterface $entity): void
111
    {
112
        if (!$entity instanceof NotifyPropertyChanged) {
0 ignored issues
show
introduced by
$entity is always a sub-type of Doctrine\Common\NotifyPropertyChanged.
Loading history...
113
            return;
114
        }
115
        $listener = $this->entityManager->getUnitOfWork();
116
        $entity->addPropertyChangedListener($listener);
117
    }
118
119
    /**
120
     * Set all the values, if there are any
121
     *
122
     * @param EntityInterface $entity
123
     * @param array           $values
124
     */
125
    private function setEntityValues(EntityInterface $entity, array $values): void
126
    {
127
        if ([] === $values) {
128
            return;
129
        }
130
        foreach ($values as $property => $value) {
131
            $setter = 'set' . $property;
132
            if (!method_exists($entity, $setter)) {
133
                throw new \InvalidArgumentException(
134
                    'The entity ' . \get_class($entity) . ' does not have the setter method ' . $setter
135
                    . "\n\nmethods: " . \print_r(get_class_methods($entity), true)
136
                );
137
            }
138
            $entity->$setter($value);
139
        }
140
    }
141
}
142