Passed
Push — master ( 62c073...480444 )
by Dan
05:23
created

Loader::createFilesLoader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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