Completed
Push — master ( 32bf99...3c4ae0 )
by Christopher
06:22
created

MetadataManager::pluralize()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 11

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 5
nop 3
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
12
    private $serializer = null;
13
14
    public function __construct($namespaceName = "Data", $containerName = "DefaultContainer")
15
    {
16
        $this->V3Edmx = new Edmx($namespaceName, $containerName);
17
        if (!$this->V3Edmx->isOK($msg)) {
18
            throw new \Exception($msg);
19
        }
20
        $ymlDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . "MetadataV3" . DIRECTORY_SEPARATOR . "JMSmetadata";
21
        $this->serializer =
22
            \JMS\Serializer\SerializerBuilder::create()
23
                ->addMetadataDir($ymlDir)
24
                ->build();
25
    }
26
27
    public function getEdmx()
28
    {
29
        return $this->V3Edmx;
30
    }
31
32
    public function getEdmxXML()
33
    {
34
        return $this->serializer->serialize($this->V3Edmx, "xml");
35
    }
36
37
    public function addEntityType($name, $accessType = "Public")
38
    {
39
        $NewEntity = new TEntityTypeType();
40
        $NewEntity->setName($name);
41
42
43
        $entitySet = new \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType();
44
        $entitySet->setName($this->pluralize(2, $NewEntity->getName()));
45
        $namespace = $this->V3Edmx->getDataServices()[0]->getNamespace();
46
        if (0 == strlen(trim($namespace))) {
47
            $entityTypeName = $NewEntity->getName();
48
        } else {
49
            $entityTypeName = $namespace . "." . $NewEntity->getName();
50
        }
51
        $entitySet->setEntityType($entityTypeName);
52
        $entitySet->setGetterAccess($accessType);
53
54
        $this->V3Edmx->getDataServices()[0]->addToEntityType($NewEntity);
55
        $this->V3Edmx->getDataServices()[0]->getEntityContainer()[0]->addToEntitySet($entitySet);
56
        return $NewEntity;
57
    }
58
59
    /**
60
     * Pluralizes a word if quantity is not one.
61
     *
62
     * @param int $quantity Number of items
63
     * @param string $singular Singular form of word
64
     * @param string $plural Plural form of word; function will attempt to deduce plural form from singular if not provided
65
     * @return string Pluralized word if quantity is not one, otherwise singular
66
     */
67 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...
68
    {
69
        if ($quantity == 1 || !strlen($singular)) return $singular;
70
        if ($plural !== null) return $plural;
71
72
        $last_letter = strtolower($singular[strlen($singular) - 1]);
73
        switch ($last_letter) {
74
            case 'y':
75
                return substr($singular, 0, -1) . 'ies';
76
            case 's':
77
                return $singular . 'es';
78
            default:
79
                return $singular . 's';
80
        }
81
    }
82
83
    public function addComplexType(\ReflectionClass $refClass, $name, $namespace = null, $baseResourceType = null)
84
    {
85
        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...
86
    }
87
}
88