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

FixtureLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1
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;
12
13
use Doctrine\Bundle\DoctrineBundle\Registry;
14
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
15
use Doctrine\Common\DataFixtures\Loader;
16
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
17
use Doctrine\Common\DataFixtures\ReferenceRepository;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Ynlo\GraphQLBundle\Test\FixtureLoader\DataPopulator\DataLoaderInterface;
20
use Ynlo\GraphQLBundle\Test\FixtureLoader\DataPopulator\ORMDataLoader;
21
use Ynlo\GraphQLBundle\Test\FixtureLoader\SchemaUpdater\ORMSQLite;
22
use Ynlo\GraphQLBundle\Test\FixtureLoader\SchemaUpdater\SchemaUpdaterInterface;
23
24
/**
25
 * Class FixtureLoader
26
 */
27
class FixtureLoader
28
{
29
    /**
30
     * @var ContainerInterface
31
     */
32
    protected $container;
33
34
    /**
35
     * @var Registry
36
     */
37
    protected $registry;
38
39
    /**
40
     * @var array[]
41
     */
42
    protected $plugins = [];
43
44
    /**
45
     * @var bool
46
     */
47
    protected static $schemaUpdated = false;
48
49
    /**
50
     * FixtureLoader constructor.
51
     *
52
     * @param ContainerInterface $container
53
     * @param Registry           $registry
54
     * @param array              $plugins
55
     */
56 22
    public function __construct(ContainerInterface $container, Registry $registry, $plugins = [])
57
    {
58 22
        $this->registry = $registry;
59 22
        $this->container = $container;
60
61 22
        $this->plugins = $plugins;
62
63 22
        $cacheDir = $container->getParameter('kernel.cache_dir').DIRECTORY_SEPARATOR.'tests';
64 22
        $container->get('filesystem')->mkdir($cacheDir);
65
66 22
        $this->plugins[] = new ORMSQLite($cacheDir);
67 22
        $this->plugins[] = new ORMDataLoader($cacheDir);
68 22
    }
69
70
    /**
71
     * @param array $classNames
72
     * @param bool  $append
73
     *
74
     * @return ReferenceRepository
75
     */
76 22
    public function loadFixtures($classNames = [], $append = false): ReferenceRepository
77
    {
78 22
        if (!self::$schemaUpdated) {
79 1
            foreach ($this->plugins as $plugin) {
80 1
                if ($plugin instanceof SchemaUpdaterInterface) {
81 1
                    if ($plugin->supports($this->registry)) {
82 1
                        $plugin->updateSchema($this->registry);
83 1
                        self::$schemaUpdated = true;
84 1
                        break;
85
                    }
86
                }
87
            }
88
        }
89
90 22
        $loader = $this->getFixtureLoader($this->container, $classNames);
91 22
        $fixtures = $loader->getFixtures();
92 22
        $referenceRepository = new ProxyReferenceRepository($this->registry->getManager());
93 22
        foreach ($this->plugins as $plugin) {
94 22
            if ($plugin instanceof DataLoaderInterface) {
95 22
                if ($plugin->supports($this->registry)) {
96 22
                    $executor = $plugin->createExecutor($this->registry);
97 22
                    $executor->purge();
98 22
                    $executor->setReferenceRepository($referenceRepository);
99 22
                    $executor->execute($fixtures, $append);
100 22
                    break;
101
                }
102
            }
103
        }
104
105 22
        return $referenceRepository;
106
    }
107
108
    /**
109
     * Retrieve Doctrine DataFixtures loader.
110
     *
111
     * @param ContainerInterface $container
112
     * @param array              $classNames
113
     *
114
     * @return Loader
115
     */
116 22
    protected function getFixtureLoader(ContainerInterface $container, array $classNames)
117
    {
118 22
        $loaderClass = class_exists('Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader')
119 22
            ? 'Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader'
120
            : (class_exists('Doctrine\Bundle\FixturesBundle\Common\DataFixtures\Loader')
121
                // This class is not available during tests.
122
                // @codeCoverageIgnoreStart
123
                ? 'Doctrine\Bundle\FixturesBundle\Common\DataFixtures\Loader'
124
                // @codeCoverageIgnoreEnd
125 22
                : 'Symfony\Bundle\DoctrineFixturesBundle\Common\DataFixtures\Loader');
126
127
        /** @var Loader $loader */
128 22
        $loader = new $loaderClass($container);
129
130 22
        if ($classNames) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $classNames of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
introduced by
The condition $classNames can never be true.
Loading history...
131
            foreach ($classNames as $className) {
132
                $this->loadFixtureClass($loader, $className);
133
            }
134
        } else {
135 22
            $bundles = $container->get('kernel')->getBundles();
136 22
            foreach ($bundles as $bundle) {
137 22
                $dir = $bundle->getPath().'/DataFixtures';
138 22
                if (file_exists($dir)) {
139 22
                    $loader->loadFromDirectory($dir);
140
                }
141
            }
142
        }
143
144 22
        return $loader;
145
    }
146
147
    /**
148
     * Load a data fixture class.
149
     *
150
     * @param Loader $loader
151
     * @param string $className
152
     */
153
    protected function loadFixtureClass($loader, $className)
154
    {
155
        $fixture = new $className();
156
157
        if ($loader->hasFixture($fixture)) {
158
            unset($fixture);
159
160
            return;
161
        }
162
163
        $loader->addFixture($fixture);
164
165
        if ($fixture instanceof DependentFixtureInterface) {
166
            foreach ($fixture->getDependencies() as $dependency) {
167
                $this->loadFixtureClass($loader, $dependency);
168
            }
169
        }
170
    }
171
}
172