Passed
Pull Request — master (#11)
by Dan
04:08
created

Loader::finaliseAssociations()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 26
rs 9.0777
cc 6
nc 6
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\Fixture\FixtureId;
8
use Nelmio\Alice\Definition\Property;
9
use Nelmio\Alice\Definition\Value\FixtureReferenceValue;
10
use Nelmio\Alice\FilesLoaderInterface;
11
use Nelmio\Alice\FixtureInterface;
12
use Nelmio\Alice\FixtureSet;
13
use Nelmio\Alice\Loader\NativeLoader;
14
use Nelmio\Alice\Loader\SimpleFilesLoader;
15
use Nelmio\Alice\ObjectBag;
16
use Nelmio\Alice\Throwable\Exception\ObjectNotFoundException;
17
use Symfony\Component\PropertyAccess\PropertyAccess;
18
19
class Loader extends NativeLoader implements
20
    LoaderInterface
21
{
22
    /**
23
     * @var null|FixtureSet
24
     */
25
    private $fixtureSet = null;
26
    private $objects = null;
27
    private $loader;
28
    private $dataLoader;
29
30
    public function __construct(
31
        FilesLoaderInterface $filesLoader = null,
32
        DataLoader $dataLoader = null
33
    ) {
34
        parent::__construct();
35
36
        $this->dataLoader = $dataLoader;
37
        $this->loader = $filesLoader ?? $this->createFilesLoader();
38
    }
39
40
    public function getCache()
41
    {
42
        if (null === $this->fixtureSet) {
43
            return array();
44
        }
45
46
        $cache = array();
47
        $fixtures = $this->fixtureSet->getFixtures();
48
        /** @var FixtureInterface $fixture */
49
        foreach ($fixtures as $fixture) {
50
            $spec = array();
51
52
            $properties = $fixture->getSpecs()->getProperties();
53
            /** @var Property $property */
54
            foreach ($properties as $property) {
55
                $spec[ $property->getName() ] = $property->getValue();
56
            }
57
58
            $cache[] = array($spec, $this->objects[$fixture->getId()]);
59
        }
60
61
        return $cache;
62
    }
63
64
    public function clearCache()
65
    {
66
        $this->fixtureSet = null;
67
        $this->objects = null;
68
    }
69
70
    public function load($filename)
71
    {
72
        if ( ! is_array($filename)) {
73
            $filename = array($filename);
74
        }
75
        $this->objects = $this->loader->loadFiles($filename)->getObjects();
76
        $this->fixtureSet = $this->dataLoader->getLastFixtureSet();
0 ignored issues
show
Bug introduced by
The method getLastFixtureSet() does not exist on null. ( Ignorable by Annotation )

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

76
        /** @scrutinizer ignore-call */ 
77
        $this->fixtureSet = $this->dataLoader->getLastFixtureSet();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
78
        $this->finaliseAssociations();
79
80
        return $this->objects;
81
    }
82
83
    protected function createDataLoader() : DataLoaderInterface
84
    {
85
        return new DataLoader(
86
            $this->getFixtureBuilder(),
87
            $this->getGenerator()
88
        );
89
    }
90
91
    protected function createFilesLoader(): FilesLoaderInterface
92
    {
93
        if (null === $this->dataLoader) {
94
            $this->dataLoader = $this->createDataLoader();
95
        }
96
97
        return new SimpleFilesLoader(
98
            $this->getParser(),
99
            $this->dataLoader
100
        );
101
    }
102
103
    private function finaliseAssociations()
104
    {
105
        if ( ! $this->fixtureSet instanceof FixtureSet) {
106
            return;
107
        }
108
109
        $accessor = PropertyAccess::createPropertyAccessor();
110
        $objectBag = new ObjectBag($this->objects);
111
        $fixtureBag = $this->fixtureSet->getFixtures();
112
113
        /** @var FixtureInterface $fixture */
114
        foreach ($fixtureBag as $fixture) {
115
            $spec = $fixture->getSpecs();
116
            foreach ($spec->getProperties() as $property) {
117
                $value = $property->getValue();
118
                if ( ! $value instanceof FixtureReferenceValue) {
119
                    continue;
120
                }
121
122
                $owner = $objectBag->get($fixture)->getInstance();
123
                $target = $objectBag->get(
124
                    $fixtureBag->get($value->getValue())
125
                )->getInstance();
126
127
                if ($accessor->getValue($owner, $property->getName()) !== $target) {
128
                    $accessor->setValue($owner, $property->getName(), $target);
129
                }
130
            }
131
        }
132
    }
133
}
134