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

EmbedMetadata::validateEmbed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
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 embed.
9
 * Should be loaded using the MetadataFactory, not instantiated directly.
10
 *
11
 * @author Jacob Bare <[email protected]>
12
 */
13
class EmbedMetadata implements Interfaces\AttributeInterface, Interfaces\EmbedInterface, Interfaces\MixinInterface
14
{
15
    /**
16
     * Uses attributes.
17
     */
18
    use Traits\AttributesTrait;
19
20
    /**
21
     * Uses embeds.
22
     */
23
    use Traits\EmbedsTrait;
24
25
    /**
26
     * Uses mixins.
27
     */
28
    use Traits\MixinsTrait;
29
30
    /**
31
     * Uses merged properties.
32
     */
33
    use Traits\PropertiesTrait;
34
35
    /**
36
     * The embed name/key.
37
     *
38
     * @var string
39
     */
40
    public $name;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param   string  $name   The embed name.
46
     */
47
    public function __construct($name)
48
    {
49
        $this->name = $name;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getProperties()
56
    {
57
        return array_merge($this->getAttributes(), $this->getEmbeds());
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function applyMixinProperties(MixinMetadata $mixin)
64
    {
65
        foreach ($mixin->getAttributes() as $attribute) {
66
            if (true === $this->hasAttribute($attribute->key)) {
67
                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...
68
            }
69
            $this->addAttribute($attribute);
70
        }
71
        foreach ($mixin->getEmbeds() as $embed) {
72
            if (true === $this->hasEmbed($embed->key)) {
73
                throw MetadataException::mixinPropertyExists($this->type, $mixin->name, 'embed', $embed->key);
74
            }
75
            $this->addEmbed($embed);
76
        }
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    protected function validateAttribute(AttributeMetadata $attribute)
83
    {
84
        if (true === $this->hasEmbed($attribute->getKey())) {
85
            throw MetadataException::fieldKeyInUse('attribute', 'embed', $attribute->getKey(), $this->name);
86
        }
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    protected function validateEmbed(EmbeddedPropMetadata $embed)
93
    {
94
        if (true === $this->hasAttribute($embed->getKey())) {
95
            throw MetadataException::fieldKeyInUse('embed', 'attribute', $embed->getKey(), $this->name);
96
        }
97
    }
98
}
99