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 (#120)
by joseph
21:56
created

AbstractEntityFixtureLoader::updateGenerated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
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 list of Faker data providers for the project
74
     *
75
     * @return array|string[]
76
     */
77 5
    protected function getFakerDataProviders(): array
78
    {
79 5
        $projectRootNamespace = $this->namespaceHelper->getProjectNamespaceRootFromEntityFqn($this->entityFqn);
80 5
        $abstractTestFqn      = $this->namespaceHelper->tidy(
81 5
            $projectRootNamespace . '\\Entities\\AbstractEntityTest'
82
        );
83 5
        if (!\class_exists($abstractTestFqn)) {
84
            return [];
85
        }
86 5
        if (!\defined($abstractTestFqn . '::FAKER_DATA_PROVIDERS')) {
87
            return [];
88
        }
89
90 5
        return $abstractTestFqn::FAKER_DATA_PROVIDERS;
91
    }
92
93
    /**
94
     * @return int
95
     */
96 1
    public function getOrder(): int
97
    {
98 1
        return $this->order;
99
    }
100
101
    /**
102
     * @param int $order
103
     *
104
     * @return AbstractEntityFixtureLoader
105
     */
106 1
    public function setOrder(int $order): self
107
    {
108 1
        $this->order = $order;
109
110 1
        return $this;
111
    }
112
113
    /**
114
     * Use this method to inject your own modifier that will receive the array of generated entities and can then
115
     * update them as you see fit
116
     *
117
     * @param FixtureEntitiesModifierInterface $modifier
118
     */
119 2
    public function setModifier(FixtureEntitiesModifierInterface $modifier): void
120
    {
121 2
        $this->modifier = $modifier;
122 2
    }
123
124
125
    /**
126
     * Load data fixtures with the passed EntityManager
127
     *
128
     * @param ObjectManager $manager
129
     *
130
     * @throws \Doctrine\ORM\Mapping\MappingException
131
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
132
     * @throws \ErrorException
133
     * @throws \ReflectionException
134
     */
135 3
    public function load(ObjectManager $manager)
136
    {
137 3
        if (!$manager instanceof EntityManagerInterface) {
138
            throw new \RuntimeException(
139
                'Expecting $manager to be EntityManagerInterface but got ' . \get_class($manager)
140
            );
141
        }
142 3
        $entities = $this->loadBulk($manager);
143 3
        $this->updateGenerated($entities);
144 3
        $this->saver->saveAll($entities);
145 3
    }
146
147 3
    protected function updateGenerated(array &$entities)
148
    {
149 3
        if (null === $this->modifier) {
150 2
            return;
151
        }
152 1
        $this->modifier->modifyEntities($entities);
153 1
    }
154
155
    /**
156
     * @param EntityManagerInterface $entityManager
157
     *
158
     * @return array|EntityInterface[]
159
     * @throws \Doctrine\ORM\Mapping\MappingException
160
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
161
     * @throws \ErrorException
162
     * @throws \ReflectionException
163
     */
164 3
    protected function loadBulk(EntityManagerInterface $entityManager): array
165
    {
166 3
        $entities = $this->testEntityGenerator->generateEntities(
167 3
            $entityManager,
168 3
            $this->entityFqn,
169 3
            static::BULK_AMOUNT_TO_GENERATE
170
        );
171 3
        foreach ($entities as $generated) {
172 3
            $this->testEntityGenerator->addAssociationEntities($entityManager, $generated);
173
        }
174
175 3
        return $entities;
176
    }
177
178
    /**
179
     * Get the fully qualified name of the Entity we are testing,
180
     * assumes EntityNameTest as the entity class short name
181
     *
182
     * @return string
183
     */
184 5
    protected function getEntityFqn(): string
185
    {
186 5
        if (null === $this->entityFqn) {
187 5
            $this->entityFqn = \str_replace(
188 5
                '\\Assets\\EntityFixtures\\',
189 5
                '\\Entities\\',
190 5
                \substr(static::class, 0, -7)
191
            );
192
        }
193
194 5
        return $this->entityFqn;
195
    }
196
}
197