Test Failed
Pull Request — master (#115)
by Alex
03:29
created

getRelationsByRelationName()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 26
rs 8.439
c 1
b 0
f 0
cc 5
eloc 17
nc 5
nop 2
1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Models;
4
5
class MetadataGubbinsHolder
6
{
7
    protected $relations = [];
8
9
    public function addEntity(EntityGubbins $entity)
10
    {
11
        $className = $entity->getClassName();
12
        if (array_key_exists($className, $this->relations)) {
13
            $msg = $className.' already added';
14
            throw new \InvalidArgumentException($msg);
15
        }
16
        $this->relations[$className] = $entity;
17
    }
18
19
    public function getRelationsByRelationName($className, $relName)
20
    {
21
        $this->checkClassExists($className);
22
23
        $rels = $this->relations[$className];
24
25
        if (!array_key_exists($relName, $rels->getStubs())) {
26
            $msg = 'Relation ' . $relName . ' not registered on ' . $className;
27
            throw new \InvalidArgumentException($msg);
28
        }
29
        $stub = $rels->getStubs()[$relName];
30
        $targType = $stub->getTargType();
31
        if (!array_key_exists($targType, $this->relations)) {
32
            return [];
33
        }
34
        $targRel = $this->relations[$targType];
35
        // now dig out compatible stubs on target type
36
        $targStubs = $targRel->getStubs();
37
        $relStubs = [];
38
        foreach ($targStubs as $targStub) {
39
            if ($stub->isCompatible($targStub)) {
40
                $relStubs[] = $targStub;
41
            }
42
        }
43
        return $relStubs;
44
    }
45
46
    public function getRelationsByClass($className)
47
    {
48
        $this->checkClassExists($className);
49
50
        $rels = $this->relations[$className];
51
        $stubs = $rels->getStubs();
52
53
        $associations = [];
54
        foreach ($stubs as $relName => $stub) {
55
            $others = $this->getRelationsByRelationName($className, $relName);
56
            if (1 === count($others)) {
57
                $others = $others[0];
58
                $assoc = new Association();
59
                $first = -1 === $stub->compare($others);
60
                $assoc->setFirst($first ? $stub : $others);
61
                $assoc->setLast($first ? $others : $stub);
62
                assert($assoc->isOk());
63
                $associations[] = $assoc;
64
            }
65
        }
66
        return $associations;
67
    }
68
69 View Code Duplication
    public function getRelations()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
70
    {
71
        $classNames = array_keys($this->relations);
72
73
        $associations = [];
74
75
        foreach ($classNames as $class) {
76
            $rawAssoc = $this->getRelationsByClass($class);
77
            foreach ($rawAssoc as $raw) {
78
                if (!in_array($raw, $associations)) {
79
                    $associations[] = $raw;
80
                }
81
            }
82
        }
83
84
        return $associations;
85
    }
86
87
    /**
88
     * @param $className
89
     */
90 View Code Duplication
    protected function checkClassExists($className)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
91
    {
92
        if (!array_key_exists($className, $this->relations)) {
93
            $msg = $className . ' does not exist in holder';
94
            throw new \InvalidArgumentException($msg);
95
        }
96
    }
97
}
98