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 ( bcc1e6...b8d566 )
by joseph
19:29 queued 16:40
created

AbstractEntityFixtureLoader::loadBulk()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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
16
/**
17
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18
 */
19
abstract class AbstractEntityFixtureLoader extends AbstractFixture implements OrderedFixtureInterface
20
{
21
    public const ORDER_FIRST = 1000;
22
23
    public const ORDER_DEFAULT = 2000;
24
25
    public const ORDER_LAST = 3000;
26
27
    public const BULK_AMOUNT_TO_GENERATE = 100;
28
    /**
29
     * @var TestEntityGenerator
30
     */
31
    protected $testEntityGenerator;
32
    /**
33
     * @var EntitySaverInterface
34
     */
35
    protected $saver;
36
37
    /**
38
     * @var null|FixtureEntitiesModifierInterface
39
     */
40
    protected $modifier;
41
42
    /**
43
     * @var string
44
     */
45
    protected $entityFqn;
46
    /**
47
     * @var NamespaceHelper
48
     */
49
    protected $namespaceHelper;
50
51
    /**
52
     * @var int
53
     */
54
    protected $order = self::ORDER_DEFAULT;
55
56 5
    public function __construct(
57
        TestEntityGeneratorFactory $testEntityGeneratorFactory,
58
        EntitySaverFactory $saverFactory,
59
        NamespaceHelper $namespaceHelper,
60
        ?FixtureEntitiesModifierInterface $modifier = null
61
    ) {
62 5
        $this->namespaceHelper = $namespaceHelper;
63 5
        $this->entityFqn       = $this->getEntityFqn();
64 5
        $this->saver           = $saverFactory->getSaverForEntityFqn($this->entityFqn);
65 5
        $testEntityGeneratorFactory->setFakerDataProviderClasses($this->getFakerDataProviders());
66 5
        $this->testEntityGenerator = $testEntityGeneratorFactory->createForEntityFqn($this->entityFqn);
67 5
        if (null !== $modifier) {
68 2
            $this->setModifier($modifier);
69
        }
70 5
    }
71
72
    /**
73
     * Get the fully qualified name of the Entity we are testing,
74
     * assumes EntityNameTest as the entity class short name
75
     *
76
     * @return string
77
     */
78 5
    protected function getEntityFqn(): string
79
    {
80 5
        if (null === $this->entityFqn) {
81 5
            $this->entityFqn = \str_replace(
82 5
                '\\Assets\\EntityFixtures\\',
83 5
                '\\Entities\\',
84 5
                \substr(static::class, 0, -7)
85
            );
86
        }
87
88 5
        return $this->entityFqn;
89
    }
90
91
    /**
92
     * Get the list of Faker data providers for the project
93
     *
94
     * @return array|string[]
95
     */
96 5
    protected function getFakerDataProviders(): array
97
    {
98 5
        $projectRootNamespace = $this->namespaceHelper->getProjectNamespaceRootFromEntityFqn($this->entityFqn);
99 5
        $abstractTestFqn      = $this->namespaceHelper->tidy(
100 5
            $projectRootNamespace . '\\Entities\\AbstractEntityTest'
101
        );
102 5
        if (!\class_exists($abstractTestFqn)) {
103
            return [];
104
        }
105 5
        if (!\defined($abstractTestFqn . '::FAKER_DATA_PROVIDERS')) {
106
            return [];
107
        }
108
109 5
        return $abstractTestFqn::FAKER_DATA_PROVIDERS;
110
    }
111
112
    /**
113
     * Use this method to inject your own modifier that will receive the array of generated entities and can then
114
     * update them as you see fit
115
     *
116
     * @param FixtureEntitiesModifierInterface $modifier
117
     */
118 2
    public function setModifier(FixtureEntitiesModifierInterface $modifier): void
119
    {
120 2
        $this->modifier = $modifier;
121 2
    }
122
123
    /**
124
     * @return int
125
     */
126 1
    public function getOrder(): int
127
    {
128 1
        return $this->order;
129
    }
130
131
    /**
132
     * @param int $order
133
     *
134
     * @return AbstractEntityFixtureLoader
135
     */
136 1
    public function setOrder(int $order): self
137
    {
138 1
        $this->order = $order;
139
140 1
        return $this;
141
    }
142
143
    /**
144
     * Load data fixtures with the passed EntityManager
145
     *
146
     * @param ObjectManager $manager
147
     *
148
     * @throws \Doctrine\ORM\Mapping\MappingException
149
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
150
     * @throws \ErrorException
151
     * @throws \ReflectionException
152
     */
153 3
    public function load(ObjectManager $manager)
154
    {
155 3
        if (!$manager instanceof EntityManagerInterface) {
156
            throw new \RuntimeException(
157
                'Expecting $manager to be EntityManagerInterface but got ' . \get_class($manager)
158
            );
159
        }
160 3
        $entities = $this->loadBulk($manager);
161 3
        $this->updateGenerated($entities);
162 3
        $this->saver->saveAll($entities);
163 3
    }
164
165
    /**
166
     * @param EntityManagerInterface $entityManager
167
     *
168
     * @return array|EntityInterface[]
169
     * @throws \Doctrine\ORM\Mapping\MappingException
170
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
171
     * @throws \ErrorException
172
     * @throws \ReflectionException
173
     */
174 3
    protected function loadBulk(EntityManagerInterface $entityManager): array
175
    {
176 3
        $entities = $this->testEntityGenerator->generateEntities(
177 3
            $entityManager,
178 3
            $this->entityFqn,
179 3
            static::BULK_AMOUNT_TO_GENERATE
180
        );
181 3
        foreach ($entities as $generated) {
182 3
            $this->testEntityGenerator->addAssociationEntities($entityManager, $generated);
183
        }
184
185 3
        return $entities;
186
    }
187
188 3
    protected function updateGenerated(array &$entities)
189
    {
190 3
        if (null === $this->modifier) {
191 2
            return;
192
        }
193 1
        $this->modifier->modifyEntities($entities);
194 1
    }
195
}
196