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
19:10
created

AbstractEntityFixtureLoader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 167
ccs 0
cts 69
cp 0
rs 10
c 0
b 0
f 0
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 12 2
A __construct() 0 15 2
A getContainer() 0 3 1
A setOrder() 0 5 1
A updateGenerated() 0 6 2
A loadBulk() 0 10 2
A getOrder() 0 3 1
A getEntityFqn() 0 7 2
A setModifier() 0 3 1
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
    public function __construct(
66
        TestEntityGeneratorFactory $testEntityGeneratorFactory,
67
        EntitySaverFactory $saverFactory,
68
        NamespaceHelper $namespaceHelper,
69
        ContainerInterface $container,
70
        ?FixtureEntitiesModifierInterface $modifier = null
71
    ) {
72
        $this->namespaceHelper = $namespaceHelper;
73
        $this->entityFqn       = $this->getEntityFqn();
74
        $this->saver           = $saverFactory->getSaverForEntityFqn($this->entityFqn);
75
        if (null !== $modifier) {
76
            $this->setModifier($modifier);
77
        }
78
        $this->testEntityGeneratorFactory = $testEntityGeneratorFactory;
79
        $this->container                  = $container;
80
    }
81
82
    /**
83
     * Get the fully qualified name of the Entity we are testing,
84
     * assumes EntityNameTest as the entity class short name
85
     *
86
     * @return string
87
     */
88
    protected function getEntityFqn(): string
89
    {
90
        if (null === $this->entityFqn) {
91
            $this->entityFqn = $this->namespaceHelper->getEntityFqnFromFixtureFqn(static::class);
92
        }
93
94
        return $this->entityFqn;
95
    }
96
97
    /**
98
     * Use this method to inject your own modifier that will receive the array of generated entities and can then
99
     * update them as you see fit
100
     *
101
     * @param FixtureEntitiesModifierInterface $modifier
102
     */
103
    public function setModifier(FixtureEntitiesModifierInterface $modifier): void
104
    {
105
        $this->modifier = $modifier;
106
    }
107
108
    /**
109
     * @return int
110
     */
111
    public function getOrder(): int
112
    {
113
        return $this->order;
114
    }
115
116
    /**
117
     * @param int $order
118
     *
119
     * @return AbstractEntityFixtureLoader
120
     */
121
    public function setOrder(int $order): self
122
    {
123
        $this->order = $order;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Load data fixtures with the passed EntityManager
130
     *
131
     * @param ObjectManager $manager
132
     *
133
     * @throws \Doctrine\ORM\Mapping\MappingException
134
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
135
     * @throws \ErrorException
136
     * @throws \ReflectionException
137
     */
138
    public function load(ObjectManager $manager)
139
    {
140
        if (!$manager instanceof EntityManagerInterface) {
141
            throw new \RuntimeException(
142
                'Expecting $manager to be EntityManagerInterface but got ' . \get_class($manager)
143
            );
144
        }
145
        $this->testEntityGenerator = $this->testEntityGeneratorFactory->createForEntityFqn($this->entityFqn, $manager);
146
        $this->testEntityGenerator->assertSameEntityManagerInstance($manager);
147
        $entities = $this->loadBulk();
148
        $this->updateGenerated($entities);
149
        $this->saver->saveAll($entities);
150
    }
151
152
    /**
153
     * @return array|EntityInterface[]
154
     * @throws \Doctrine\ORM\Mapping\MappingException
155
     * @throws \ErrorException
156
     * @throws \ReflectionException
157
     */
158
    protected function loadBulk(): array
159
    {
160
        $entities = $this->testEntityGenerator->generateEntities(
161
            static::BULK_AMOUNT_TO_GENERATE
162
        );
163
        foreach ($entities as $generated) {
164
            $this->testEntityGenerator->addAssociationEntities($generated);
165
        }
166
167
        return $entities;
168
    }
169
170
    protected function updateGenerated(array &$entities)
171
    {
172
        if (null === $this->modifier) {
173
            return;
174
        }
175
        $this->modifier->modifyEntities($entities);
176
    }
177
178
    /**
179
     * Generally we should avoid using the container as a service locator, however for test assets it is acceptable if
180
     * really necessary
181
     *
182
     * @return ContainerInterface
183
     */
184
    protected function getContainer(): ContainerInterface
185
    {
186
        return $this->container;
187
    }
188
}
189