Completed
Push — master ( 8c9387...9f1772 )
by Joschi
03:25
created

MetaPropertiesTrait::setMetaProperties()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 4
rs 9.2
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Domain
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Domain\Model\Object\Traits;
38
39
use Apparat\Object\Domain\Model\Object\ObjectInterface;
40
use Apparat\Object\Domain\Model\Properties\MetaProperties;
41
42
/**
43
 * Meta properties trait
44
 *
45
 * @package Apparat\Object
46
 * @subpackage Apparat\Object\Domain
47
 */
48
trait MetaPropertiesTrait
49
{
50
    /**
51
     * Meta properties
52
     *
53
     * @var MetaProperties
54
     */
55
    protected $metaProperties;
56
57
    /**
58
     * Return the object title
59
     *
60
     * @return string Object title
61
     */
62 1
    public function getTitle()
63
    {
64 1
        return $this->metaProperties->getTitle();
65
    }
66
67
    /**
68
     * Set the title
69
     *
70
     * @param string $title Title
71
     * @return ObjectInterface Self reference
72
     */
73 1
    public function setTitle($title)
74
    {
75 1
        $this->setMetaProperties($this->metaProperties->setTitle($title));
76 1
        return $this;
77
    }
78
79
    /**
80
     * Set the meta properties collection
81
     *
82
     * @param MetaProperties $metaProperties Meta property collection
83
     * @param bool $overwrite Overwrite the existing collection (if present)
84
     */
85 18
    protected function setMetaProperties(MetaProperties $metaProperties, $overwrite = false)
86
    {
87 18
        $this->metaProperties = $metaProperties;
88 18
        $metaPropertiesState = spl_object_hash($this->metaProperties);
89
90
        // If the meta property collection state has changed
91 18
        if (!$overwrite
92 18
            && !empty($this->collectionStates[MetaProperties::COLLECTION])
0 ignored issues
show
Bug introduced by
The property collectionStates 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...
93 18
            && ($metaPropertiesState !== $this->collectionStates[MetaProperties::COLLECTION])
94
        ) {
95
            // Flag this object as mutated
96 1
            $this->setMutatedState();
0 ignored issues
show
Bug introduced by
It seems like setMutatedState() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
97
        }
98
99 18
        $this->collectionStates[MetaProperties::COLLECTION] = $metaPropertiesState;
100 18
    }
101
102
    /**
103
     * Return the object slug
104
     *
105
     * @return string Object slug
106
     */
107 1
    public function getSlug()
108
    {
109 1
        return $this->metaProperties->getSlug();
110
    }
111
112
    /**
113
     * Set the slug
114
     *
115
     * @param string $slug Slug
116
     * @return ObjectInterface Self reference
117
     */
118 1
    public function setSlug($slug)
119
    {
120 1
        $this->setMetaProperties($this->metaProperties->setSlug($slug));
121 1
        return $this;
122
    }
123
124
    /**
125
     * Return the object description
126
     *
127
     * @return string Object description
128
     */
129 2
    public function getDescription()
130
    {
131 2
        return $this->metaProperties->getDescription();
132
    }
133
134
    /**
135
     * Set the description
136
     *
137
     * @param string $description Description
138
     * @return ObjectInterface Self reference
139
     */
140 1
    public function setDescription($description)
141
    {
142 1
        $this->setMetaProperties($this->metaProperties->setDescription($description));
143 1
        return $this;
144
    }
145
146
    /**
147
     * Return the object abstract
148
     *
149
     * @return string Object abstract
150
     */
151 2
    public function getAbstract()
152
    {
153 2
        return $this->metaProperties->getAbstract();
154
    }
155
156
    /**
157
     * Set the abstract
158
     *
159
     * @param string $abstract Abstract
160
     * @return ObjectInterface Self reference
161
     */
162 1
    public function setAbstract($abstract)
163
    {
164 1
        $this->setMetaProperties($this->metaProperties->setAbstract($abstract));
165 1
        return $this;
166
    }
167
168
    /**
169
     * Return the license
170
     *
171
     * @return string License
172
     */
173 1
    public function getLicense()
174
    {
175 1
        return $this->metaProperties->getLicense();
176
    }
177
178
    /**
179
     * Set the license
180
     *
181
     * @param string $license License
182
     * @return MetaProperties Self reference
183
     */
184 1
    public function setLicense($license)
185
    {
186 1
        $this->setMetaProperties($this->metaProperties->setLicense($license));
187 1
        return $this;
188
    }
189
190
    /**
191
     * Return the privacy
192
     *
193
     * @return string Privacy
194
     */
195 1
    public function getPrivacy()
196
    {
197 1
        return $this->metaProperties->getPrivacy();
198
    }
199
200
    /**
201
     * Set the privacy
202
     *
203
     * @param string $privacy Privacy
204
     * @return MetaProperties Self reference
205
     */
206 1
    public function setPrivacy($privacy)
207
    {
208 1
        $this->setMetaProperties($this->metaProperties->setPrivacy($privacy));
209 1
        return $this;
210
    }
211
212
    /**
213
     * Return all object keywords
214
     *
215
     * @return array Object keywords
216
     */
217 2
    public function getKeywords()
218
    {
219 2
        return $this->metaProperties->getKeywords();
220
    }
221
222
    /**
223
     * Set the keywords
224
     *
225
     * @param array $keywords Keywords
226
     * @return ObjectInterface Self reference
227
     */
228 1
    public function setKeywords(array $keywords)
229
    {
230 1
        $this->setMetaProperties($this->metaProperties->setKeywords($keywords));
231 1
        return $this;
232
    }
233
234
    /**
235
     * Return all object categories
236
     *
237
     * @return array Object categories
238
     */
239 2
    public function getCategories()
240
    {
241 2
        return $this->metaProperties->getCategories();
242
    }
243
244
    /**
245
     * Set the categories
246
     *
247
     * @param array $categories Categories
248
     * @return ObjectInterface Self reference
249
     */
250 1
    public function setCategories(array $categories)
251
    {
252 1
        $this->setMetaProperties($this->metaProperties->setCategories($categories));
253 1
        return $this;
254
    }
255
}
256