Completed
Pull Request — master (#79)
by Jacob
03:15
created

EmbedCollection::createNewEmbed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

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 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace As3\Modlr\Models\Collections;
4
5
use As3\Modlr\Metadata\EmbedMetadata;
6
use As3\Modlr\Models\AbstractModel;
7
use As3\Modlr\Models\Embed;
8
use As3\Modlr\Store\Store;
9
10
/**
11
 * Model collection that contains embedded fragments from a persistence (database) layer.
12
 *
13
 * @author Jacob Bare <[email protected]>
14
 */
15
class EmbedCollection extends AbstractCollection
16
{
17
    /**
18
     * @var EmbedMetadata
19
     */
20
    protected $metadata;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param   EmbedMetadata  $metadata
26
     * @param   Store           $store
27
     * @param   AbstractModel[] $models
28
     */
29
    public function __construct(EmbedMetadata $metadata, Store $store, array $models = [])
30
    {
31
        $this->metadata = $metadata;
32
        parent::__construct($store, $models);
33
    }
34
35
    /**
36
     * Creates a new Embed model instance based on the collection
37
     *
38
     * @return  Embed
39
     */
40
    public function createNewEmbed()
41
    {
42
        $embed = $this->store->loadEmbed($this->getMetadata(), []);
43
        $embed->getState()->setNew();
44
        return $embed;
45
    }
46
47
    /**
48
     * Gets the metadata for the model collection.
49
     *
50
     * @return  EmbedMetadata
51
     */
52
    public function getMetadata()
53
    {
54
        return $this->metadata;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getType()
61
    {
62
        return $this->getMetadata()->name;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function isDirty()
69
    {
70
        if (true === $this->hasDirtyModels()) {
71
            return true;
72
        }
73
        return parent::isDirty();
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    protected function modelsMatch(AbstractModel $model, AbstractModel $loaded)
80
    {
81
        $this->validateModelClass($model);
82
        $this->validateModelClass($loaded);
83
        return spl_object_hash($model) === spl_object_hash($loaded);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    protected function validateAdd(AbstractModel $model)
90
    {
91
        $this->validateModelClass($model);
92
        $this->store->validateEmbedSet($this->getMetadata(), $model->getName());
93
    }
94
95
    /**
96
     * Validates that the model class instance is supported.
97
     *
98
     * @param   AbstractModel   $model
99
     * @throws  \InvalidArgumentException
100
     */
101
    protected function validateModelClass(AbstractModel $model)
102
    {
103
        if (!$model instanceof Embed) {
104
            throw new \InvalidArgumentExcepton('The model must be an instanceof of Embed');
105
        }
106
    }
107
}
108