FileFixture   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 13
c 1
b 0
f 0
dl 0
loc 54
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 11 2
A __construct() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[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
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\AppBundle\Fixture;
17
18
use Doctrine\Bundle\FixturesBundle\Fixture;
19
use Doctrine\Common\Persistence\ObjectManager;
20
use Nelmio\Alice\FileLoaderInterface;
21
use Symfony\Component\Config\FileLocatorInterface;
22
23
/**
24
 * Fixture that loads entities from file
25
 */
26
class FileFixture extends Fixture
27
{
28
    /**
29
     * Locates file by physical or logical '@' path
30
     *
31
     * @var FileLocatorInterface
32
     */
33
    private $fileLocator;
34
35
    /**
36
     * Loads a fixture files
37
     *
38
     * @var FileLoaderInterface
39
     */
40
    private $fixtureLoader;
41
42
    /**
43
     * Fixture path to load
44
     *
45
     * @var string
46
     */
47
    private $fixturePath;
48
49
    /**
50
     * FileFixture constructor.
51
     *
52
     * @param FileLocatorInterface $fileLocator   Locates file by physical or logic '@' path
53
     * @param FileLoaderInterface  $fixtureLoader Loads a fixture files
54
     * @param string               $fixturePath   Fixture path to load
55
     */
56
    public function __construct(
57
        FileLocatorInterface $fileLocator,
58
        FileLoaderInterface $fixtureLoader,
59
        string $fixturePath
60
    ) {
61
        $this->fileLocator   = $fileLocator;
62
        $this->fixtureLoader = $fixtureLoader;
63
        $this->fixturePath   = $fixturePath;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function load(ObjectManager $objectManager)
70
    {
71
        $fixtureRealPath = $this->fileLocator->locate($this->fixturePath);
72
        $objectSet       = $this->fixtureLoader->loadFile($fixtureRealPath);
0 ignored issues
show
Bug introduced by
It seems like $fixtureRealPath can also be of type array; however, parameter $file of Nelmio\Alice\FileLoaderInterface::loadFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

72
        $objectSet       = $this->fixtureLoader->loadFile(/** @scrutinizer ignore-type */ $fixtureRealPath);
Loading history...
73
        $entities        = $objectSet->getObjects();
74
75
        foreach ($entities as $entity) {
76
            $objectManager->persist($entity);
77
        }
78
79
        $objectManager->flush();
80
    }
81
}
82