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 Ross
22:04
created

AbstractEntityFixtureLoader::addReference()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 6
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\DataFixtures\OrderedFixtureInterface;
7
use Doctrine\Common\Persistence\ObjectManager;
8
use Doctrine\ORM\EntityManagerInterface;
9
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
10
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
11
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory;
12
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverInterface;
13
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator;
14
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGeneratorFactory;
15
use Psr\Container\ContainerInterface;
16
17
/**
18
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19
 */
20
abstract class AbstractEntityFixtureLoader extends AbstractFixture implements OrderedFixtureInterface
21
{
22
    public const ORDER_FIRST = 1000;
23
24
    public const ORDER_DEFAULT = 2000;
25
26
    public const ORDER_LAST = 3000;
27
28
    public const BULK_AMOUNT_TO_GENERATE = 100;
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 int
54
     */
55
    protected $order = self::ORDER_DEFAULT;
56
    /**
57
     * @var TestEntityGeneratorFactory
58
     */
59
    private $testEntityGeneratorFactory;
60
    /**
61
     * @var ContainerInterface
62
     */
63
    private $container;
64
65
    private $usingReferences = true;
66
67
    public function __construct(
68
        TestEntityGeneratorFactory $testEntityGeneratorFactory,
69
        EntitySaverFactory $saverFactory,
70
        NamespaceHelper $namespaceHelper,
71
        ContainerInterface $container,
72
        ?FixtureEntitiesModifierInterface $modifier = null
73
    ) {
74
        $this->namespaceHelper = $namespaceHelper;
75
        $this->entityFqn       = $this->getEntityFqn();
76
        $this->saver           = $saverFactory->getSaverForEntityFqn($this->entityFqn);
77
        if (null !== $modifier) {
78
            $this->setModifier($modifier);
79
        }
80
        $this->testEntityGeneratorFactory = $testEntityGeneratorFactory;
81
        $this->container                  = $container;
82
    }
83
84
    /**
85
     * Get the fully qualified name of the Entity we are testing,
86
     * assumes EntityNameTest as the entity class short name
87
     *
88
     * @return string
89
     */
90
    protected function getEntityFqn(): string
91
    {
92
        if (null === $this->entityFqn) {
93
            $this->entityFqn = $this->namespaceHelper->getEntityFqnFromFixtureFqn(static::class);
94
        }
95
96
        return $this->entityFqn;
97
    }
98
99
    /**
100
     * Use this method to inject your own modifier that will receive the array of generated entities and can then
101
     * update them as you see fit
102
     *
103
     * @param FixtureEntitiesModifierInterface $modifier
104
     */
105
    public function setModifier(FixtureEntitiesModifierInterface $modifier): void
106
    {
107
        $this->modifier = $modifier;
108
    }
109
110
    /**
111
     * @return int
112
     */
113
    public function getOrder(): int
114
    {
115
        return $this->order;
116
    }
117
118
    /**
119
     * @param int $order
120
     *
121
     * @return AbstractEntityFixtureLoader
122
     */
123
    public function setOrder(int $order): self
124
    {
125
        $this->order = $order;
126
127
        return $this;
128
    }
129
130
    /**
131
     * Load data fixtures with the passed EntityManager
132
     *
133
     * @param ObjectManager $manager
134
     *
135
     * @throws \Doctrine\ORM\Mapping\MappingException
136
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
137
     * @throws \ErrorException
138
     * @throws \ReflectionException
139
     */
140
    public function load(ObjectManager $manager)
141
    {
142
        if (!$manager instanceof EntityManagerInterface) {
143
            throw new \RuntimeException(
144
                'Expecting $manager to be EntityManagerInterface but got ' . \get_class($manager)
145
            );
146
        }
147
        $this->testEntityGenerator = $this->testEntityGeneratorFactory->createForEntityFqn($this->entityFqn, $manager);
148
        $this->testEntityGenerator->assertSameEntityManagerInstance($manager);
149
        $entities = $this->loadBulk();
150
        $this->updateGenerated($entities);
151
        $this->saver->saveAll($entities);
152
    }
153
154
    /**
155
     * @return array|EntityInterface[]
156
     * @throws \Doctrine\ORM\Mapping\MappingException
157
     * @throws \ErrorException
158
     * @throws \ReflectionException
159
     */
160
    protected function loadBulk(): array
161
    {
162
        $entities = $this->testEntityGenerator->generateEntities(
163
            static::BULK_AMOUNT_TO_GENERATE
164
        );
165
        foreach ($entities as $generated) {
166
            $this->testEntityGenerator->addAssociationEntities($generated);
167
        }
168
169
        return $entities;
170
    }
171
172
    protected function updateGenerated(array &$entities)
173
    {
174
        if (null === $this->modifier) {
175
            return;
176
        }
177
        $this->modifier->modifyEntities($entities);
178
    }
179
180
    /**
181
     * @param bool $usingReferences
182
     *
183
     * @return AbstractEntityFixtureLoader
184
     */
185
    public function setUsingReferences(bool $usingReferences): AbstractEntityFixtureLoader
186
    {
187
        $this->usingReferences = $usingReferences;
188
189
        return $this;
190
    }
191
192
    public function addReference($name, $object)
193
    {
194
        if (false === $this->usingReferences) {
195
            return;
196
        }
197
        parent::addReference($name, $object);
198
    }
199
200
    /**
201
     * Generally we should avoid using the container as a service locator, however for test assets it is acceptable if
202
     * really necessary
203
     *
204
     * @return ContainerInterface
205
     */
206
    protected function getContainer(): ContainerInterface
207
    {
208
        return $this->container;
209
    }
210
211
212
}
213