Completed
Push — master ( b5a813...e7314c )
by Christopher
05:12
created

addNavigationPropertyToEntityType()   C

Complexity

Conditions 8
Paths 24

Size

Total Lines 91
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 91
rs 5.574
cc 8
eloc 72
nc 24
nop 16

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace AlgoWeb\ODataMetadata;
4
5
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\AssociationSetAnonymousType;
6
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\AssociationSetAnonymousType\EndAnonymousType;
7
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType;
8
use AlgoWeb\ODataMetadata\MetadataV3\edm\TAssociationEndType;
9
use AlgoWeb\ODataMetadata\MetadataV3\edm\TAssociationType;
10
use AlgoWeb\ODataMetadata\MetadataV3\edm\TConstraintType;
11
use AlgoWeb\ODataMetadata\MetadataV3\edm\TDocumentationType;
12
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityPropertyType;
13
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType;
14
use AlgoWeb\ODataMetadata\MetadataV3\edm\TNavigationPropertyType;
15
use AlgoWeb\ODataMetadata\MetadataV3\edm\TPropertyRefType;
16
use AlgoWeb\ODataMetadata\MetadataV3\edm\TReferentialConstraintRoleElementType;
17
use AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx;
18
use JMS\Serializer\SerializerBuilder;
19
20
class MetadataManager
21
{
22
    private $V3Edmx = null;
23
    private $oldEdmx = null;
24
    private $lastError = null;
25
    private $serializer = null;
26
27
    public function __construct($namespaceName = "Data", $containerName = "DefaultContainer")
28
    {
29
        $this->V3Edmx = new Edmx($namespaceName, $containerName);
30
        if (!$this->V3Edmx->isOK($msg)) {
31
            throw new \Exception($msg);
32
        }
33
        $ymlDir = __DIR__ . DIRECTORY_SEPARATOR . "MetadataV3" . DIRECTORY_SEPARATOR . "JMSmetadata";
34
        $this->serializer =
35
            SerializerBuilder::create()
36
                ->addMetadataDir($ymlDir)
37
                ->build();
38
    }
39
40
    public function getEdmx()
41
    {
42
        return $this->V3Edmx;
43
    }
44
45
    public function getEdmxXML()
46
    {
47
        return $this->serializer->serialize($this->V3Edmx, "xml");
48
    }
49
50
    public function addEntityType($name, $accessType = "Public", $summary = null, $longDescription = null)
51
    {
52
        $this->startEdmxTransaction();
53
        $NewEntity = new TEntityTypeType();
54
        $NewEntity->setName($name);
55 View Code Duplication
        if (null != $summary || null != $longDescription) {
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...
56
            $documentation = new TDocumentationType();
57
            $documentation->setSummary($summary);
58
            $documentation->setLongDescription($longDescription);
59
            $NewEntity->setDocumentation($documentation);
60
        }
61
62
        $entitySet = new EntitySetAnonymousType();
63
        $entitySet->setName($this->pluralize(2, $NewEntity->getName()));
64
        $namespace = $this->V3Edmx->getDataServices()[0]->getNamespace();
65 View Code Duplication
        if (0 == strlen(trim($namespace))) {
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...
66
            $entityTypeName = $NewEntity->getName();
67
        } else {
68
            $entityTypeName = $namespace . "." . $NewEntity->getName();
69
        }
70
        $entitySet->setEntityType($entityTypeName);
71
        $entitySet->setGetterAccess($accessType);
72
73
        $this->V3Edmx->getDataServices()[0]->addToEntityType($NewEntity);
74
        $this->V3Edmx->getDataServices()[0]->getEntityContainer()[0]->addToEntitySet($entitySet);
75
        if (!$this->V3Edmx->isok($this->lastError)) {
76
            $this->revertEdmxTransaction();
77
            return false;
78
        }
79
        $this->commitEdmxTransaction();
80
        return $NewEntity;
81
    }
82
83
    private function startEdmxTransaction()
84
    {
85
        $this->oldEdmx = serialize($this->V3Edmx);
86
    }
87
88
    /**
89
     * Pluralizes a word if quantity is not one.
90
     *
91
     * @param int $quantity Number of items
92
     * @param string $singular Singular form of word
93
     * @param string $plural Plural form of word; function will attempt to deduce plural
94
     * form from singular if not provided
95
     * @return string Pluralized word if quantity is not one, otherwise singular
96
     */
97 View Code Duplication
    public static function pluralize($quantity, $singular, $plural = null)
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...
98
    {
99
        if ($quantity == 1 || !strlen($singular)) {
100
            return $singular;
101
        }
102
        if ($plural !== null) {
103
            return $plural;
104
        }
105
106
        $last_letter = strtolower($singular[strlen($singular) - 1]);
107
        switch ($last_letter) {
108
            case 'y':
109
                return substr($singular, 0, -1) . 'ies';
110
            case 's':
111
                return $singular . 'es';
112
            default:
113
                return $singular . 's';
114
        }
115
    }
116
117
    private function revertEdmxTransaction()
118
    {
119
        $this->V3Edmx = unserialize($this->oldEdmx);
120
    }
121
122
    private function commitEdmxTransaction()
123
    {
124
        $this->oldEdmx = null;
125
    }
126
127
    public function addPropertyToEntityType(
128
        $entityType,
129
        $name,
130
        $type,
131
        $defaultValue = null,
132
        $nullable = false,
133
        $isKey = false,
134
        $storeGeneratedPattern = null,
135
        $summary = null,
136
        $longDescription = null
137
    )
138
    {
139
        $this->startEdmxTransaction();
140
        $NewProperty = new TEntityPropertyType();
141
        $NewProperty->setName($name);
142
        $NewProperty->setType($type);
143
        $NewProperty->setStoreGeneratedPattern($storeGeneratedPattern);
144
        $NewProperty->setNullable($nullable);
145 View Code Duplication
        if (null != $summary || null != $longDescription) {
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...
146
            $documentation = new TDocumentationType();
147
            $documentation->setSummary($summary);
148
            $documentation->setLongDescription($longDescription);
149
            $NewProperty->addToDocumentation($documentation);
150
        }
151
        if (null != $defaultValue) {
152
            $NewProperty->setDefaultValue($defaultValue);
153
        }
154
        $entityType->addToProperty($NewProperty);
155
        if ($isKey) {
156
            $Key = new TPropertyRefType();
157
            $Key->setName($name);
158
            $entityType->addToKey($Key);
159
        }
160
        if (!$this->V3Edmx->isok($this->lastError)) {
161
            $this->revertEdmxTransaction();
162
            return false;
163
        }
164
        $this->commitEdmxTransaction();
165
        return $NewProperty;
166
    }
167
168
    public function addNavigationPropertyToEntityType(
169
        TEntityTypeType $principalType,
170
        $principalProperty,
171
        $principalMultiplicity,
172
        $principalSummery,
173
        $principalLongDescription,
174
        TEntityTypeType $dependentType,
175
        $dependentProperty = "",
176
        $dependentMultiplicity,
177
        $dependentSummery,
178
        $dependentLongDescription,
179
        array $principalConstraintProperty = null,
180
        array $dependentConstraintProperty = null,
181
        $principalGetterAccess = "Public",
182
        $principalSetterAccess = "Public",
183
        $dependentGetterAccess = "Public",
184
        $dependentSetterAccess = "Public"
185
    )
186
    {
187
        $this->startEdmxTransaction();
188
        $dependentNavigationProperty = new TNavigationPropertyType();
189
        $principalEntitySetName = $this->pluralize(2, $principalType->getName());
190
        $dependentEntitySetName = $this->pluralize(2, $dependentType->getName());
191
        $relationName = $principalType->getName() . "_" . $principalProperty . "_" . $dependentType->getName() . "_" . $dependentProperty;
192
        $relationName = trim($relationName, "_");
193
194
        $namespace = $this->V3Edmx->getDataServices()[0]->getNamespace();
195
        if (0 == strlen(trim($namespace))) {
196
            $relationFQName = $relationName;
197
        } else {
198
            $relationFQName = $namespace . "." . $relationName;
199
        }
200
201
        $principalNavigationProperty = new TNavigationPropertyType();
202
        $principalNavigationProperty->setName($principalProperty);
203
        $principalNavigationProperty->setToRole($dependentEntitySetName);
204
        $principalNavigationProperty->setFromRole($principalEntitySetName);
205
        $principalNavigationProperty->setRelationship($relationFQName);
206
        $principalNavigationProperty->setGetterAccess($principalGetterAccess);
207
        $principalNavigationProperty->setSetterAccess($principalSetterAccess);
208
        if (null != $principalSummery || null != $principalLongDescription) {
209
            $principalDocumentation = new TDocumentationType();
210
            $principalDocumentation->setSummary($principalSummery);
211
            $principalDocumentation->setLongDescription($principalLongDescription);
212
            $principalNavigationProperty->setDocumentation($principalDocumentation);
213
        }
214
        $principalType->addToNavigationProperty($principalNavigationProperty);
215
216
217
        if (!empty($dependentProperty)) {
218
            $dependentNavigationProperty = new TNavigationPropertyType();
219
            $dependentNavigationProperty->setName($dependentProperty);
220
            $dependentNavigationProperty->setToRole($principalEntitySetName);
221
            $dependentNavigationProperty->setFromRole($dependentEntitySetName);
222
            $dependentNavigationProperty->setRelationship($relationFQName);
223
            $dependentNavigationProperty->setGetterAccess($dependentGetterAccess);
224
            $dependentNavigationProperty->setSetterAccess($dependentSetterAccess);
225
            if (null != $dependentSummery || null != $dependentLongDescription) {
226
                $dependentDocumentation = new TDocumentationType();
227
                $dependentDocumentation->setSummary($dependentSummery);
228
                $dependentDocumentation->setLongDescription($dependentLongDescription);
229
                $dependentNavigationProperty->setDocumentation($dependentDocumentation);
230
            }
231
            $dependentType->addToNavigationProperty($dependentNavigationProperty);
232
        }
233
234
        $assocation = createAssocationFromNavigationProperty(
235
            $principalType,
236
            $dependentType,
237
            $principalNavigationProperty,
238
            $dependentNavigationProperty,
239
            $principalMultiplicity,
240
            $dependentMultiplicity,
241
            $principalConstraintProperty,
242
            $dependentConstraintProperty
243
        );
244
245
        $this->V3Edmx->getDataServices()[0]->addToAssociation($assocation);
246
247
        $associationSet = createAssocationSetForAssocation($assocation, $principalEntitySetName, $dependentEntitySetName);
248
249
        $this->V3Edmx->getDataServices()[0]->getEntityContainer()[0]->addToAssociationSet($associationSet);
250
251
252
        if (!$this->V3Edmx->isok($this->lastError)) {
253
            $this->revertEdmxTransaction();
254
            return false;
255
        }
256
        $this->commitEdmxTransaction();
257
        return [$principalNavigationProperty, $dependentNavigationProperty];
258
    }
259
260
    public function getLastError()
261
    {
262
        return $this->lastError;
263
    }
264
265
    protected function createAssocationFromNavigationProperty(
266
        TEntityTypeType $principalType,
267
        TEntityTypeType $dependentType,
268
        TNavigationPropertyType $principalNavigationProperty,
269
        TNavigationPropertyType $dependentNavigationProperty = null,
270
        $principalMultiplicity,
271
        $dependentMultiplicity,
272
        array $principalConstraintProperty = null,
273
        array $dependentConstraintProperty = null
274
275
    )
276
    {
277
        if (null != $dependentNavigationProperty) {
278
            if ($dependentNavigationProperty->getRelationship() != $principalNavigationProperty->getRelationship()) {
279
                throw new \Exception("if you have both a dependant property and a principal property they should both have the same relationship");
280
            }
281
            if ($dependentNavigationProperty->getFromRole() != $principalNavigationProperty->getToRole() ||
282
                $dependentNavigationProperty->getToRole() != $principalNavigationProperty->getFromRole()
283
            ) {
284
                throw new \Exception("The from roles and two roles from matching properties should match");
285
            }
286
        }
287
        $namespace = $this->V3Edmx->getDataServices()[0]->getNamespace();
288
289
        if (0 == strlen(trim($namespace))) {
290
            $principalTypeFQName = $principalType->getName();
291
            $dependentTypeFQName = $dependentType->getName();
292
        } else {
293
            $principalTypeFQName = $namespace . "." . $principalType->getName();
294
            $dependentTypeFQName = $namespace . "." . $dependentType->getName();
295
        }
296
        $association = new TAssociationType();
297
        $association->setName($principalNavigationProperty->getRelationship());
298
        $principalEnd = new TAssociationEndType();
299
        $principalEnd->setType($principalTypeFQName);
300
        $principalEnd->setRole($principalNavigationProperty->getFromRole());
301
        $principalEnd->setMultiplicity($principalMultiplicity);
302
        $dependentEnd = new TAssociationEndType();
303
        $dependentEnd->setType($dependentTypeFQName);
304
        $dependentEnd->setRole($dependentNavigationProperty->getFromRole());
0 ignored issues
show
Bug introduced by
It seems like $dependentNavigationProperty is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
305
        $dependentEnd->setMultiplicity($dependentMultiplicity);
306
        $association->addToEnd($principalEnd);
307
        $association->addToEnd($dependentEnd);
308
        $principalReferralConstraint = null;
309
        $dependentReferralConstraint = null;
310 View Code Duplication
        if (null != $principalConstraintProperty && 0 < count($principalConstraintProperty)) {
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...
311
            $principalReferralConstraint = new TReferentialConstraintRoleElementType();
312
            $principalReferralConstraint->setRole($principalNavigationProperty->getFromRole());
313
            foreach ($principalConstraintProperty as $propertyRef) {
314
                $principalReferralConstraint->addToPropertyRef($propertyRef);
315
            }
316
        }
317 View Code Duplication
        if (null != $dependentConstraintProperty && 0 < count($dependentConstraintProperty)) {
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...
318
            $dependentReferralConstraint = new TReferentialConstraintRoleElementType();
319
            $dependentReferralConstraint->setRole($dependentNavigationProperty->getFromRole());
320
            foreach ($dependentConstraintProperty as $propertyRef) {
321
                $dependentReferralConstraint->addToPropertyRef($propertyRef);
322
            }
323
        }
324
325
        if (null != $dependentReferralConstraint || null != $principalReferralConstraint) {
326
            $constraint = new TConstraintType();
327
            $constraint->setPrincipal($principalReferralConstraint);
0 ignored issues
show
Bug introduced by
It seems like $principalReferralConstraint defined by null on line 308 can be null; however, AlgoWeb\ODataMetadata\Me...intType::setPrincipal() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
328
            $constraint->setDependent($dependentReferralConstraint);
0 ignored issues
show
Bug introduced by
It seems like $dependentReferralConstraint defined by null on line 309 can be null; however, AlgoWeb\ODataMetadata\Me...intType::setDependent() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
329
            $association->setReferentialConstraint($constraint);
330
        }
331
        return $association;
332
    }
333
334
    protected function createAssocationSetForAssocation(
335
        TAssociationType $association,
336
        $principalEntitySetName,
337
        $dependentEntitySetName
338
    )
339
    {
340
        $as = new AssociationSetAnonymousType();
341
        $name = $association->getName();
342
        $as->setName($name);
343
        $namespace = $this->V3Edmx->getDataServices()[0]->getNamespace();
344 View Code Duplication
        if (0 == strlen(trim($namespace))) {
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...
345
            $associationSetName = $association->getName();
346
        } else {
347
            $associationSetName = $namespace . "." . $association->getName();
348
        }
349
        $as->setAssociation($associationSetName);
350
        $end1 = new EndAnonymousType();
351
        $end1->setRole($association->getEnd()[0]->getRole());
352
        $end1->setEntitySet($principalEntitySetName);
353
        $end2 = new EndAnonymousType();
354
        $end2->setRole($association->getEnd()[1]->getRole());
355
        $end2->setEntitySet($dependentEntitySetName);
356
        $as->addToEnd($end1);
357
        $as->addToEnd($end2);
358
        return $as;
359
    }
360
361
}
362