Loader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Knp\FriendlyContexts\Alice\Fixtures\Alice3;
4
5
use Knp\FriendlyContexts\Alice\Fixtures\Loader as LoaderInterface;
6
use Nelmio\Alice\DataLoaderInterface;
7
use Nelmio\Alice\Definition\Property;
8
use Nelmio\Alice\FilesLoaderInterface;
9
use Nelmio\Alice\FixtureInterface;
10
use Nelmio\Alice\FixtureSet;
11
use Nelmio\Alice\Loader\NativeLoader;
12
use Nelmio\Alice\Loader\SimpleFilesLoader;
13
use Nelmio\Alice\Throwable\Exception\ObjectNotFoundException;
14
15
class Loader extends NativeLoader implements
16
    LoaderInterface
17
{
18
    /**
19
     * @var null|FixtureSet
20
     */
21
    private $fixtureSet = null;
22
    private $objects = null;
23
    private $loader;
24
    private $dataLoader;
25
26
    public function __construct()
27
    {
28
        parent::__construct();
29
30
        $this->loader = $this->createFilesLoader();
31
    }
32
33
    public function getCache()
34
    {
35
        if (null === $this->fixtureSet) {
36
            return array();
37
        }
38
39
        $cache = array();
40
        $fixtures = $this->fixtureSet->getFixtures();
41
        /** @var FixtureInterface $fixture */
42
        foreach ($fixtures as $fixture) {
43
            $spec = array();
44
45
            $properties = $fixture->getSpecs()->getProperties();
46
            /** @var Property $property */
47
            foreach ($properties->getIterator() as $property) {
48
                $spec[ $property->getName() ] = $property->getValue();
49
            }
50
51
            $cache[] = array($spec, $this->objects[$fixture->getId()]);
52
        }
53
54
        return $cache;
55
    }
56
57
    public function clearCache()
58
    {
59
        $this->fixtureSet = null;
60
        $this->objects = null;
61
    }
62
63
    public function load($filename)
64
    {
65
        if ( ! is_array($filename)) {
66
            $filename = array($filename);
67
        }
68
        $this->objects = $this->loader->loadFiles($filename)->getObjects();
69
        $this->fixtureSet = $this->dataLoader->getLastFixtureSet();
70
        return $this->objects;
71
    }
72
73
    protected function createDataLoader() : DataLoaderInterface
74
    {
75
        return new DataLoader(
76
            $this->getFixtureBuilder(),
77
            $this->getGenerator()
78
        );
79
    }
80
81
    protected function createFilesLoader(): FilesLoaderInterface
82
    {
83
        if (null === $this->dataLoader) {
84
            $this->dataLoader = $this->createDataLoader();
85
        }
86
87
        return new SimpleFilesLoader(
88
            $this->getParser(),
89
            $this->dataLoader
0 ignored issues
show
Bug introduced by
It seems like $this->dataLoader can also be of type mixed; however, parameter $dataLoader of Nelmio\Alice\Loader\Simp...esLoader::__construct() does only seem to accept Nelmio\Alice\DataLoaderInterface, 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

89
            /** @scrutinizer ignore-type */ $this->dataLoader
Loading history...
90
        );
91
    }
92
}
93