GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 719445...c6607f )
by Bruno
03:00
created

StringGenerator::getAssociationLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher;
4
5
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
6
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\StringGenerator\VisitedAssociationLogger;
7
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\StringGenerator\VisitedAssociationLoggerInterface;
8
9
class StringGenerator implements StringGeneratorInterface
10
{
11
12
    /**
13
     * @var array
14
     */
15
    protected $classStrings;
16
17
18
    /**
19
     * @var ClassStoreInterface
20
     */
21
    protected $classStore;
22
23
    /**
24
     * @var VisitedAssociationLoggerInterface
25
     */
26
    protected $associationLogger;
27
28
    /**
29
     * @param ClassStoreInterface $classStore
30
     */
31 9
    public function __construct(ClassStoreInterface $classStore)
32
    {
33 9
        $this->classStore = $classStore;
34 9
        $this->associationLogger = new VisitedAssociationLogger();
35 9
    }
36
37
    /**
38
     * @return VisitedAssociationLoggerInterface
39
     */
40 1
    public function getAssociationLogger()
41
    {
42 1
        return $this->associationLogger;
43
    }
44
45
    /**
46
     * Build the string representing the single graph item
47
     *
48
     * @param ClassMetadata $class
49
     *
50
     * @return string
51
     */
52 7
    public function getClassString(ClassMetadata $class)
53
    {
54 7
        $className    = $class->getName();
55 7
        if (!isset($this->classStrings[$className])) {
56 7
            $this->associationLogger->visitAssociation($className);
57
58 7
            $classText    = '[' . str_replace('\\', '.', $className);
59 7
            $fields       = array();
60 7
            $parent       = $this->classStore->getParent($class);
61 7
            $parentFields = $parent ? $parent->getFieldNames() : array();
62
63 7
            foreach ($class->getFieldNames() as $fieldName) {
64 1
                if (in_array($fieldName, $parentFields)) {
65 1
                    continue;
66
                }
67
68 1
                if ($class->isIdentifier($fieldName)) {
69 1
                    $fields[] = '+' . $fieldName;
70 1
                } else {
71 1
                    $fields[] = $fieldName;
72
                }
73 7
            }
74
75 7
            if (!empty($fields)) {
76 1
                $classText .= '|' . implode(';', $fields);
77 1
            }
78
79 7
            $classText .= ']';
80
81 7
            $this->classStrings[$className] = $classText;
82 7
        }
83
84 7
        return $this->classStrings[$className];
85
    }
86
87
    /**
88
     * @param ClassMetadata $class1
89
     * @param string $association
90
     * @return string
91
     */
92 6
    public function getAssociationString(ClassMetadata $class1, $association)
93
    {
94 6
        $targetClassName = $class1->getAssociationTargetClass($association);
95 6
        $class2          = $this->classStore->getClassByName($targetClassName);
96 6
        $isInverse       = $class1->isAssociationInverseSide($association);
97 6
        $class1Count     = $class1->isCollectionValuedAssociation($association) ? 2 : 1;
98
99 6
        if (null === $class2) {
100 2
            return $this->makeSingleSidedLinkString($class1, $isInverse, $association, $class1Count, $targetClassName);
101
        }
102
103 5
        $class1SideName = $association;
104 5
        $class2SideName = $this->getClassReverseAssociationName($class1, $association);
105 5
        $class2Count    = 0;
106 5
        $bidirectional  = false;
107
108 5
        if (null !== $class2SideName) {
109 3
            if ($isInverse) {
110 3
                $class2Count    = $class2->isCollectionValuedAssociation($class2SideName) ? 2 : 1;
111 3
                $bidirectional  = true;
112 3
            } elseif ($class2->isAssociationInverseSide($class2SideName)) {
113 3
                $class2Count    = $class2->isCollectionValuedAssociation($class2SideName) ? 2 : 1;
114 3
                $bidirectional  = true;
115 3
            }
116 3
        }
117
118 5
        $this->associationLogger->visitAssociation($targetClassName, $class2SideName);
119
120 5
        return $this->makeDoubleSidedLinkString(
121 5
            $class1,
122 5
            $class2,
123 5
            $bidirectional,
124 5
            $isInverse,
125 5
            $class2SideName,
126 5
            $class2Count,
127 5
            $class1SideName,
128
            $class1Count
129 5
        );
130
    }
131
132
133
134
    /**
135
     * Returns the $class2 association name for $class1 if reverse related (or null if not)
136
     *
137
     * @param ClassMetadata $class1
138
     * @param string $association
139
     *
140
     * @return string|null
141
     */
142 5
    private function getClassReverseAssociationName(ClassMetadata $class1, $association)
143
    {
144 5
        if ($class1->getAssociationMapping($association)['isOwningSide']) {
145 5
            return $class1->getAssociationMapping($association)['inversedBy'];
146
        }
147
148 3
        return $class1->getAssociationMapping($association)['mappedBy'];
149
    }
150
151
    /**
152
     * @param ClassMetadata $class1
153
     * @param boolean $isInverse
154
     * @param string $association
155
     * @param int $class1Count
156
     * @param string $targetClassName
157
     * @return string
158
     */
159 2
    private function makeSingleSidedLinkString(
160
        ClassMetadata $class1,
161
        $isInverse,
162
        $association,
163
        $class1Count,
164
        $targetClassName
165
    ) {
166 2
        return $this->getClassString($class1) . ($isInverse ? '<' : '<>') . '-' . $association . ' '
167 2
        . ($class1Count > 1 ? '*' : ($class1Count ? '1' : '')) . ($isInverse ? '<>' : '>')
168 2
        . '[' . str_replace('\\', '.', $targetClassName) . ']';
169
    }
170
171
    /**
172
     * @param ClassMetadata $class1
173
     * @param ClassMetadata $class2
174
     * @param boolean $bidirectional
175
     * @param boolean $isInverse
176
     * @param string $class2SideName
177
     * @param integer $class2Count
178
     * @param string $class1SideName
179
     * @param integer $class1Count
180
     *
181
     * @return string
182
     */
183 5
    private function makeDoubleSidedLinkString(
184
        ClassMetadata $class1,
185
        ClassMetadata $class2,
186
        $bidirectional,
187
        $isInverse,
188
        $class2SideName,
189
        $class2Count,
190
        $class1SideName,
191
        $class1Count
192
    ) {
193 5
        return $this->getClassString($class1) . ($bidirectional ? ($isInverse ? '<' : '<>') : '')
194 5
        . ($class2SideName ? $class2SideName . ' ' : '') . ($class2Count > 1 ? '*' : ($class2Count ? '1' : ''))
195 5
        . '-' . $class1SideName . ' ' . ($class1Count > 1 ? '*' : ($class1Count ? '1' : ''))
196 5
        . (($bidirectional && $isInverse) ? '<>' : '>') . $this->getClassString($class2);
197
    }
198
}
199