Completed
Pull Request — master (#15)
by Eric
02:08
created

AbstractAliceContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
            } elseif (0 === strpos($fixturesFile, '@')) {
187
                if (false === strpos($fixturesFile, '.')) {
188
                    $fixtureBundles[] = $this->kernel->getBundle(substr($fixturesFile, 1));
189
                    unset($fixturesFiles[$key]);
190
                }
191
            } else {
192
                $fixturesFiles[$key] = sprintf('%s/%s', $this->basePath, $fixturesFile);
193
            }
194
        }
195
196
        if (!empty($fixtureBundles)) {
197
            $fixturesFiles = array_merge(
198
                $fixturesFiles,
199
                $this->fixturesFinder->getFixtures($this->kernel, $fixtureBundles, null)
200
            );
201
        }
202
203
        if (!empty($fixtureDirectories)) {
204
            $fixturesFiles = array_merge(
205
                $fixturesFiles,
206
                $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...
207
            );
208
        }
209
210
        $this->loader->load(
211
            $persister,
212
            $this->fixturesFinder->resolveFixtures($this->kernel, $fixturesFiles)
213
        );
214
    }
215
216
    /**
217
     * @param Doctrine|PersisterInterface|null $persister
218
     *
219
     * @return PersisterInterface
220
     *
221
     * @throws \InvalidArgumentException
222
     */
223
    final protected function resolvePersister($persister)
224
    {
225
        if (null === $persister) {
226
            return $this->persister;
227
        }
228
229
        switch (true) {
230
            case $persister instanceof PersisterInterface:
231
                return $persister;
232
            case $persister instanceof ObjectManager:
233
                return new Doctrine($persister);
234
235
            default:
236
                throw new \InvalidArgumentException(sprintf(
237
                    'Invalid persister type, expected Nelmio\Alice\PersisterInterface or Doctrine\Common\Persistence\ObjectManager. Got %s instead.',
238
                    get_class($persister)
239
                ));
240
        }
241
    }
242
}
243