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 (#154)
by joseph
22:43
created

FixturesHelper::createDb()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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