AliceContext::resolveDepsFromArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Knp\FriendlyContexts\Context;
4
5
class AliceContext extends Context
6
{
7
    /**
8
     * @BeforeBackground
9
     **/
10
    public function loadAlice($event)
11
    {
12
        $this->storeTags($event);
13
        $fixtures = $this->getParameter('friendly.alice.fixtures');
14
        $loader = $this->getAliceLoader();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
15
16
        $files = $this->getTagContent('alice');
17
18
        if (in_array('*', $files)) {
19
            $files = array_keys($fixtures);
20
        } else {
21
            $files = $this->resolveDepsFromArray($files);
22
        }
23
24
        foreach ($files as $name) {
25 View Code Duplication
            if (!array_key_exists($name, $fixtures)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
27
                throw new \Exception(sprintf('Fixture "%s" unknown. "%s" expected', $name, implode('", "', array_keys($fixtures))));
28
            }
29
        }
30
31
        $this->loadFixtures($loader, $fixtures, $files);
32
        $this->registerCache($loader);
33
    }
34
35
    protected function loadFixtures($loader, $fixtures, $files)
36
    {
37
        $persistable = $this->getPersistableClasses();
38
39
        foreach ($fixtures as $id => $fixture) {
40
            if (in_array($id, $files)) {
41
                foreach ($loader->load($fixture) as $object) {
42
                    if (in_array(get_class($object), $persistable)) {
43
                        $this->getEntityManager()->persist($object);
44
                    }
45
                }
46
47
                $this->getEntityManager()->flush();
48
            }
49
        }
50
    }
51
52
    private function getPersistableClasses()
53
    {
54
        $persistable = array();
55
        $metadatas   = $this->getEntityManager()->getMetadataFactory()->getAllMetadata();
56
57
        foreach ($metadatas as $metadata) {
58
            if (isset($metadata->isEmbeddedClass) && $metadata->isEmbeddedClass) {
59
                continue;
60
            }
61
62
            $persistable[] = $metadata->getName();
63
        }
64
65
        return $persistable;
66
    }
67
68
    protected function registerCache($loader)
69
    {
70
        foreach ($loader->getCache() as $cache) {
71
            list($values, $entity) = $cache;
72
            $reflection = new \ReflectionClass($entity);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
73 View Code Duplication
            do {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
                $this
75
                    ->getRecordBag()
76
                    ->getCollection($reflection->getName())
77
                    ->attach($entity, $values)
78
                ;
79
                $reflection = $reflection->getParentClass();
80
            } while (false !== $reflection);
81
        }
82
        $loader->clearCache();
83
    }
84
85
    protected function resolveDepsFromArray(array $fixtures)
86
    {
87
        $result = [];
88
89
        foreach ($fixtures as $fixture) {
90
            $this->resolveDeps($fixture, $result);
91
        }
92
93
        return $result;
94
    }
95
96
    protected function resolveDeps($fixture, array &$result = [])
97
    {
98
        $result[] = $fixture;
99
        $tree = $this->getParameter('friendly.alice.dependencies');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
100
101
        if (!empty($tree[$fixture])) {
102
            foreach ($tree[$fixture] as $dep) {
103
                if (!in_array($dep, $result)) {
104
                    $this->resolveDeps($dep, $result);
105
                }
106
            }
107
        }
108
109
        return $result;
110
    }
111
}
112