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
19:10
created

createFixtureInstanceForEntityFqn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 11
ccs 0
cts 10
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\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
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
    public function __construct(
82
        EntityManagerInterface $entityManager,
83
        Database $database,
84
        Schema $schema,
85
        FilesystemCache $cache,
86
        EntitySaverFactory $entitySaverFactory,
87
        NamespaceHelper $namespaceHelper,
88
        TestEntityGeneratorFactory $testEntityGeneratorFactory,
89
        ?string $cacheKey = null
90
    ) {
91
        $purger                           = null;
92
        $this->fixtureExecutor            = new ORMExecutor($entityManager, $purger);
93
        $this->fixtureLoader              = new Loader();
94
        $this->database                   = $database;
95
        $this->schema                     = $schema;
96
        $this->entityManager              = $entityManager;
97
        $this->cache                      = $cache;
98
        $this->entitySaverFactory         = $entitySaverFactory;
99
        $this->namespaceHelper            = $namespaceHelper;
100
        $this->testEntityGeneratorFactory = $testEntityGeneratorFactory;
101
        $this->cacheKey                   = $cacheKey;
102
    }
103
104
    /**
105
     * @param null|string $cacheKey
106
     */
107
    public function setCacheKey(?string $cacheKey): void
108
    {
109
        $this->cacheKey = $cacheKey;
110
    }
111
112
    public function createFixtureInstanceForEntityFqn(
113
        string $entityFqn,
114
        FixtureEntitiesModifierInterface $modifier = null
115
    ): AbstractEntityFixtureLoader {
116
        $fixtureFqn = $this->namespaceHelper->getFixtureFqnFromEntityFqn($entityFqn);
117
118
        return new $fixtureFqn(
119
            $this->testEntityGeneratorFactory,
120
            $this->entitySaverFactory,
121
            $this->namespaceHelper,
122
            $modifier
123
        );
124
    }
125
126
    /**
127
     * Clean the DB and insert fixtures
128
     *
129
     * You can pass in a fixture loader directly which will be appended to the main fixtureLoader, or you can add
130
     * fixtures prior to calling this method
131
     *
132
     * We do not use the purge functionality of the `doctrine/data-fixtures` module, instead we fully drop and create
133
     * the database.
134
     *
135
     * @param FixtureInterface $fixture
136
     *
137
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
138
     */
139
    public function createDb(?FixtureInterface $fixture = null): void
140
    {
141
        if (null !== $fixture) {
142
            $this->addFixture($fixture);
143
        } elseif ([] === $this->fixtureLoader->getFixtures()) {
144
            throw new \RuntimeException(
145
                'No fixtures have been set.'
146
                . 'You need to either pass in a Fixture to this method, or have called `addFixture` at least once '
147
                . 'before calling this method'
148
            );
149
        }
150
        $this->database->drop(true)->create(true);
151
        $cacheKey = $this->getCacheKey();
152
        if ($this->loadFromCache && $this->cache->contains($cacheKey)) {
153
            $logger = $this->cache->fetch($cacheKey);
154
            $logger->run($this->entityManager->getConnection());
155
            $this->loadedFromCache = true;
156
157
            return;
158
        }
159
        $logger = $this->getLogger();
160
        $this->entityManager->getConfiguration()->setSQLLogger($logger);
161
        $this->schema->create();
162
        $this->fixtureExecutor->execute($this->fixtureLoader->getFixtures(), true);
163
        $this->entityManager->getConfiguration()->setSQLLogger(null);
164
        $this->cache->save($cacheKey, $logger);
165
    }
166
167
    public function addFixture(FixtureInterface $fixture): void
168
    {
169
        $this->fixtureLoader->addFixture($fixture);
170
    }
171
172
    private function getCacheKey(): string
173
    {
174
        if (null !== $this->cacheKey) {
175
            return $this->cacheKey;
176
        }
177
178
        return md5(print_r(array_keys($this->fixtureLoader->getFixtures()), true));
179
    }
180
181
    private function getLogger(): SQLLogger
182
    {
183
        return new QueryCachingLogger();
184
    }
185
186
    /**
187
     * @return bool
188
     */
189
    public function isLoadedFromCache(): bool
190
    {
191
        return $this->loadedFromCache;
192
    }
193
194
    /**
195
     * @param bool $loadFromCache
196
     *
197
     * @return FixturesHelper
198
     */
199
    public function setLoadFromCache(bool $loadFromCache): FixturesHelper
200
    {
201
        $this->loadFromCache = $loadFromCache;
202
203
        return $this;
204
    }
205
}
206