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
Pull Request — master (#154)
by joseph
34:48
created

AbstractEntityFixtureLoader::setOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
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 = true;
62
    /**
63
     * @var EntityManagerInterface
64
     */
65
    private $entityManager;
66
67
    public function __construct(
68
        TestEntityGeneratorFactory $testEntityGeneratorFactory,
69
        EntitySaverFactory $saverFactory,
70
        NamespaceHelper $namespaceHelper,
71
        EntityManagerInterface $entityManager,
72
        ContainerInterface $container,
73
        ?FixtureEntitiesModifierInterface $modifier = null
74
    ) {
75
        $this->namespaceHelper = $namespaceHelper;
76
        $this->entityFqn       = $this->getEntityFqn();
77
        $this->saver           = $saverFactory->getSaverForEntityFqn($this->entityFqn);
78
        if (null !== $modifier) {
79
            $this->setModifier($modifier);
80
        }
81
        $this->testEntityGeneratorFactory = $testEntityGeneratorFactory;
82
        $this->container                  = $container;
83
        $this->assertReferencePrefixOverridden();
84
        $this->entityManager = $entityManager;
85
    }
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
    protected function getEntityFqn(): string
94
    {
95
        if (null === $this->entityFqn) {
96
            $this->entityFqn = $this->namespaceHelper->getEntityFqnFromFixtureFqn(static::class);
97
        }
98
99
        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
    public function setModifier(FixtureEntitiesModifierInterface $modifier): void
109
    {
110
        $this->modifier = $modifier;
111
    }
112
113
    private function assertReferencePrefixOverridden(): void
114
    {
115
        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
    }
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
    public function load(ObjectManager $manager)
131
    {
132
        if (!$manager instanceof EntityManagerInterface) {
133
            throw new \RuntimeException(
134
                'Expecting $manager to be EntityManagerInterface but got ' . \get_class($manager)
135
            );
136
        }
137
        $this->testEntityGenerator = $this->testEntityGeneratorFactory->createForEntityFqn($this->entityFqn, $manager);
138
        $this->testEntityGenerator->assertSameEntityManagerInstance($manager);
139
        $entities = $this->loadBulk();
140
        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
        $this->updateGenerated($entities);
148
        $this->saver->saveAll($entities);
149
    }
150
151
    /**
152
     * @return array|EntityInterface[]
153
     * @throws \Doctrine\ORM\Mapping\MappingException
154
     * @throws \ErrorException
155
     * @throws \ReflectionException
156
     */
157
    protected function loadBulk(): array
158
    {
159
        $entities = $this->testEntityGenerator->generateEntities(
160
            static::BULK_AMOUNT_TO_GENERATE
161
        );
162
        $num      = 0;
163
        foreach ($entities as $generated) {
164
            $this->addReference(static::REFERENCE_PREFIX . $num++, $generated);
165
        }
166
167
        return $entities;
168
    }
169
170
    public function addReference($name, $object)
171
    {
172
        if (false === $this->usingReferences) {
173
            return;
174
        }
175
        parent::addReference($name, $object);
176
    }
177
178
    protected function updateGenerated(array &$entities)
179
    {
180
        if (null === $this->modifier) {
181
            return;
182
        }
183
        $this->modifier->modifyEntities($entities);
184
    }
185
186
    public function getReference($name): EntityInterface
187
    {
188
        $reference = parent::getReference($name);
189
        $this->entityManager->initializeObject($reference);
190
        if ($reference instanceof EntityInterface) {
191
            return $reference;
192
        }
193
        throw new \RuntimeException('Failed initialising refernce into Entity');
194
    }
195
196
    /**
197
     * @param bool $usingReferences
198
     *
199
     * @return AbstractEntityFixtureLoader
200
     */
201
    public function setUsingReferences(bool $usingReferences): AbstractEntityFixtureLoader
202
    {
203
        $this->usingReferences = $usingReferences;
204
205
        return $this;
206
    }
207
208
    /**
209
     * Generally we should avoid using the container as a service locator, however for test assets it is acceptable if
210
     * really necessary
211
     *
212
     * @return ContainerInterface
213
     */
214
    protected function getContainer(): ContainerInterface
215
    {
216
        return $this->container;
217
    }
218
}
219