Completed
Pull Request — master (#79)
by Jacob
02:58
created

MixinMetadata::validateAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 9
loc 9
rs 9.6666
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace As3\Modlr\Metadata;
4
5
use As3\Modlr\Exception\MetadataException;
6
7
/**
8
 * Defines the metadata for an entity mixin.
9
 * A mixin is like a PHP trait, in that properties (attributes and relationships) can be reused by multiple models.
10
 * Should be loaded using the MetadataFactory, not instantiated directly.
11
 *
12
 * @author Jacob Bare <[email protected]>
13
 */
14
class MixinMetadata implements Interfaces\AttributeInterface, Interfaces\EmbedInterface, Interfaces\RelationshipInterface
15
{
16
    /**
17
     * Uses attributes.
18
     */
19
    use Traits\AttributesTrait;
20
21
    /**
22
     * Uses embeds.
23
     */
24
    use Traits\EmbedsTrait;
25
26
    /**
27
     * Uses merged properties.
28
     */
29
    use Traits\PropertiesTrait;
30
31
    /**
32
     * Uses relationships.
33
     */
34
    use Traits\RelationshipsTrait;
35
36
    /**
37
     * The mixin name/key.
38
     *
39
     * @var string
40
     */
41
    public $name;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param   string  $name   The mixin name.
47
     */
48
    public function __construct($name)
49
    {
50
        $this->name = $name;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getProperties()
57
    {
58
        return array_merge($this->getAttributes(), $this->getRelationships(), $this->getEmbeds());
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 View Code Duplication
    protected function applyMixinProperties(MixinMetadata $mixin)
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...
65
    {
66
        foreach ($mixin->getAttributes() as $attribute) {
67
            if (true === $this->hasAttribute($attribute->key)) {
68
                throw MetadataException::mixinPropertyExists($this->type, $mixin->name, 'attribute', $attribute->key);
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
            }
70
            $this->addAttribute($attribute);
71
        }
72
        foreach ($mixin->getRelationships() as $relationship) {
73
            if (true === $this->hasRelationship($relationship->key)) {
74
                throw MetadataException::mixinPropertyExists($this->type, $mixin->name, 'relationship', $relationship->key);
75
            }
76
            $this->addRelationship($relationship);
77
        }
78
        foreach ($mixin->getEmbeds() as $embed) {
79
            if (true === $this->hasEmbed($embed->key)) {
80
                throw MetadataException::mixinPropertyExists($this->type, $mixin->name, 'embed', $embed->key);
81
            }
82
            $this->addEmbed($embed);
83
        }
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 View Code Duplication
    protected function validateAttribute(AttributeMetadata $attribute)
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...
90
    {
91
        if (true === $this->hasRelationship($attribute->getKey())) {
92
            throw MetadataException::fieldKeyInUse('attribute', 'relationship', $attribute->getKey(), $this->type);
93
        }
94
        if (true === $this->hasEmbed($attribute->getKey())) {
95
            throw MetadataException::fieldKeyInUse('attribute', 'embed', $attribute->getKey(), $this->type);
96
        }
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 View Code Duplication
    protected function validateEmbed(EmbeddedPropMetadata $embed)
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...
103
    {
104
        if (true === $this->hasAttribute($embed->getKey())) {
105
            throw MetadataException::fieldKeyInUse('embed', 'attribute', $embed->getKey(), $this->type);
106
        }
107
        if (true === $this->hasRelationship($embed->getKey())) {
108
            throw MetadataException::fieldKeyInUse('embed', 'relationship', $embed->getKey(), $this->type);
109
        }
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 View Code Duplication
    protected function validateRelationship(RelationshipMetadata $relationship)
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...
116
    {
117
        if (true === $this->hasAttribute($relationship->getKey())) {
118
            throw MetadataException::fieldKeyInUse('relationship', 'attribute', $relationship->getKey(), $this->type);
119
        }
120
        if (true === $this->hasEmbed($relationship->getKey())) {
121
            throw MetadataException::fieldKeyInUse('relationship', 'embed', $relationship->getKey(), $this->type);
122
        }
123
    }
124
}
125