Completed
Push — master ( c9e4cd...c85fb5 )
by Christopher
18:34 queued 13:48
created

MetadataManager::addPropertyToEntityType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 11
nc 2
nop 4
1
<?php
2
3
namespace AlgoWeb\ODataMetadata;
4
5
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType;
6
use AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx;
7
8
class MetadataManager
9
{
10
    private $V3Edmx = null;
11
    private $oldEdmx = null;
12
    private $lastError = null;
13
    private $serializer = null;
14
15
    public function __construct($namespaceName = "Data", $containerName = "DefaultContainer")
16
    {
17
        $this->V3Edmx = new Edmx($namespaceName, $containerName);
18
        if (!$this->V3Edmx->isOK($msg)) {
19
            throw new \Exception($msg);
20
        }
21
        $ymlDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . "MetadataV3" . DIRECTORY_SEPARATOR . "JMSmetadata";
22
        $this->serializer =
23
            \JMS\Serializer\SerializerBuilder::create()
24
                ->addMetadataDir($ymlDir)
25
                ->build();
26
    }
27
28
    public function getEdmx()
29
    {
30
        return $this->V3Edmx;
31
    }
32
33
    public function getEdmxXML()
34
    {
35
        return $this->serializer->serialize($this->V3Edmx, "xml");
36
    }
37
38
    public function addEntityType($name, $accessType = "Public")
39
    {
40
        $this->startEdmxTransaction();
41
        $NewEntity = new TEntityTypeType();
42
        $NewEntity->setName($name);
43
44
45
        $entitySet = new \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType();
46
        $entitySet->setName($this->pluralize(2, $NewEntity->getName()));
47
        $namespace = $this->V3Edmx->getDataServices()[0]->getNamespace();
48
        if (0 == strlen(trim($namespace))) {
49
            $entityTypeName = $NewEntity->getName();
50
        } else {
51
            $entityTypeName = $namespace . "." . $NewEntity->getName();
52
        }
53
        $entitySet->setEntityType($entityTypeName);
54
        $entitySet->setGetterAccess($accessType);
55
56
        $this->V3Edmx->getDataServices()[0]->addToEntityType($NewEntity);
57
        $this->V3Edmx->getDataServices()[0]->getEntityContainer()[0]->addToEntitySet($entitySet);
58
        if (!$this->V3Edmx->isok($this->lastError)) {
59
            $this->revertEdmxTransaction();
60
            return false;
61
        }
62
        $this->commitEdmxTransaction();
0 ignored issues
show
Unused Code introduced by
The call to the method AlgoWeb\ODataMetadata\Me...commitEdmxTransaction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
63
        return $NewEntity;
64
    }
65
66
    private function startEdmxTransaction()
67
    {
68
        $this->oldEdmx = serialize($this->V3Edmx);
69
    }
70
71
    /**
72
     * Pluralizes a word if quantity is not one.
73
     *
74
     * @param int $quantity Number of items
75
     * @param string $singular Singular form of word
76
     * @param string $plural Plural form of word; function will attempt to deduce plural form from singular if not provided
77
     * @return string Pluralized word if quantity is not one, otherwise singular
78
     */
79 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...
80
    {
81
        if ($quantity == 1 || !strlen($singular)) return $singular;
82
        if ($plural !== null) return $plural;
83
84
        $last_letter = strtolower($singular[strlen($singular) - 1]);
85
        switch ($last_letter) {
86
            case 'y':
87
                return substr($singular, 0, -1) . 'ies';
88
            case 's':
89
                return $singular . 'es';
90
            default:
91
                return $singular . 's';
92
        }
93
    }
94
95
    private function revertEdmxTransaction()
96
    {
97
        $this->V3Edmx = unserialize($this->oldEdmx);
98
    }
99
100
    private function commitEdmxTransaction()
101
    {
102
        $this->oldEdmx == null;
103
    }
104
105
    public function addPropertyToEntityType($entityType, $name, $type, $isKey = false)
0 ignored issues
show
Unused Code introduced by
The parameter $isKey is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107
        $this->startEdmxTransaction();
108
        $NewProperty = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityPropertyType();
109
        $NewProperty->setName("$name");
110
        $NewProperty->setType("$type");
111
        $entityType->addToProperty($NewProperty);
112
        if (!$this->V3Edmx->isok($this->lastError)) {
113
            $this->revertEdmxTransaction();
114
            return false;
115
        }
116
        $this->commitEdmxTransaction();
0 ignored issues
show
Unused Code introduced by
The call to the method AlgoWeb\ODataMetadata\Me...commitEdmxTransaction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
117
        return $NewProperty;
118
    }
119
120
    public function addComplexType(\ReflectionClass $refClass, $name, $namespace = null, $baseResourceType = null)
121
    {
122
        return $this->createResourceType($refClass, $name, $namespace, ResourceTypeKind::COMPLEX, $baseResourceType);
0 ignored issues
show
Bug introduced by
The method createResourceType() does not seem to exist on object<AlgoWeb\ODataMetadata\MetadataManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
    }
124
125
    public function getLastError()
126
    {
127
        return $this->lastError();
0 ignored issues
show
Bug introduced by
The method lastError() does not exist on AlgoWeb\ODataMetadata\MetadataManager. Did you maybe mean getLastError()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
128
    }
129
}
130