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 ( 2a6b46...0d82f1 )
by joseph
20s queued 14s
created

FixturesHelper::setCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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