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 ( 07a5d8...b7113a )
by joseph
32:24 queued 04:34
created

AbstractEntityFixtureLoader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.351

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 6
dl 0
loc 18
ccs 10
cts 18
cp 0.5556
crap 2.351
rs 9.9666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures;
4
5
use Doctrine\Common\DataFixtures\AbstractFixture;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\ORM\EntityManagerInterface;
8
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
9
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
10
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory;
11
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverInterface;
12
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator;
13
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGeneratorFactory;
14
use Psr\Container\ContainerInterface;
15
16
/**
17
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18
 */
19
abstract class AbstractEntityFixtureLoader extends AbstractFixture
20
{
21
    /**
22
     * If you override the loadBulk method, please ensure you update this number to reflect the number of Entities you
23
     * are actually generating
24
     */
25
    public const BULK_AMOUNT_TO_GENERATE = 100;
26
27
    public const REFERENCE_PREFIX = 'OVERRIDE ME';
28
29
    /**
30
     * @var TestEntityGenerator
31
     */
32
    protected $testEntityGenerator;
33
    /**
34
     * @var EntitySaverInterface
35
     */
36
    protected $saver;
37
38
    /**
39
     * @var null|FixtureEntitiesModifierInterface
40
     */
41
    protected $modifier;
42
43
    /**
44
     * @var string
45
     */
46
    protected $entityFqn;
47
    /**
48
     * @var NamespaceHelper
49
     */
50
    protected $namespaceHelper;
51
52
    /**
53
     * @var TestEntityGeneratorFactory
54
     */
55
    private $testEntityGeneratorFactory;
56
    /**
57
     * @var ContainerInterface
58
     */
59
    private $container;
60
61
    private $usingReferences = false;
62
    /**
63
     * @var EntityManagerInterface
64
     */
65
    private $entityManager;
66
67 5
    public function __construct(
68
        TestEntityGeneratorFactory $testEntityGeneratorFactory,
69
        EntitySaverFactory $saverFactory,
70
        NamespaceHelper $namespaceHelper,
71
        EntityManagerInterface $entityManager,
72
        ContainerInterface $container,
73
        ?FixtureEntitiesModifierInterface $modifier = null
74
    ) {
75 5
        $this->namespaceHelper = $namespaceHelper;
76 5
        $this->entityFqn       = $this->getEntityFqn();
77 5
        $this->saver           = $saverFactory->getSaverForEntityFqn($this->entityFqn);
78 5
        if (null !== $modifier) {
79 1
            $this->setModifier($modifier);
80
        }
81 5
        $this->testEntityGeneratorFactory = $testEntityGeneratorFactory;
82 5
        $this->container                  = $container;
83 5
        $this->assertReferencePrefixOverridden();
84 5
        $this->entityManager = $entityManager;
85 5
    }
86
87
    /**
88
     * Get the fully qualified name of the Entity we are testing,
89
     * assumes EntityNameTest as the entity class short name
90
     *
91
     * @return string
92
     */
93 5
    protected function getEntityFqn(): string
94
    {
95 5
        if (null === $this->entityFqn) {
96 5
            $this->entityFqn = $this->namespaceHelper->getEntityFqnFromFixtureFqn(static::class);
97
        }
98
99 5
        return $this->entityFqn;
100
    }
101
102
    /**
103
     * Use this method to inject your own modifier that will receive the array of generated entities and can then
104
     * update them as you see fit
105
     *
106
     * @param FixtureEntitiesModifierInterface $modifier
107
     */
108 1
    public function setModifier(FixtureEntitiesModifierInterface $modifier): void
109
    {
110 1
        $this->modifier = $modifier;
111 1
    }
112
113 5
    private function assertReferencePrefixOverridden(): void
114
    {
115 5
        if (static::REFERENCE_PREFIX === self::REFERENCE_PREFIX) {
0 ignored issues
show
introduced by
The condition static::REFERENCE_PREFIX... self::REFERENCE_PREFIX is always true.
Loading history...
116
            throw new \LogicException('You must override the REFERENCE_PREFIX constant in your Fixture');
117
        }
118 5
    }
119
120
    /**
121
     * Load data fixtures with the passed EntityManager
122
     *
123
     * @param ObjectManager $manager
124
     *
125
     * @throws \Doctrine\ORM\Mapping\MappingException
126
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
127
     * @throws \ErrorException
128
     * @throws \ReflectionException
129
     */
130 4
    public function load(ObjectManager $manager)
131
    {
132 4
        if (!$manager instanceof EntityManagerInterface) {
133
            throw new \RuntimeException(
134
                'Expecting $manager to be EntityManagerInterface but got ' . \get_class($manager)
135
            );
136
        }
137 4
        $this->testEntityGenerator = $this->testEntityGeneratorFactory->createForEntityFqn($this->entityFqn, $manager);
138 4
        $this->testEntityGenerator->assertSameEntityManagerInstance($manager);
139 4
        $entities = $this->loadBulk();
140 4
        if (count($entities) !== static::BULK_AMOUNT_TO_GENERATE) {
141
            throw new \RuntimeException(
142
                'generated ' . count($entities) .
143
                ' but the constant ' . get_class($this) . '::BULK_AMOUNT_TO_GENERATE is ' .
144
                static::BULK_AMOUNT_TO_GENERATE
145
            );
146
        }
147 4
        $this->updateGenerated($entities);
148 4
        $this->saver->saveAll($entities);
149 4
    }
150
151
    /**
152
     * @return array|EntityInterface[]
153
     * @throws \Doctrine\ORM\Mapping\MappingException
154
     * @throws \ErrorException
155
     * @throws \ReflectionException
156
     */
157 4
    protected function loadBulk(): array
158
    {
159 4
        $entities = $this->testEntityGenerator->generateEntities(
160 4
            static::BULK_AMOUNT_TO_GENERATE
161
        );
162 4
        $num      = 0;
163 4
        foreach ($entities as $generated) {
164 4
            $this->addReference(static::REFERENCE_PREFIX . $num++, $generated);
165
        }
166
167 4
        return $entities;
168
    }
169
170 4
    public function addReference($name, $object)
171
    {
172 4
        if (false === $this->usingReferences) {
173
            return;
174
        }
175 4
        parent::addReference($name, $object);
176 4
    }
177
178 4
    protected function updateGenerated(array &$entities)
179
    {
180 4
        if (null === $this->modifier) {
181 3
            return;
182
        }
183 1
        $this->modifier->modifyEntities($entities);
184 1
    }
185
186 4
    public function setReferenceRepository(\Doctrine\Common\DataFixtures\ReferenceRepository $referenceRepository)
187
    {
188 4
        $this->setUsingReferences(true);
189 4
        parent::setReferenceRepository($referenceRepository); // TODO: Change the autogenerated stub
190 4
    }
191
192
    /**
193
     * @param bool $usingReferences
194
     *
195
     * @return AbstractEntityFixtureLoader
196
     */
197 4
    public function setUsingReferences(bool $usingReferences): AbstractEntityFixtureLoader
198
    {
199 4
        $this->usingReferences = $usingReferences;
200
201 4
        return $this;
202
    }
203
204
    public function getReference($name): EntityInterface
205
    {
206
        $reference = parent::getReference($name);
207
        $this->entityManager->initializeObject($reference);
208
        if ($reference instanceof EntityInterface) {
209
            return $reference;
210
        }
211
        throw new \RuntimeException('Failed initialising refernce into Entity');
212
    }
213
214
    /**
215
     * Generally we should avoid using the container as a service locator, however for test assets it is acceptable if
216
     * really necessary
217
     *
218
     * @return ContainerInterface
219
     */
220
    protected function getContainer(): ContainerInterface
221
    {
222
        return $this->container;
223
    }
224
}
225