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
33:02
created

FixturesHelper   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 207
ccs 0
cts 68
cp 0
rs 10
c 0
b 0
f 0
wmc 17

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setCacheKey() 0 3 1
A setLoadFromCache() 0 5 1
A createFixture() 0 10 1
A getCacheKey() 0 7 2
A run() 0 15 3
A getLogger() 0 3 1
A __construct() 0 25 1
A addFixture() 0 3 1
A isLoadedFromCache() 0 3 1
A createFixtureInstanceForEntityFqn() 0 7 1
A clearFixtures() 0 3 1
A createDb() 0 14 3
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->container,
138
            $modifier
139
        );
140
    }
141
142
    /**
143
     * Clean the DB and insert fixtures
144
     *
145
     * You can pass in a fixture loader directly which will be appended to the main fixtureLoader, or you can add
146
     * fixtures prior to calling this method
147
     *
148
     * We do not use the purge functionality of the `doctrine/data-fixtures` module, instead we fully drop and create
149
     * the database.
150
     *
151
     * @param FixtureInterface $fixture
152
     *
153
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
154
     */
155
    public function createDb(?FixtureInterface $fixture = null): void
156
    {
157
        if (null !== $fixture) {
158
            $this->addFixture($fixture);
159
        } elseif ([] === $this->fixtureLoader->getFixtures()) {
160
            throw new \RuntimeException(
161
                'No fixtures have been set.'
162
                . 'You need to either pass in a Fixture to this method, or have called `addFixture` at least once '
163
                . 'before calling this method'
164
            );
165
        }
166
        $this->database->drop(true)->create(true);
167
        $this->schema->create();
168
        $this->run();
169
    }
170
171
    public function addFixture(FixtureInterface $fixture): void
172
    {
173
        $this->fixtureLoader->addFixture($fixture);
174
    }
175
176
    public function run(): void
177
    {
178
        $cacheKey = $this->getCacheKey();
179
        if ($this->loadFromCache && $this->cache->contains($cacheKey)) {
180
            $logger = $this->cache->fetch($cacheKey);
181
            $logger->run($this->entityManager->getConnection());
182
            $this->loadedFromCache = true;
183
184
            return;
185
        }
186
        $logger = $this->getLogger();
187
        $this->entityManager->getConfiguration()->setSQLLogger($logger);
188
        $this->fixtureExecutor->execute($this->fixtureLoader->getFixtures(), true);
189
        $this->entityManager->getConfiguration()->setSQLLogger(null);
190
        $this->cache->save($cacheKey, $logger);
191
    }
192
193
    private function getCacheKey(): string
194
    {
195
        if (null !== $this->cacheKey) {
196
            return $this->cacheKey;
197
        }
198
199
        return md5(print_r(array_keys($this->fixtureLoader->getFixtures()), true));
200
    }
201
202
    private function getLogger(): SQLLogger
203
    {
204
        return new QueryCachingLogger();
205
    }
206
207
    public function clearFixtures(): void
208
    {
209
        $this->fixtureLoader = new Loader();
210
    }
211
212
    /**
213
     * @return bool
214
     */
215
    public function isLoadedFromCache(): bool
216
    {
217
        return $this->loadedFromCache;
218
    }
219
220
    /**
221
     * @param bool $loadFromCache
222
     *
223
     * @return FixturesHelper
224
     */
225
    public function setLoadFromCache(bool $loadFromCache): FixturesHelper
226
    {
227
        $this->loadFromCache = $loadFromCache;
228
229
        return $this;
230
    }
231
}
232