Completed
Push — master ( 81c425...a2bf82 )
by Joschi
02:41
created

ObjectFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 93.55%

Importance

Changes 15
Bugs 0 Features 1
Metric Value
wmc 10
c 15
b 0
f 1
lcom 0
cbo 7
dl 0
loc 82
ccs 29
cts 31
cp 0.9355
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromResource() 0 21 4
A createFromParams() 0 19 3
A objectClassFromType() 0 14 3
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 18
    public static function createFromResource(RepositoryPathInterface $path, ResourceInterface $objectResource)
63
    {
64 18
        $propertyData = $objectResource->getPropertyData();
65
66
        // If the object type is undefined
67 18
        if (!array_key_exists(SystemProperties::COLLECTION, $propertyData) ||
68 17
            !is_array($propertyData[SystemProperties::COLLECTION]) ||
69 17
            empty($propertyData[SystemProperties::COLLECTION]['type'])
70 18
        ) {
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 17
        $objectClass = self::objectClassFromType($path->getType());
79
80
        // Instantiate the object
81 16
        return Kernel::create($objectClass, [$objectResource->getPayload(), $propertyData, $path]);
82
    }
83
84
    /**
85
     * Create and return a new object
86
     *
87
     * @param RepositoryPathInterface $path Repository object path
88
     * @param string $payload Object payload
89
     * @param array $propertyData Object property data
90
     * @return ObjectInterface Object
91
     */
92 1
    public static function createFromParams(RepositoryPathInterface $path, $payload = '', array $propertyData = [])
93
    {
94
        // Determine the object class
95 1
        $objectClass = self::objectClassFromType($path->getType());
96
97
        // Prepare the system properties collection
98 1
        $systemPropertyData = (empty($propertyData[SystemProperties::COLLECTION]) ||
99
            !is_array(
100
                $propertyData[SystemProperties::COLLECTION]
101 1
            )) ? [] : $propertyData[SystemProperties::COLLECTION];
102 1
        $systemPropertyData[SystemProperties::PROPERTY_ID] = $path->getId()->getId();
103 1
        $systemPropertyData[SystemProperties::PROPERTY_TYPE] = $path->getType()->getType();
104 1
        $systemPropertyData[SystemProperties::PROPERTY_REVISION] = $path->getRevision()->getRevision();
105 1
        $systemPropertyData[SystemProperties::PROPERTY_CREATED] = $path->getCreationDate()->format('U');
106 1
        $propertyData[SystemProperties::COLLECTION] = $systemPropertyData;
107
108
        // Instantiate the object
109 1
        return Kernel::create($objectClass, [$payload, $propertyData, $path]);
110
    }
111
112
    /**
113
     * Determine and validate the object class name from its type
114
     *
115
     * @param Type $type Object type
116
     * @return string Object class name
117
     * @throws InvalidArgumentException If the object type is invalid
118
     */
119 18
    protected static function objectClassFromType(Type $type)
120
    {
121
        // If the object type is invalid
122 18
        $objectType = $type->getType();
123 18
        $objectClass = 'Apparat\\Object\\Application\\Model\\Object\\'.ucfirst($objectType);
124 18
        if (!Type::isValidType($objectType) || !class_exists($objectClass)) {
125 1
            throw new InvalidArgumentException(
126 1
                sprintf('Invalid object type "%s"', $objectType),
127
                InvalidArgumentException::INVALID_OBJECT_TYPE
128 1
            );
129
        }
130
131 17
        return $objectClass;
132
    }
133
}
134