AbstractAliceContext   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 24
c 4
b 0
f 1
lcom 1
cbo 6
dl 0
loc 219
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A thereAreFixtures() 0 4 1
A thereAreSeveralFixtures() 0 10 2
C loadFixtures() 0 54 10
A __construct() 0 4 1
A init() 0 16 2
setKernel() 0 1 ?
A setBasePath() 0 6 1
A getBasePath() 0 4 1
A castServiceIdToService() 0 4 1
A castServiceIdToPersister() 0 6 1
A resolvePersister() 0 19 4
1
<?php
2
3
/*
4
 * This file is part of the Fidry\AliceBundleExtension package.
5
 *
6
 * (c) Théo FIDRY <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Fidry\AliceBundleExtension\Context\Doctrine;
13
14
use Behat\Gherkin\Node\TableNode;
15
use Behat\Symfony2Extension\Context\KernelAwareContext;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Fidry\AliceBundleExtension\Context\AliceContextInterface;
18
use Hautelook\AliceBundle\Alice\DataFixtures\LoaderInterface;
19
use Hautelook\AliceBundle\Finder\FixturesFinderInterface;
20
use Nelmio\Alice\Persister\Doctrine;
21
use Nelmio\Alice\PersisterInterface;
22
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
23
use Symfony\Component\HttpKernel\KernelInterface;
24
25
/**
26
 * Context to load fixtures files with Alice loader.
27
 *
28
 * @author Théo FIDRY <[email protected]>
29
 */
30
abstract class AbstractAliceContext implements KernelAwareContext, AliceContextInterface
31
{
32
    /**
33
     * @var string
34
     */
35
    protected $basePath;
36
37
    /**
38
     * @var string[]
39
     */
40
    protected $classes;
41
42
    /**
43
     * @var FixturesFinderInterface
44
     */
45
    protected $fixturesFinder;
46
47
    /**
48
     * @var KernelInterface
49
     */
50
    protected $kernel;
51
52
    /**
53
     * @var LoaderInterface
54
     */
55
    protected $loader;
56
57
    /**
58
     * @var PersisterInterface
59
     */
60
    protected $persister;
61
62
    /**
63
     * @param string|null $basePath
64
     */
65
    public function __construct($basePath = null)
66
    {
67
        $this->basePath = $basePath;
68
    }
69
70
    /**
71
     * @param KernelInterface                  $kernel
72
     * @param FixturesFinderInterface          $fixturesFinder
73
     * @param LoaderInterface                  $loader
74
     * @param PersisterInterface|ObjectManager $persister
75
     * @param string                           $basePath
76
     */
77
    final public function init(
78
        KernelInterface $kernel,
79
        FixturesFinderInterface $fixturesFinder,
80
        LoaderInterface $loader,
81
        PersisterInterface $persister,
82
        $basePath = null
83
    ) {
84
        $this->kernel = $kernel;
85
        $this->fixturesFinder = $fixturesFinder;
86
        $this->loader = $loader;
87
        $this->persister = $persister;
88
89
        if (null !== $basePath) {
90
            $this->basePath = $basePath;
91
        }
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     *
97
     * @return $this
98
     */
99
    abstract public function setKernel(KernelInterface $kernel);
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    final public function setBasePath($basePath)
105
    {
106
        $this->basePath = $basePath;
107
108
        return $this;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    final public function getBasePath()
115
    {
116
        return $this->basePath;
117
    }
118
119
    /**
120
     * @Transform /^service$/
121
     *
122
     * @throws ServiceNotFoundException
123
     */
124
    public function castServiceIdToService($serviceId)
125
    {
126
        return $this->kernel->getContainer()->get($serviceId);
127
    }
128
129
    /**
130
     * @Transform /^persister$/
131
     *
132
     * @throws ServiceNotFoundException
133
     */
134
    public function castServiceIdToPersister($serviceId)
135
    {
136
        $service = $this->castServiceIdToService($serviceId);
137
138
        return $this->resolvePersister($service);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function thereAreFixtures($fixturesFile, $persister = null)
145
    {
146
        $this->loadFixtures([$fixturesFile], $persister);
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function thereAreSeveralFixtures(TableNode $fixturesFileRows, $persister = null)
153
    {
154
        $fixturesFiles = [];
155
156
        foreach ($fixturesFileRows->getRows() as $fixturesFileRow) {
157
            $fixturesFiles[] = $fixturesFileRow[0];
158
        }
159
160
        $this->loadFixtures($fixturesFiles, $persister);
161
    }
162
163
    /**
164
     * @param array              $fixturesFiles
165
     * @param PersisterInterface $persister
166
     */
167
    private function loadFixtures($fixturesFiles, $persister = null)
168
    {
169
        if (null === $persister) {
170
            $persister = $this->persister;
171
        }
172
173
        if (true === is_string($persister)) {
174
            $persister = $this->castServiceIdToPersister($persister);
175
        }
176
177
        $fixtureBundles = [];
178
        $fixtureDirectories = [];
179
180
        foreach ($fixturesFiles as $key => $fixturesFile) {
181
            if (0 === strpos($fixturesFile, '/')) {
182
                if (is_dir($fixturesFile)) {
183
                    $fixtureDirectories[] = $fixturesFile;
184
                    unset($fixturesFiles[$key]);
185
                }
186
187
                continue;
188
            }
189
190
            if (0 === strpos($fixturesFile, '@')) {
191
                if (false === strpos($fixturesFile, '.')) {
192
                    $fixtureBundles[] = $this->kernel->getBundle(substr($fixturesFile, 1));
193
                    unset($fixturesFiles[$key]);
194
                }
195
196
                continue;
197
            }
198
199
            $fixturesFiles[$key] = sprintf('%s/%s', $this->basePath, $fixturesFile);
200
        }
201
202
        if (false === empty($fixtureBundles)) {
203
            $fixturesFiles = array_merge(
204
                $fixturesFiles,
205
                $this->fixturesFinder->getFixtures($this->kernel, $fixtureBundles, $this->kernel->getEnvironment())
206
            );
207
        }
208
209
        if (false === empty($fixtureDirectories)) {
210
            $fixturesFiles = array_merge(
211
                $fixturesFiles,
212
                $this->fixturesFinder->getFixturesFromDirectory($fixtureDirectories)
0 ignored issues
show
Documentation introduced by
$fixtureDirectories is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
213
            );
214
        }
215
216
        $this->loader->load(
217
            $persister,
218
            $this->fixturesFinder->resolveFixtures($this->kernel, $fixturesFiles)
219
        );
220
    }
221
222
    /**
223
     * @param Doctrine|PersisterInterface|null $persister
224
     *
225
     * @return PersisterInterface
226
     *
227
     * @throws \InvalidArgumentException
228
     */
229
    final protected function resolvePersister($persister)
230
    {
231
        if (null === $persister) {
232
            return $this->persister;
233
        }
234
235
        switch (true) {
236
            case $persister instanceof PersisterInterface:
237
                return $persister;
238
            case $persister instanceof ObjectManager:
239
                return new Doctrine($persister);
240
241
            default:
242
                throw new \InvalidArgumentException(sprintf(
243
                    'Invalid persister type, expected Nelmio\Alice\PersisterInterface or Doctrine\Common\Persistence\ObjectManager. Got %s instead.',
244
                    get_class($persister)
245
                ));
246
        }
247
    }
248
}
249