Completed
Push — master ( 844a18...827038 )
by Joschi
02:54
created

ObjectFactory::createFromResource()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.0534
cc 4
eloc 10
nc 2
nop 2
crap 4
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Application
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\Application\Factory;
38
39
use Apparat\Object\Domain\Model\Object\Id;
40
use Apparat\Object\Domain\Model\Object\ObjectInterface;
41
use Apparat\Object\Domain\Model\Object\ResourceInterface;
42
use Apparat\Object\Domain\Model\Object\Revision;
43
use Apparat\Object\Domain\Model\Object\Type;
44
use Apparat\Object\Domain\Model\Path\RepositoryPath;
45
use Apparat\Object\Domain\Model\Properties\SystemProperties;
46
47
/**
48
 * Object factory
49
 *
50
 * @package Apparat\Object
51
 * @subpackage Apparat\Object\Application
52
 */
53
class ObjectFactory
54
{
55
    /**
56
     * Create an object
57
     *
58
     * @param ResourceInterface $objectResource
59
     * @param RepositoryPath $path Repository object path
60
     * @return ObjectInterface Object
61
     * @throws InvalidArgumentException If the object type is undefined
62
     */
63 8
    public static function createFromResource(ResourceInterface $objectResource, RepositoryPath $path)
64
    {
65 8
        $propertyData = $objectResource->getPropertyData();
66
67
        // If the object type is undefined
68 8
        if (!array_key_exists(SystemProperties::COLLECTION, $propertyData) ||
69 7
            !is_array($propertyData[SystemProperties::COLLECTION]) ||
70 7
            empty($propertyData[SystemProperties::COLLECTION]['type'])
71 8
        ) {
72 1
            throw new InvalidArgumentException(
73 1
                'Undefined object type',
74
                InvalidArgumentException::UNDEFINED_OBJECT_TYPE
75 1
            );
76
        }
77
78
        // Determine the object class
79 7
        $objectClass = self::objectClassFromType($path->getType());
80
81
        // Instantiate the object
82 6
        return new $objectClass($objectResource->getPayload(), $propertyData, $path);
83
    }
84
85
    /**
86
     * Determine and validate the object class name from its type
87
     *
88
     * @param Type $type Object type
89
     * @return string Object class name
90
     * @throws InvalidArgumentException If the object type is invalid
91
     */
92 8
    protected static function objectClassFromType(Type $type)
93
    {
94
95
        // If the object type is invalid
96 8
        $objectType = $type->getType();
97 8
        $objectClass = 'Apparat\\Object\\Application\\Model\\Object\\'.ucfirst($objectType);
98 8
        if (!Type::isValidType($objectType) || !class_exists($objectClass)) {
99 1
            throw new InvalidArgumentException(
100 1
                sprintf('Invalid object type "%s"', $objectType),
101
                InvalidArgumentException::INVALID_OBJECT_TYPE
102 1
            );
103
        }
104
105 7
        return $objectClass;
106
    }
107
108
    /**
109
     * Create and return a new object
110
     *
111
     * @param Type $type Object type
112
     * @param string $payload Object payload
113
     * @param array $propertyData Object property data
114
     * @return ObjectInterface Object
115
     */
116 1
    public static function createNew(Type $type, $payload = '', array $propertyData = [])
117
    {
118
119
        // Determine the object class
120 1
        $objectClass = self::objectClassFromType($type);
121
122
        // Prepare the system properties collection
123 1
        $systemPropertyData = (empty($propertyData[SystemProperties::COLLECTION]) ||
124
            !is_array(
125
                $propertyData[SystemProperties::COLLECTION]
126 1
            )) ? [] : $propertyData[SystemProperties::COLLECTION];
127 1
        $systemPropertyData[SystemProperties::PROPERTY_ID] = Id::PROVISIONAL;
128 1
        $systemPropertyData[SystemProperties::PROPERTY_TYPE] = $type->getType();
129 1
        $systemPropertyData[SystemProperties::PROPERTY_REVISION] = Revision::DRAFT;
130 1
        $systemPropertyData[SystemProperties::PROPERTY_CREATED] = time();
131 1
        $propertyData[SystemProperties::COLLECTION] = $systemPropertyData;
132
133
        // Instantiate the object
134 1
        return new $objectClass($payload, $propertyData);
135
    }
136
}
137