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 (#109)
by joseph
33:02
created

FixturesHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Cache\FilesystemCache;
7
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
8
use Doctrine\Common\DataFixtures\FixtureInterface;
9
use Doctrine\Common\DataFixtures\Loader;
10
use Doctrine\DBAL\Logging\SQLLogger;
11
use Doctrine\ORM\EntityManagerInterface;
12
use EdmondsCommerce\DoctrineStaticMeta\Schema\Database;
13
use EdmondsCommerce\DoctrineStaticMeta\Schema\Schema;
14
15
/**
16
 * To be used in your Test classes. This provides you with the methods to use in your setup method to create the
17
 * fixtures are required
18
 *
19
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20
 */
21
class FixturesHelper
22
{
23
    /**
24
     * @var Database
25
     */
26
    protected $database;
27
    /**
28
     * @var Schema
29
     */
30
    protected $schema;
31
    /**
32
     * @var EntityManagerInterface
33
     */
34
    protected $entityManager;
35
    /**
36
     * @var Cache
37
     */
38
    protected $cache;
39
    /**
40
     * @var null|string
41
     */
42
    protected $cacheKey;
43
    /**
44
     * @var ORMExecutor
45
     */
46
    private $fixtureExecutor;
47
48
    /**
49
     * @var Loader
50
     */
51
    private $fixtureLoader;
52
53
    /**
54
     * @var bool
55
     */
56
    private $loadedFromCache = false;
57
58 3
    public function __construct(
59
        EntityManagerInterface $entityManager,
60
        Database $database,
61
        Schema $schema,
62
        FilesystemCache $cache,
63
        ?string $cacheKey = null
64
    ) {
65 3
        $purger                = null;
66 3
        $this->fixtureExecutor = new ORMExecutor($entityManager, $purger);
67 3
        $this->fixtureLoader   = new Loader();
68 3
        $this->database        = $database;
69 3
        $this->schema          = $schema;
70 3
        $this->entityManager   = $entityManager;
71 3
        $this->cache           = $cache;
72 3
        $this->cacheKey        = $cacheKey;
73 3
    }
74
75
    /**
76
     * @param null|string $cacheKey
77
     */
78 3
    public function setCacheKey(?string $cacheKey): void
79
    {
80 3
        $this->cacheKey = $cacheKey;
81 3
    }
82
83
84
85 3
    private function getCacheKey(): string
86
    {
87 3
        if (null !== $this->cacheKey) {
88 3
            return $this->cacheKey;
89
        }
90
91
        return md5(print_r(array_keys($this->fixtureLoader->getFixtures()), true));
92
    }
93
94 3
    public function addFixture(FixtureInterface $fixture): void
95
    {
96 3
        $this->fixtureLoader->addFixture($fixture);
97 3
    }
98
99
    /**
100
     * Clean the DB and insert fixtures
101
     *
102
     * You can pass in a fixture loader directly which will be appended to the main fixtureLoader, or you can add
103
     * fixtures prior to calling this method
104
     *
105
     * We do not use the purge functionality of the `doctrine/data-fixtures` module, instead we fully drop and create
106
     * the database.
107
     *
108
     * @param FixtureInterface $fixture
109
     *
110
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
111
     */
112 3
    public function createDb(?FixtureInterface $fixture = null): void
113
    {
114 3
        if (null !== $fixture) {
115
            $this->addFixture($fixture);
116 3
        } elseif ([] === $this->fixtureLoader->getFixtures()) {
117
            throw new \RuntimeException(
118
                'No fixtures have been set.'
119
                . 'You need to either pass in a Fixture to this method, or have called `addFixture` at least once '
120
                . 'before calling this method'
121
            );
122
        }
123 3
        $this->database->drop(true)->create(true);
124 3
        $cacheKey = $this->getCacheKey();
125 3
        if ($this->cache->contains($cacheKey)) {
126 1
            $logger = $this->cache->fetch($cacheKey);
127 1
            $logger->run($this->entityManager->getConnection());
128 1
            $this->loadedFromCache = true;
129
130 1
            return;
131
        }
132 2
        $logger = $this->getLogger();
133 2
        $this->entityManager->getConfiguration()->setSQLLogger($logger);
134 2
        $this->schema->create();
135 2
        $this->fixtureExecutor->execute($this->fixtureLoader->getFixtures(), true);
136 2
        $this->entityManager->getConfiguration()->setSQLLogger(null);
137 2
        $this->cache->save($cacheKey, $logger);
138 2
    }
139
140 2
    private function getLogger(): SQLLogger
141
    {
142 2
        return new Logger();
143
    }
144
145
    /**
146
     * @return bool
147
     */
148 1
    public function isLoadedFromCache(): bool
149
    {
150 1
        return $this->loadedFromCache;
151
    }
152
}
153