Completed
Push — master ( 755174...9d7326 )
by Joschi
10:05
created

ObjectFactory::createFromParams()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.027

Importance

Changes 12
Bugs 0 Features 5
Metric Value
cc 6
eloc 25
c 12
b 0
f 5
nc 32
nop 3
dl 0
loc 36
ccs 20
cts 22
cp 0.9091
crap 6.027
rs 8.439
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\Kernel\Ports\Kernel;
40
use Apparat\Object\Application\Service\TypeService;
41
use Apparat\Object\Domain\Model\Object\ObjectInterface;
42
use Apparat\Object\Domain\Model\Object\ResourceInterface;
43
use Apparat\Object\Domain\Model\Object\Type;
44
use Apparat\Object\Domain\Model\Properties\MetaProperties;
45
use Apparat\Object\Domain\Model\Properties\SystemProperties;
46
use Apparat\Object\Domain\Model\Uri\RepositoryLocatorInterface;
47
48
/**
49
 * Object factory
50
 *
51
 * @package Apparat\Object
52
 * @subpackage Apparat\Object\Application
53
 */
54
class ObjectFactory
55
{
56
    /**
57
     * Create an object
58
     *
59
     * @param RepositoryLocatorInterface $locator Repository object locator
60
     * @param ResourceInterface $objectResource
61
     * @return ObjectInterface Object
62
     * @throws InvalidArgumentException If the object type is undefined
63
     */
64 41
    public static function createFromResource(RepositoryLocatorInterface $locator, ResourceInterface $objectResource)
65
    {
66 41
        $propertyData = $objectResource->getPropertyData();
67
68
        // If the object type is undefined
69 41
        if (!array_key_exists(SystemProperties::COLLECTION, $propertyData) ||
70 40
            !is_array($propertyData[SystemProperties::COLLECTION]) ||
71 41
            empty($propertyData[SystemProperties::COLLECTION]['type'])
72
        ) {
73 1
            throw new InvalidArgumentException(
74 1
                'Undefined object type',
75 1
                InvalidArgumentException::UNDEFINED_OBJECT_TYPE
76
            );
77
        }
78
79
        // Determine the object class
80 40
        $objectClass = self::objectClassFromType($locator->getObjectType());
81
82
        // Instantiate the object
83 39
        return Kernel::create($objectClass, [$locator, $objectResource->getPayload(), $propertyData]);
84
    }
85
86
    /**
87
     * Determine and validate the object class name from its type
88
     *
89
     * @param Type $type Object type
90
     * @return string Object class name
91
     * @throws InvalidArgumentException If the object type is invalid
92
     */
93 46
    protected static function objectClassFromType(Type $type)
94
    {
95
        // If the object type is invalid
96 46
        $objectType = $type->getType();
97 46
        $objectClass = 'Apparat\\Object\\Application\\Model\\Object\\'.ucfirst($objectType);
98 46
        if (!TypeService::isEnabled($objectType) || !class_exists($objectClass)) {
99 1
            throw new InvalidArgumentException(
100 1
                sprintf('Invalid object type "%s"', $objectType),
101 1
                InvalidArgumentException::INVALID_OBJECT_TYPE
102
            );
103
        }
104
105 45
        return $objectClass;
106
    }
107
108
    /**
109
     * Create and return a new object
110
     *
111
     * @param RepositoryLocatorInterface $locator Repository object locator
112
     * @param string $payload Object payload
113
     * @param array $propertyData Object property data
114
     * @return ObjectInterface Object
115
     */
116 6
    public static function createFromParams(
117
        RepositoryLocatorInterface $locator,
118
        $payload = '',
119
        array $propertyData = []
120
    ) {
121
        // Determine the object class
122 6
        $objectClass = self::objectClassFromType($locator->getObjectType());
123
124
        // Prepare the system properties collection
125 6
        $systemPropertyData = (empty($propertyData[SystemProperties::COLLECTION]) ||
126
            !is_array(
127 6
                $propertyData[SystemProperties::COLLECTION]
128 6
            )) ? [] : $propertyData[SystemProperties::COLLECTION];
129 6
        $systemPropertyData[SystemProperties::PROPERTY_ID] = $locator->getId()->getId();
130 6
        $systemPropertyData[SystemProperties::PROPERTY_TYPE] = $locator->getObjectType()->getType();
131 6
        $systemPropertyData[SystemProperties::PROPERTY_REVISION] = $locator->getRevision()->getRevision();
132 6
        $systemPropertyData[SystemProperties::PROPERTY_CREATED] =
133 6
        $systemPropertyData[SystemProperties::PROPERTY_MODIFIED] = $locator->getCreationDate();
134 6
        if (empty($systemPropertyData[SystemProperties::PROPERTY_LANGUAGE])) {
135 6
            $systemPropertyData[SystemProperties::PROPERTY_LANGUAGE] = getenv('OBJECT_DEFAULT_LANGUAGE');
136
        }
137 6
        $propertyData[SystemProperties::COLLECTION] = $systemPropertyData;
138
139
        // Prepare the meta properties collection
140 6
        $metaPropertyData = (empty($propertyData[MetaProperties::COLLECTION]) ||
141
            !is_array(
142 6
                $propertyData[MetaProperties::COLLECTION]
143 6
            )) ? [] : $propertyData[MetaProperties::COLLECTION];
144 6
        $metaPropertyData[MetaProperties::PROPERTY_PRIVACY] = getenv('OBJECT_DEFAULT_PRIVACY');
145 6
        $propertyData[MetaProperties::COLLECTION] = $metaPropertyData;
146
147
        // Instantiate the object
148
        /** @var ObjectInterface $object */
149 6
        $object = Kernel::create($objectClass, [$locator, '', $propertyData]);
150 6
        return $object->setPayload($payload);
151
    }
152
}
153