Passed
Push — master ( 36097f...93304f )
by Rafael
03:14
created

SQLiteORMExecutor::purge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 2.1481
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Test\FixtureLoader\DataPopulator;
12
13
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
14
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
15
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Symfony\Component\HttpFoundation\File\File;
18
19
/**
20
 * This executor save serialized fixture references and copy
21
 * of database using a unique hash for fixtures and schema
22
 */
23
class SQLiteORMExecutor extends ORMExecutor
24
{
25
    private $database;
26
27
    protected $cacheDir;
28
29 22
    public function __construct(EntityManagerInterface $em, ORMPurger $purger = null, $cacheDir = null)
30
    {
31 22
        parent::__construct($em, $purger);
32
33 22
        $this->cacheDir = $cacheDir;
34
35 22
        $params = $em->getConnection()->getParams();
36 22
        if (isset($params['master'])) {
37
            $params = $params['master'];
38
        }
39
40 22
        $this->database = $params['path'] ?? ($params['dbname'] ?? null);
41 22
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46 22
    public function purge()
47
    {
48 22
        if (!$this->cacheDir) {
49
            parent::purge();
50
        }
51 22
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56 22
    public function execute(array $fixtures, $append = false)
57
    {
58 22
        $repo = $this->getReferenceRepository();
59
60 22
        if ($this->cacheDir && $repo instanceof ProxyReferenceRepository) {
61 22
            static $hash = null;
62 22
            if (!$hash) {
63 1
                $hash = $this->buildHash($fixtures);
64
            }
65
66 22
            if ($repo->load($this->getDataCacheFile($hash))) {
67 21
                copy($this->getDataCacheFile($hash), $this->database);
68
69 21
                return;
70
            }
71
        }
72
73 1
        parent::execute($fixtures, $append);
74
75 1
        if (isset($hash)) {
76 1
            $repo->save($this->getDataCacheFile($hash));
0 ignored issues
show
introduced by
The method save() does not exist on Doctrine\Common\DataFixtures\ReferenceRepository. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
            $repo->/** @scrutinizer ignore-call */ 
77
                   save($this->getDataCacheFile($hash));
Loading history...
77
78 1
            copy($this->database, $this->getDataCacheFile($hash));
79
        }
80 1
    }
81
82 1
    protected function buildHash($fixtures): string
83
    {
84 1
        $om = $this->getObjectManager();
85 1
        $metadata = $om->getMetadataFactory()->getAllMetadata();
86 1
        $hash = md5(serialize($metadata));
87
88 1
        foreach ($fixtures as $fixture) {
89 1
            $ref = new \ReflectionClass(get_class($fixture));
90 1
            $file = new File($ref->getFileName());
91 1
            $hash .= $file->getMTime();
92
        }
93
94 1
        return md5($hash);
95
    }
96
97 22
    public function getDataCacheFile($hash): string
98
    {
99 22
        return $this->cacheDir.DIRECTORY_SEPARATOR.$hash.'.data';
100
    }
101
}
102