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 ( 84e592...894b2c )
by joseph
127:56 queued 125:09
created

FixturesHelper::setCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
crap 1.037
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 5
    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 5
        $purger                           = null;
97 5
        $this->fixtureExecutor            = new ORMExecutor($entityManager, $purger);
98 5
        $this->fixtureLoader              = new Loader();
99 5
        $this->database                   = $database;
100 5
        $this->schema                     = $schema;
101 5
        $this->entityManager              = $entityManager;
102 5
        $this->cache                      = $cache;
103 5
        $this->entitySaverFactory         = $entitySaverFactory;
104 5
        $this->namespaceHelper            = $namespaceHelper;
105 5
        $this->testEntityGeneratorFactory = $testEntityGeneratorFactory;
106 5
        $this->cacheKey                   = $cacheKey;
107 5
        $this->container                  = $container;
108
109 5
        $this->fixtureLoader->setFixturesHelper($this);
110 5
    }
111
112
    /**
113
     * @param null|string $cacheKey
114
     */
115 5
    public function setCacheKey(?string $cacheKey): void
116
    {
117 5
        $this->cacheKey = $cacheKey;
118 5
    }
119
120 5
    public function createFixtureInstanceForEntityFqn(
121
        string $entityFqn,
122
        FixtureEntitiesModifierInterface $modifier = null
123
    ): AbstractEntityFixtureLoader {
124 5
        $fixtureFqn = $this->namespaceHelper->getFixtureFqnFromEntityFqn($entityFqn);
125
126 5
        return $this->createFixture($fixtureFqn, $modifier);
127
    }
128
129 5
    public function createFixture(
130
        string $fixtureFqn,
131
        FixtureEntitiesModifierInterface $modifier = null
132
    ): AbstractEntityFixtureLoader {
133 5
        return new $fixtureFqn(
134 5
            $this->testEntityGeneratorFactory,
135 5
            $this->entitySaverFactory,
136 5
            $this->namespaceHelper,
137 5
            $this->entityManager,
138 5
            $this->container,
139 5
            $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 5
    public function createDb(?FixtureInterface $fixture = null): void
157
    {
158 5
        if (null !== $fixture) {
159
            $this->addFixture($fixture);
160 5
        } 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 5
        $this->database->drop(true)->create(true);
168 5
        $this->schema->create();
169 5
        $this->run();
170 5
    }
171
172 5
    public function addFixture(FixtureInterface $fixture): void
173
    {
174 5
        $this->fixtureLoader->addFixture($fixture);
175 5
    }
176
177 5
    public function run(): void
178
    {
179 5
        $cacheKey = $this->getCacheKey();
180 5
        if ($this->loadFromCache && $this->cache->contains($cacheKey)) {
181 1
            $logger = $this->cache->fetch($cacheKey);
182 1
            $logger->run($this->entityManager->getConnection());
183 1
            $this->loadedFromCache = true;
184
185 1
            return;
186
        }
187 4
        $logger = $this->getLogger();
188 4
        $this->entityManager->getConfiguration()->setSQLLogger($logger);
189 4
        $this->fixtureExecutor->execute($this->fixtureLoader->getFixtures(), true);
190 4
        $this->entityManager->getConfiguration()->setSQLLogger(null);
191 4
        $this->cache->save($cacheKey, $logger);
192 4
    }
193
194 5
    private function getCacheKey(): string
195
    {
196 5
        if (null !== $this->cacheKey) {
197 5
            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 4
    private function getLogger(): SQLLogger
209
    {
210 4
        return new QueryCachingLogger();
211
    }
212
213
    public function clearFixtures(): void
214
    {
215
        $this->fixtureLoader = new Loader();
216
    }
217
218
    /**
219
     * @return bool
220
     */
221 2
    public function isLoadedFromCache(): bool
222
    {
223 2
        return $this->loadedFromCache;
224
    }
225
226
    /**
227
     * @param bool $loadFromCache
228
     *
229
     * @return FixturesHelper
230
     */
231 1
    public function setLoadFromCache(bool $loadFromCache): FixturesHelper
232
    {
233 1
        $this->loadFromCache = $loadFromCache;
234
235 1
        return $this;
236
    }
237
}
238