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 ( 34f827...08e101 )
by joseph
30s queued 13s
created

FixturesHelper::createDb()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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