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
16:19
created

FixturesHelper::getCacheKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 12
rs 10
ccs 0
cts 10
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\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
    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
        $purger                           = null;
99
        $this->fixtureExecutor            = new ORMExecutor($entityManager, $purger);
100
        $this->fixtureLoader              = new Loader();
101
        $this->database                   = $database;
102
        $this->schema                     = $schema;
103
        $this->entityManager              = $entityManager;
104
        $this->cache                      = $cache;
105
        $this->entitySaverFactory         = $entitySaverFactory;
106
        $this->namespaceHelper            = $namespaceHelper;
107
        $this->testEntityGeneratorFactory = $testEntityGeneratorFactory;
108
        $this->cacheKey                   = $cacheKey;
109
        $this->container                  = $container;
110
111
        $this->fixtureLoader->setFixturesHelper($this);
112
    }
113
114
    /**
115
     * @param null|string $cacheKey
116
     */
117
    public function setCacheKey(?string $cacheKey): void
118
    {
119
        $this->cacheKey = $cacheKey;
120
    }
121
122
    public function createFixtureInstanceForEntityFqn(
123
        string $entityFqn,
124
        FixtureEntitiesModifierInterface $modifier = null
125
    ): AbstractEntityFixtureLoader {
126
        $fixtureFqn = $this->namespaceHelper->getFixtureFqnFromEntityFqn($entityFqn);
127
128
        return $this->createFixture($fixtureFqn, $modifier);
129
    }
130
131
    public function createFixture(
132
        string $fixtureFqn,
133
        FixtureEntitiesModifierInterface $modifier = null
134
    ): AbstractEntityFixtureLoader {
135
        return new $fixtureFqn(
136
            $this->testEntityGeneratorFactory,
137
            $this->entitySaverFactory,
138
            $this->namespaceHelper,
139
            $this->entityManager,
140
            $this->container,
141
            $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
    public function createDb(?FixtureInterface $fixture = null): void
159
    {
160
        if (null !== $fixture) {
161
            $this->addFixture($fixture);
162
        } 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
        $this->database->drop(true)->create(true);
170
        $this->schema->create();
171
        $this->run();
172
    }
173
174
    public function addFixture(FixtureInterface $fixture): void
175
    {
176
        $this->fixtureLoader->addFixture($fixture);
177
    }
178
179
    public function run(): void
180
    {
181
        $cacheKey = $this->getCacheKey();
182
        $connection = $this->entityManager->getConnection();
183
        if ($this->loadFromCache && $this->cache->contains($cacheKey)) {
184
            $logger = $this->cache->fetch($cacheKey);
185
            $logger->run($this->entityManager->getConnection());
186
            $this->loadedFromCache = true;
187
188
            return;
189
        }
190
191
        $logger = $this->getLogger();
192
        $connectionLogger = $connection->getConfiguration()->getSQLLogger();
193
        $configurationLogger = $this->entityManager->getConfiguration()->getSQLLogger();
194
195
        $connection->getConfiguration()->setSQLLogger($logger);
196
        $this->entityManager->getConfiguration()->setSQLLogger($logger);
197
198
        $this->fixtureExecutor->execute($this->fixtureLoader->getFixtures(), true);
199
200
        $this->entityManager->getConfiguration()->setSQLLogger($configurationLogger);
201
        $connection->getConfiguration()->setSQLLogger($connectionLogger);
202
203
        $this->cache->save($cacheKey, $logger);
204
    }
205
206
    private function getCacheKey(): string
207
    {
208
        if (null !== $this->cacheKey) {
209
            return $this->cacheKey;
210
        }
211
212
        $fixtureFqns = [];
213
        foreach ($this->fixtureLoader->getFixtures() as $fixture) {
214
            $fixtureFqns[] = get_class($fixture);
215
        }
216
217
        return md5(print_r($fixtureFqns, true));
218
    }
219
220
    private function getLogger(): SQLLogger
221
    {
222
        return new QueryCachingLogger();
223
    }
224
225
    public function clearFixtures(): void
226
    {
227
        $this->fixtureLoader = new Loader();
228
    }
229
230
    /**
231
     * @return bool
232
     */
233
    public function isLoadedFromCache(): bool
234
    {
235
        return $this->loadedFromCache;
236
    }
237
238
    /**
239
     * @param bool $loadFromCache
240
     *
241
     * @return FixturesHelper
242
     */
243
    public function setLoadFromCache(bool $loadFromCache): FixturesHelper
244
    {
245
        $this->loadFromCache = $loadFromCache;
246
247
        return $this;
248
    }
249
}
250