Completed
Push — master ( 88eef6...81252c )
by Joschi
02:44
created

AbstractObject::__construct()   F

Complexity

Conditions 12
Paths 1025

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 14.4758

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 0
loc 46
ccs 23
cts 31
cp 0.7419
rs 2.7211
cc 12
eloc 29
nc 1025
nop 3
crap 14.4758

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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)
0 ignored issues
show
Coding Style introduced by
Spaces must be used for alignment; tabs are not allowed
Loading history...
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;
38
39
use Apparat\Object\Domain\Model\Author\AuthorInterface;
40
use Apparat\Object\Domain\Model\Path\RepositoryPathInterface;
41
use Apparat\Object\Domain\Model\Properties\AbstractDomainProperties;
42
use Apparat\Object\Domain\Model\Properties\InvalidArgumentException as PropertyInvalidArgumentException;
43
use Apparat\Object\Domain\Model\Properties\MetaProperties;
44
use Apparat\Object\Domain\Model\Properties\ProcessingInstructions;
45
use Apparat\Object\Domain\Model\Properties\Relations;
46
use Apparat\Object\Domain\Model\Properties\SystemProperties;
47
48
/**
49
 * Abstract object
50
 *
51
 * @package Apparat\Object
52
 * @subpackage Apparat\Object\Domain
53
 */
54
abstract class AbstractObject implements ObjectInterface
55
{
56
    /**
57
     * System properties
58
     *
59
     * @var SystemProperties
60
     */
61
    protected $_systemProperties;
62
    /**
63
     * Meta properties
64
     *
65
     * @var MetaProperties
66
     */
67
    protected $_metaProperties;
68
    /**
69
     * Domain properties
70
     *
71
     * @var AbstractDomainProperties
72
     */
73
    protected $_domainProperties;
74
    /**
75
     * Object relations
76
     *
77
     * @var Relations
78
     */
79
    private $_relations;
80
    /**
81
     * Processing instructions
82
     *
83
     * @var ProcessingInstructions
84
     */
85
    private $_processingInstructions;
86
    /**
87
     * Object payload
88
     *
89
     * @var string
90
     */
91
    protected $_payload;
92
    /**
93
     * Repository path
94
     *
95
     * @var RepositoryPathInterface
96
     */
97
    protected $_path;
98
    /**
99
     * Domain property collection class
100
     *
101
     * @var string
102
     */
103
    protected $_domainPropertyCollectionClass = null;
104
105
    /**
106
     * Object constructor
107
     *
108
     * @param RepositoryPathInterface $path Object repository path
109
     * @param array $propertyData Property data
110
     * @param string $payload Object payload
111
     * @throws PropertyInvalidArgumentException If the domain property collection class is invalid
112
     */
113 2
    public function __construct(RepositoryPathInterface $path, array $propertyData = [], $payload = '')
114
    {
115
        // If the domain property collection class is invalid
116 2
        if (!is_subclass_of($this->_domainPropertyCollectionClass, AbstractDomainProperties::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Apparat\Object\Domain\M...DomainProperties::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
117
            throw new PropertyInvalidArgumentException(
118
                sprintf(
119
                    'Invalid domain property collection class "%s"',
120
                    $this->_domainPropertyCollectionClass
121
                ),
122
                PropertyInvalidArgumentException::INVALID_DOMAIN_PROPERTY_COLLECTION_CLASS
123
            );
124
        }
125
126 2
        $this->_payload = $payload;
127 2
        $this->_path = $path;
128
129
        // Instantiate the system properties
130 2
        $systemPropertyData = (empty($propertyData[SystemProperties::COLLECTION]) || !is_array(
131 2
                $propertyData[SystemProperties::COLLECTION]
132 2
            )) ? [] : $propertyData[SystemProperties::COLLECTION];
133 2
        $this->_systemProperties = new SystemProperties($systemPropertyData, $this);
134
135
        // Instantiate the meta properties
136 1
        $metaPropertyData = (empty($propertyData[MetaProperties::COLLECTION]) || !is_array(
137 1
                $propertyData[MetaProperties::COLLECTION]
138 1
            )) ? [] : $propertyData[MetaProperties::COLLECTION];
139 1
        $this->_metaProperties = new MetaProperties($metaPropertyData, $this);
140
141
        // Instantiate the domain properties
142 1
        $domainPropertyData = (empty($propertyData[AbstractDomainProperties::COLLECTION]) || !is_array(
143 1
                $propertyData[AbstractDomainProperties::COLLECTION]
144 1
            )) ? [] : $propertyData[AbstractDomainProperties::COLLECTION];
145 1
        $this->_domainProperties = new $this->_domainPropertyCollectionClass($domainPropertyData, $this);
146
147
        // Instantiate the processing instructions
148 1
        $processingInstructionData = (empty($propertyData[ProcessingInstructions::COLLECTION]) || !is_array(
149
                $propertyData[ProcessingInstructions::COLLECTION]
150 1
            )) ? [] : $propertyData[ProcessingInstructions::COLLECTION];
151 1
        $this->_processingInstructions = new ProcessingInstructions($processingInstructionData, $this);
152
153
        // Instantiate the object relations
154 1
        $relationData = (empty($propertyData[Relations::COLLECTION]) || !is_array(
155
                $propertyData[Relations::COLLECTION]
156 1
            )) ? [] : $propertyData[Relations::COLLECTION];
157 1
        $this->_relations = new Relations($relationData, $this);
158 1
    }
159
160
    /**
161
     * Return the object ID
162
     *
163
     * @return Id Object ID
164
     */
165
    public function getId()
166
    {
167
        return $this->_systemProperties->getId();
168
    }
169
170
    /**
171
     * Return the object type
172
     *
173
     * @return Type Object type
174
     */
175
    public function getType()
176
    {
177
        return $this->_systemProperties->getType();
178
    }
179
180
    /**
181
     * Return the object revision
182
     *
183
     * @return Revision Object revision
184
     */
185
    public function getRevision()
186
    {
187
        return $this->_systemProperties->getRevision();
188
    }
189
190
    /**
191
     * Return the creation date & time
192
     *
193
     * @return \DateTimeImmutable Creation date & time
194
     */
195
    public function getCreated()
196
    {
197
        return $this->_systemProperties->getCreated();
198
    }
199
200
    /**
201
     * Return the publication date & time
202
     *
203
     * @return \DateTimeImmutable Publication date & time
204
     */
205
    public function getPublished()
206
    {
207
        return $this->_systemProperties->getPublished();
208
    }
209
210
    /**
211
     * Return all object keywords
212
     *
213
     * @return array Object keywords
214
     */
215 1
    public function getKeywords()
216
    {
217 1
        return $this->_metaProperties->getKeywords();
218
    }
219
220
    /**
221
     * Return all object categories
222
     *
223
     * @return array Object categories
224
     */
225 1
    public function getCategories()
226
    {
227 1
        return $this->_metaProperties->getCategories();
228
    }
229
230
    /**
231
     * Return all object authors
232
     *
233
     * @return AuthorInterface[] Authors
234
     */
235 1
    public function getAuthors()
236
    {
237 1
        return $this->_metaProperties->getAuthors();
238
    }
239
240
    /**
241
     * Add an object author
242
     *
243
     * @param AuthorInterface $author Author
244
     * @return ObjectInterface Self reference
245
     */
246
    public function addAuthor(AuthorInterface $author)
247
    {
248
        $authors = $this->_metaProperties->getAuthors();
249
        $authors[] = $author;
250
        $this->_metaProperties->setAuthors($authors);
251
        return $this;
252
    }
253
254
    /**
255
     * Return the object repository path
256
     *
257
     * @return RepositoryPathInterface Object repository path
258
     */
259 1
    public function getRepositoryPath()
260
    {
261 1
        return $this->_path;
262
    }
263
264
    /**
265
     * Return the absolute object URL
266
     *
267
     * @return string
268
     */
269
    public function getAbsoluteUrl()
270
    {
271
        return getenv('APPARAT_BASE_URL').$this->_path->getRepository()->getUrl().strval($this->_path);
272
    }
273
}
274