Completed
Push — master ( 1587a0...b0ef19 )
by Joschi
04:29
created

ObjectFactory::createFromParams()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0416

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 19
ccs 10
cts 12
cp 0.8333
rs 9.4285
cc 3
eloc 12
nc 4
nop 3
crap 3.0416
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 ResourceInterface $objectResource
58
     * @param RepositoryPathInterface $path Repository object path
59
     * @return ObjectInterface Object
60
     * @throws InvalidArgumentException If the object type is undefined
61
     */
62 8
    public static function createFromResource(ResourceInterface $objectResource, RepositoryPathInterface $path)
63
    {
64 8
        $propertyData = $objectResource->getPropertyData();
65
66
        // If the object type is undefined
67 8
        if (!array_key_exists(SystemProperties::COLLECTION, $propertyData) ||
68 7
            !is_array($propertyData[SystemProperties::COLLECTION]) ||
69 7
            empty($propertyData[SystemProperties::COLLECTION]['type'])
70 8
        ) {
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 7
        $objectClass = self::objectClassFromType($path->getType());
79
80
        // Instantiate the object
81 6
        return Kernel::create($objectClass, [$objectResource->getPayload(), $propertyData, $path]);
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 8
    protected static function objectClassFromType(Type $type)
92
    {
93
        // If the object type is invalid
94 8
        $objectType = $type->getType();
95 8
        $objectClass = 'Apparat\\Object\\Application\\Model\\Object\\'.ucfirst($objectType);
96 8
        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 7
        return $objectClass;
104
    }
105
106
    /**
107
     * Create and return a new object
108
     *
109
     * @param string $payload Object payload
110
     * @param array $propertyData Object property data
111
     * @param RepositoryPathInterface $path Repository object path
112
     * @return ObjectInterface Object
113
     */
114 1
    public static function createFromParams($payload = '', array $propertyData = [], RepositoryPathInterface $path)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
115
    {
116
        // Determine the object class
117 1
        $objectClass = self::objectClassFromType($path->getType());
118
119
        // Prepare the system properties collection
120 1
        $systemPropertyData = (empty($propertyData[SystemProperties::COLLECTION]) ||
121
            !is_array(
122
                $propertyData[SystemProperties::COLLECTION]
123 1
            )) ? [] : $propertyData[SystemProperties::COLLECTION];
124 1
        $systemPropertyData[SystemProperties::PROPERTY_ID] = $path->getId()->getId();
125 1
        $systemPropertyData[SystemProperties::PROPERTY_TYPE] = $path->getType()->getType();
126 1
        $systemPropertyData[SystemProperties::PROPERTY_REVISION] = $path->getRevision()->getRevision();
127 1
        $systemPropertyData[SystemProperties::PROPERTY_CREATED] = $path->getCreationDate()->format('U');
128 1
        $propertyData[SystemProperties::COLLECTION] = $systemPropertyData;
129
130
        // Instantiate the object
131 1
        return Kernel::create($objectClass, [$payload, $propertyData, $path]);
132
    }
133
}
134