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 Ross
22:04
created

FixturesHelper   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 64
dl 0
loc 198
ccs 0
cts 65
cp 0
rs 10
c 0
b 0
f 0
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setCacheKey() 0 3 1
A __construct() 0 23 1
A addFixture() 0 3 1
A createFixtureInstanceForEntityFqn() 0 12 1
A createDb() 0 14 3
A setLoadFromCache() 0 5 1
A run() 0 15 3
A getCacheKey() 0 7 2
A getLogger() 0 3 1
A isLoadedFromCache() 0 3 1
A clearFixtures() 0 3 1
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->schema->create();
160
        $this->run();
161
    }
162
163
    public function addFixture(FixtureInterface $fixture): void
164
    {
165
        $this->fixtureLoader->addFixture($fixture);
166
    }
167
168
    public function clearFixtures(): void
169
    {
170
        $this->fixtureLoader = new Loader();
171
    }
172
173
    public function run(): void
174
    {
175
        $cacheKey = $this->getCacheKey();
176
        if ($this->loadFromCache && $this->cache->contains($cacheKey)) {
177
            $logger = $this->cache->fetch($cacheKey);
178
            $logger->run($this->entityManager->getConnection());
179
            $this->loadedFromCache = true;
180
181
            return;
182
        }
183
        $logger = $this->getLogger();
184
        $this->entityManager->getConfiguration()->setSQLLogger($logger);
185
        $this->fixtureExecutor->execute($this->fixtureLoader->getFixtures(), true);
186
        $this->entityManager->getConfiguration()->setSQLLogger(null);
187
        $this->cache->save($cacheKey, $logger);
188
    }
189
190
    private function getCacheKey(): string
191
    {
192
        if (null !== $this->cacheKey) {
193
            return $this->cacheKey;
194
        }
195
196
        return md5(print_r(array_keys($this->fixtureLoader->getFixtures()), true));
197
    }
198
199
    private function getLogger(): SQLLogger
200
    {
201
        return new QueryCachingLogger();
202
    }
203
204
    /**
205
     * @return bool
206
     */
207
    public function isLoadedFromCache(): bool
208
    {
209
        return $this->loadedFromCache;
210
    }
211
212
    /**
213
     * @param bool $loadFromCache
214
     *
215
     * @return FixturesHelper
216
     */
217
    public function setLoadFromCache(bool $loadFromCache): FixturesHelper
218
    {
219
        $this->loadFromCache = $loadFromCache;
220
221
        return $this;
222
    }
223
}
224