Completed
Push — master ( 7c0043...0bd31b )
by Joschi
03:18
created

ObjectFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 94.44%

Importance

Changes 19
Bugs 0 Features 4
Metric Value
c 19
b 0
f 4
dl 0
loc 88
ccs 34
cts 36
cp 0.9444
rs 10
wmc 11
lcom 0
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromResource() 0 21 4
A objectClassFromType() 0 14 3
B createFromParams() 0 25 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\Kernel\Ports\Kernel;
40
use Apparat\Object\Domain\Model\Object\ObjectInterface;
41
use Apparat\Object\Domain\Model\Object\ResourceInterface;
42
use Apparat\Object\Domain\Model\Object\Type;
43
use Apparat\Object\Domain\Model\Path\RepositoryPathInterface;
44
use Apparat\Object\Domain\Model\Properties\SystemProperties;
45
46
/**
47
 * Object factory
48
 *
49
 * @package Apparat\Object
50
 * @subpackage Apparat\Object\Application
51
 */
52
class ObjectFactory
53
{
54
    /**
55
     * Create an object
56
     *
57
     * @param RepositoryPathInterface $path Repository object path
58
     * @param ResourceInterface $objectResource
59
     * @return ObjectInterface Object
60
     * @throws InvalidArgumentException If the object type is undefined
61
     */
62 27
    public static function createFromResource(RepositoryPathInterface $path, ResourceInterface $objectResource)
63
    {
64 27
        $propertyData = $objectResource->getPropertyData();
65
66
        // If the object type is undefined
67 27
        if (!array_key_exists(SystemProperties::COLLECTION, $propertyData) ||
68 26
            !is_array($propertyData[SystemProperties::COLLECTION]) ||
69 26
            empty($propertyData[SystemProperties::COLLECTION]['type'])
70 27
        ) {
71 1
            throw new InvalidArgumentException(
72 1
                'Undefined object type',
73
                InvalidArgumentException::UNDEFINED_OBJECT_TYPE
74 1
            );
75
        }
76
77
        // Determine the object class
78 26
        $objectClass = self::objectClassFromType($path->getType());
79
80
        // Instantiate the object
81 25
        return Kernel::create($objectClass, [$path, $objectResource->getPayload(), $propertyData]);
82
    }
83
84
    /**
85
     * Determine and validate the object class name from its type
86
     *
87
     * @param Type $type Object type
88
     * @return string Object class name
89
     * @throws InvalidArgumentException If the object type is invalid
90
     */
91 30
    protected static function objectClassFromType(Type $type)
92
    {
93
        // If the object type is invalid
94 30
        $objectType = $type->getType();
95 30
        $objectClass = 'Apparat\\Object\\Application\\Model\\Object\\'.ucfirst($objectType);
96 30
        if (!Type::isValidType($objectType) || !class_exists($objectClass)) {
97 1
            throw new InvalidArgumentException(
98 1
                sprintf('Invalid object type "%s"', $objectType),
99
                InvalidArgumentException::INVALID_OBJECT_TYPE
100 1
            );
101
        }
102
103 29
        return $objectClass;
104
    }
105
106
    /**
107
     * Create and return a new object
108
     *
109
     * @param RepositoryPathInterface $path Repository object path
110
     * @param string $payload Object payload
111
     * @param array $propertyData Object property data
112
     * @return ObjectInterface Object
113
     */
114 4
    public static function createFromParams(RepositoryPathInterface $path, $payload = '', array $propertyData = [])
115
    {
116
        // Determine the object class
117 4
        $objectClass = self::objectClassFromType($path->getType());
118
119
        // Prepare the system properties collection
120 4
        $systemPropertyData = (empty($propertyData[SystemProperties::COLLECTION]) ||
121
            !is_array(
122
                $propertyData[SystemProperties::COLLECTION]
123 4
            )) ? [] : $propertyData[SystemProperties::COLLECTION];
124 4
        $systemPropertyData[SystemProperties::PROPERTY_ID] = $path->getId()->getId();
125 4
        $systemPropertyData[SystemProperties::PROPERTY_TYPE] = $path->getType()->getType();
126 4
        $systemPropertyData[SystemProperties::PROPERTY_REVISION] = $path->getRevision()->getRevision();
127 4
        $systemPropertyData[SystemProperties::PROPERTY_CREATED] =
128 4
        $systemPropertyData[SystemProperties::PROPERTY_MODIFIED] = $path->getCreationDate();
129 4
        if (empty($systemPropertyData[SystemProperties::PROPERTY_LANGUAGE])) {
130 4
            $systemPropertyData[SystemProperties::PROPERTY_LANGUAGE] = getenv('OBJECT_DEFAULT_LANGUAGE');
131 4
        }
132 4
        $propertyData[SystemProperties::COLLECTION] = $systemPropertyData;
133
134
        // Instantiate the object
135
        /** @var ObjectInterface $object */
136 4
        $object = Kernel::create($objectClass, [$path, '', $propertyData]);
137 4
        return $object->setPayload($payload);
138
    }
139
}
140