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

FixturesHelper::createDb()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.0975

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 3
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 9.9666
ccs 5
cts 13
cp 0.3846
crap 5.0975
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures;
4
5
use Doctrine\Common\Cache\FilesystemCache;
6
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
7
use Doctrine\Common\DataFixtures\FixtureInterface;
8
use Doctrine\DBAL\Logging\SQLLogger;
9
use Doctrine\ORM\EntityManagerInterface;
10
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
11
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory;
12
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGeneratorFactory;
13
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
14
use EdmondsCommerce\DoctrineStaticMeta\Schema\Database;
15
use EdmondsCommerce\DoctrineStaticMeta\Schema\Schema;
16
use Psr\Container\ContainerInterface;
17
use RuntimeException;
18
19
/**
20
 * To be used in your Test classes. This provides you with the methods to use in your setup method to create the
21
 * fixtures are required
22
 *
23
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
24
 */
25
class FixturesHelper
26
{
27
    /**
28
     * @var Database
29
     */
30
    protected $database;
31
    /**
32
     * @var Schema
33
     */
34
    protected $schema;
35
    /**
36
     * @var EntityManagerInterface
37
     */
38
    protected $entityManager;
39
    /**
40
     * @var FilesystemCache
41
     */
42
    protected $cache;
43
    /**
44
     * @var null|string
45
     */
46
    protected $cacheKey;
47
    /**
48
     * @var ORMExecutor
49
     */
50
    private $fixtureExecutor;
51
52
    /**
53
     * @var Loader
54
     */
55
    private $fixtureLoader;
56
57
    /**
58
     * Did we load cached Fixture SQL?
59
     *
60
     * @var bool
61
     */
62
    private $loadedFromCache = false;
63
64
    /**
65
     * Should we load cached Fixture SQL if available?
66
     *
67
     * @var bool
68
     */
69
    private $loadFromCache = true;
70
    /**
71
     * @var EntitySaverFactory
72
     */
73
    private $entitySaverFactory;
74
    /**
75
     * @var NamespaceHelper
76
     */
77
    private $namespaceHelper;
78
    /**
79
     * @var TestEntityGeneratorFactory
80
     */
81
    private $testEntityGeneratorFactory;
82
    /**
83
     * @var ContainerInterface
84
     */
85
    private $container;
86
87 4
    public function __construct(
88
        EntityManagerInterface $entityManager,
89
        Database $database,
90
        Schema $schema,
91
        FilesystemCache $cache,
92
        EntitySaverFactory $entitySaverFactory,
93
        NamespaceHelper $namespaceHelper,
94
        TestEntityGeneratorFactory $testEntityGeneratorFactory,
95
        ContainerInterface $container,
96
        ?string $cacheKey = null
97
    ) {
98 4
        $purger                           = null;
99 4
        $this->fixtureExecutor            = new ORMExecutor($entityManager, $purger);
100 4
        $this->fixtureLoader              = new Loader();
101 4
        $this->database                   = $database;
102 4
        $this->schema                     = $schema;
103 4
        $this->entityManager              = $entityManager;
104 4
        $this->cache                      = $cache;
105 4
        $this->entitySaverFactory         = $entitySaverFactory;
106 4
        $this->namespaceHelper            = $namespaceHelper;
107 4
        $this->testEntityGeneratorFactory = $testEntityGeneratorFactory;
108 4
        $this->cacheKey                   = $cacheKey;
109 4
        $this->container                  = $container;
110
111 4
        $this->fixtureLoader->setFixturesHelper($this);
112 4
    }
113
114
    /**
115
     * @param null|string $cacheKey
116
     */
117 4
    public function setCacheKey(?string $cacheKey): void
118
    {
119 4
        $this->cacheKey = $cacheKey;
120 4
    }
121
122 4
    public function createFixtureInstanceForEntityFqn(
123
        string $entityFqn,
124
        FixtureEntitiesModifierInterface $modifier = null
125
    ): AbstractEntityFixtureLoader {
126 4
        $fixtureFqn = $this->namespaceHelper->getFixtureFqnFromEntityFqn($entityFqn);
127
128 4
        return $this->createFixture($fixtureFqn, $modifier);
129
    }
130
131 4
    public function createFixture(
132
        string $fixtureFqn,
133
        FixtureEntitiesModifierInterface $modifier = null
134
    ): AbstractEntityFixtureLoader {
135 4
        return new $fixtureFqn(
136 4
            $this->testEntityGeneratorFactory,
137 4
            $this->entitySaverFactory,
138 4
            $this->namespaceHelper,
139 4
            $this->entityManager,
140 4
            $this->container,
141 4
            $modifier
142
        );
143
    }
144
145
    /**
146
     * Clean the DB and insert fixtures
147
     *
148
     * You can pass in a fixture loader directly which will be appended to the main fixtureLoader, or you can add
149
     * fixtures prior to calling this method
150
     *
151
     * We do not use the purge functionality of the `doctrine/data-fixtures` module, instead we fully drop and create
152
     * the database.
153
     *
154
     * @param FixtureInterface $fixture
155
     *
156
     * @throws DoctrineStaticMetaException
157
     */
158 4
    public function createDb(?FixtureInterface $fixture = null): void
159
    {
160 4
        if (null !== $fixture) {
161
            $this->addFixture($fixture);
162 4
        } elseif ([] === $this->fixtureLoader->getFixtures()) {
163
            throw new RuntimeException(
164
                'No fixtures have been set.'
165
                . 'You need to either pass in a Fixture to this method, or have called `addFixture` at least once '
166
                . 'before calling this method'
167
            );
168
        }
169 4
        $this->resetDb();
170 4
        $this->run();
171 4
    }
172
173 4
    public function addFixture(FixtureInterface $fixture): void
174
    {
175 4
        $this->fixtureLoader->addFixture($fixture);
176 4
    }
177
178 4
    public function resetDb(): void
179
    {
180 4
        $this->database->drop(true)->create(true);
181 4
        $this->schema->create();
182 4
    }
183
184 4
    public function run(): void
185
    {
186 4
        $cacheKey   = $this->getCacheKey();
187 4
        $connection = $this->entityManager->getConnection();
188 4
        if ($this->loadFromCache && $this->cache->contains($cacheKey)) {
189 1
            $logger = $this->cache->fetch($cacheKey);
190 1
            $logger->run($this->entityManager->getConnection());
191 1
            $this->loadedFromCache = true;
192
193 1
            return;
194
        }
195
196 3
        $logger              = $this->getLogger();
197 3
        $connectionLogger    = $connection->getConfiguration()->getSQLLogger();
198 3
        $configurationLogger = $this->entityManager->getConfiguration()->getSQLLogger();
199
200 3
        $connection->getConfiguration()->setSQLLogger($logger);
201 3
        $this->entityManager->getConfiguration()->setSQLLogger($logger);
202
203 3
        $this->fixtureExecutor->execute($this->fixtureLoader->getFixtures(), true);
204
205 3
        $this->entityManager->getConfiguration()->setSQLLogger($configurationLogger);
206 3
        $connection->getConfiguration()->setSQLLogger($connectionLogger);
207
208 3
        $this->cache->save($cacheKey, $logger);
209 3
    }
210
211 4
    private function getCacheKey(): string
212
    {
213 4
        if (null !== $this->cacheKey) {
214 4
            return $this->cacheKey;
215
        }
216
217
        $fixtureFqns = [];
218
        foreach ($this->fixtureLoader->getFixtures() as $fixture) {
219
            $fixtureFqns[] = get_class($fixture);
220
        }
221
222
        return md5(print_r($fixtureFqns, true));
223
    }
224
225 3
    private function getLogger(): SQLLogger
226
    {
227 3
        return new QueryCachingLogger();
228
    }
229
230
    public function clearFixtures(): void
231
    {
232
        $this->fixtureLoader = new Loader();
233
    }
234
235
    /**
236
     * @return bool
237
     */
238 2
    public function isLoadedFromCache(): bool
239
    {
240 2
        return $this->loadedFromCache;
241
    }
242
243
    /**
244
     * @param bool $loadFromCache
245
     *
246
     * @return FixturesHelper
247
     */
248 1
    public function setLoadFromCache(bool $loadFromCache): FixturesHelper
249
    {
250 1
        $this->loadFromCache = $loadFromCache;
251
252 1
        return $this;
253
    }
254
}
255