Completed
Pull Request — master (#137)
by Christopher
03:25
created

MetadataGubbinsHolder   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 36
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 156
rs 8.8

6 Methods

Rating   Name   Duplication   Size   Complexity  
B addEntity() 0 15 5
B getRelationsByRelationName() 0 26 5
B getRelationsByClass() 0 29 6
A hasClass() 0 4 1
A checkClassExists() 0 7 2
D getRelations() 0 61 17
1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Models;
4
5
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\Association;
6
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationMonomorphic;
7
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationPolymorphic;
8
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubMonomorphic;
9
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubPolymorphic;
10
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityGubbins;
11
12
class MetadataGubbinsHolder
13
{
14
    protected $relations = [];
15
    protected $knownSides = [];
16
17
    public function addEntity(EntityGubbins $entity)
18
    {
19
        $className = $entity->getClassName();
20
        if (array_key_exists($className, $this->relations)) {
21
            $msg = $className . ' already added';
22
            throw new \InvalidArgumentException($msg);
23
        }
24
        $this->relations[$className] = $entity;
25
        $this->knownSides[$className] = [];
26
        foreach ($entity->getStubs() as $relName => $stub) {
27
            if ($stub instanceof AssociationStubPolymorphic && $stub->isKnownSide()) {
28
                $this->knownSides[$className][$relName] = $stub;
29
            }
30
        }
31
    }
32
33
    public function getRelationsByRelationName($className, $relName)
34
    {
35
        $this->checkClassExists($className);
36
37
        $rels = $this->relations[$className];
38
39
        if (!array_key_exists($relName, $rels->getStubs())) {
40
            $msg = 'Relation ' . $relName . ' not registered on ' . $className;
41
            throw new \InvalidArgumentException($msg);
42
        }
43
        $stub = $rels->getStubs()[$relName];
44
        $targType = $stub->getTargType();
45
        if (!array_key_exists($targType, $this->relations)) {
46
            return [];
47
        }
48
        $targRel = $this->relations[$targType];
49
        // now dig out compatible stubs on target type
50
        $targStubs = $targRel->getStubs();
51
        $relStubs = [];
52
        foreach ($targStubs as $targStub) {
53
            if ($stub->isCompatible($targStub)) {
54
                $relStubs[] = $targStub;
55
            }
56
        }
57
        return $relStubs;
58
    }
59
60
    public function getRelationsByClass($className)
61
    {
62
        $this->checkClassExists($className);
63
64
        $rels = $this->relations[$className];
65
        $stubs = $rels->getStubs();
66
67
        $associations = [];
68
        foreach ($stubs as $relName => $stub) {
69
            $others = $this->getRelationsByRelationName($className, $relName);
70
            if ($stub instanceof AssociationStubMonomorphic) {
71
                assert(
72
                    1 >= count($others),
73
                    'Monomorphic relation stub on ' . $className . ' ' . $relName
74
                    . ' should point to at most 1 other stub'
75
                );
76
            }
77
            if (1 === count($others)) {
78
                $others = $others[0];
79
                $assoc = new AssociationMonomorphic();
80
                $first = -1 === $stub->compare($others);
81
                $assoc->setFirst($first ? $stub : $others);
82
                $assoc->setLast($first ? $others : $stub);
83
                assert($assoc->isOk());
84
                $associations[] = $assoc;
85
            }
86
        }
87
        return $associations;
88
    }
89
90
    public function getRelations()
91
    {
92
        $classNames = array_keys($this->relations);
93
94
        $associations = [];
95
96
        foreach ($classNames as $class) {
97
            $rawAssoc = $this->getRelationsByClass($class);
98
            foreach ($rawAssoc as $raw) {
99
                if (!in_array($raw, $associations)) {
100
                    $associations[] = $raw;
101
                }
102
            }
103
        }
104
105
        $unknowns = [];
106
        foreach ($this->knownSides as $knownType => $knownDeets) {
107
            $unknowns[$knownType] = [];
108
            foreach (array_keys($knownDeets) as $key) {
109
                $unknowns[$knownType][$key] = [];
110
            }
111
        }
112
        $monoAssoc = [];
113
        $polyAssoc = [];
114
        foreach ($associations as $assoc) {
115
            if ($assoc->getFirst() instanceof AssociationStubMonomorphic) {
116
                $monoAssoc[] = $assoc;
117
                continue;
118
            }
119
            // monomorphic associations are dealt with, now for the polymorphic associations - they're a mite trickier
120
            $firstKnown = $assoc->getFirst()->isKnownSide();
121
            $known = $firstKnown ? $assoc->getFirst() : $assoc->getLast();
122
            $unknown = $firstKnown ? $assoc->getLast() : $assoc->getFirst();
123
            $className = $known->getBaseType();
124
            $relName = $known->getRelationName();
125
            $unknowns[$className][$relName][] = $unknown;
126
        }
127
128
        foreach ($this->knownSides as $knownType => $knownDeets) {
129
            foreach (array_keys($knownDeets) as $key) {
130
                $lastCandidates = $unknowns[$knownType][$key];
131
                if (0 == count($lastCandidates)) {
132
                    continue;
133
                }
134
                foreach($lastCandidates as $lc){
135
                    $stub = clone $this->knownSides[$knownType][$key];
136
                    $isMulti = ($stub->getMultiplicity()->getValue() == \AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubRelationType::MANY);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 169 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
137
                    $RelPolyTypeName = str_plural(substr($lc->getBaseType(),strrpos($lc->getBaseType(),"\\")+1),$isMulti?2:1);
0 ignored issues
show
Coding Style introduced by
$RelPolyTypeName does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style Comprehensibility introduced by
The string literal \\ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
138
                    $stub->setRelationName($stub->getRelationName() . "_" . $RelPolyTypeName);
0 ignored issues
show
Coding Style introduced by
$RelPolyTypeName does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style Comprehensibility introduced by
The string literal _ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
139
                    $assoc = new AssociationMonomorphic();
140
                    $first = -1 === $stub->compare($lc);
141
                    $assoc->setFirst($first ? $stub : $lc);
142
                    $assoc->setLast($first ? $lc : $stub);
143
                    assert($assoc->isOk());
144
                    $polyAssoc[] = $assoc;
145
                }
146
            }
147
        }
148
        $result = array_merge($monoAssoc, $polyAssoc);
149
        return $result;
150
    }
151
152
    public function hasClass($className)
153
    {
154
        return array_key_exists($className, $this->relations);
155
    }
156
157
    /**
158
     * @param $className
159
     */
160
    protected function checkClassExists($className)
161
    {
162
        if (!$this->hasClass($className)) {
163
            $msg = $className . ' does not exist in holder';
164
            throw new \InvalidArgumentException($msg);
165
        }
166
    }
167
}
168