Completed
Push — master ( 1ad7db...177e73 )
by Joschi
02:33
created

ObjectFactory::createNew()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0416

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 20
ccs 10
cts 12
cp 0.8333
rs 9.4285
cc 3
eloc 12
nc 4
nop 3
crap 3.0416

1 Method

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