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 ( d68f0e...658fac )
by Ross
14s queued 12s
created

FixturesHelper::isLoadedFromCache()   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 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
        if ($this->loadFromCache && $this->cache->contains($cacheKey)) {
181
            $logger = $this->cache->fetch($cacheKey);
182
            $logger->run($this->entityManager->getConnection());
183
            $this->loadedFromCache = true;
184
185
            return;
186
        }
187
        $logger = $this->getLogger();
188
        $this->entityManager->getConfiguration()->setSQLLogger($logger);
189
        $this->fixtureExecutor->execute($this->fixtureLoader->getFixtures(), true);
190
        $this->entityManager->getConfiguration()->setSQLLogger(null);
191
        $this->cache->save($cacheKey, $logger);
192
    }
193
194
    private function getCacheKey(): string
195
    {
196
        if (null !== $this->cacheKey) {
197
            return $this->cacheKey;
198
        }
199
200
        $fixtureFqns = [];
201
        foreach ($this->fixtureLoader->getFixtures() as $fixture) {
202
            $fixtureFqns[] = get_class($fixture);
203
        }
204
205
        return md5(print_r($fixtureFqns, true));
206
    }
207
208
    private function getLogger(): SQLLogger
209
    {
210
        return new QueryCachingLogger();
211
    }
212
213
    public function clearFixtures(): void
214
    {
215
        $this->fixtureLoader = new Loader();
216
    }
217
218
    /**
219
     * @return bool
220
     */
221
    public function isLoadedFromCache(): bool
222
    {
223
        return $this->loadedFromCache;
224
    }
225
226
    /**
227
     * @param bool $loadFromCache
228
     *
229
     * @return FixturesHelper
230
     */
231
    public function setLoadFromCache(bool $loadFromCache): FixturesHelper
232
    {
233
        $this->loadFromCache = $loadFromCache;
234
235
        return $this;
236
    }
237
}
238