Completed
Push — master ( 91d196...1cc552 )
by Joschi
02:38
created

ObjectFactory::createFromResource()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.0534
cc 4
eloc 10
nc 2
nop 2
crap 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\Object\Domain\Model\Object\ObjectInterface;
40
use Apparat\Object\Domain\Model\Object\ResourceInterface;
41
use Apparat\Object\Domain\Model\Object\Type;
42
use Apparat\Object\Domain\Model\Path\RepositoryPath;
43
use Apparat\Object\Domain\Model\Properties\SystemProperties;
44
45
/**
46
 * Object factory
47
 *
48
 * @package Apparat\Object
49
 * @subpackage Apparat\Object\Application
50
 */
51
class ObjectFactory
52
{
53
    /**
54
     * Create an object
55
     *
56
     * @param ResourceInterface $objectResource
57
     * @param RepositoryPath $path Repository object path
58
     * @return ObjectInterface Object
59
     * @throws InvalidArgumentException If the object type is undefined
60
     */
61 8
    public static function createFromResource(ResourceInterface $objectResource, RepositoryPath $path)
62
    {
63 8
        $propertyData = $objectResource->getPropertyData();
64
65
        // If the object type is undefined
66 8
        if (!array_key_exists(SystemProperties::COLLECTION, $propertyData) ||
67 7
            !is_array($propertyData[SystemProperties::COLLECTION]) ||
68 7
            empty($propertyData[SystemProperties::COLLECTION]['type'])
69 8
        ) {
70 1
            throw new InvalidArgumentException(
71 1
                'Undefined object type',
72
                InvalidArgumentException::UNDEFINED_OBJECT_TYPE
73 1
            );
74
        }
75
76
        // Determine the object class
77 7
        $objectClass = self::objectClassFromType($path->getType());
78
79
        // Instantiate the object
80 6
        return new $objectClass($objectResource->getPayload(), $propertyData, $path);
81
    }
82
83
    /**
84
     * Create and return a new object
85
     *
86
     * @param Type $type Object type
87
     * @param string $payload Object payload
88
     * @param array $propertyData Object property data
89
     * @return ObjectInterface Object
90
     */
91
    public static function createNew(Type $type, $payload = '', array $propertyData = []) {
92
93
        // Determine the object class
94
        $objectClass = self::objectClassFromType($type);
95
96
        // Instantiate the object
97
        return new $objectClass($payload, $propertyData);
98
    }
99
100
    /**
101
     * Determine and validate the object class name from its type
102
     *
103
     * @param Type $type Object type
104
     * @return string Object class name
105
     * @throws InvalidArgumentException If the object type is invalid
106
     */
107 7
    protected static function objectClassFromType(Type $type) {
108
109
        // If the object type is invalid
110 7
        $objectType = $type->getType();
111 7
        $objectClass = 'Apparat\\Object\\Application\\Model\\Object\\'.ucfirst($objectType);
112 7
        if (!Type::isValidType($objectType) || !class_exists($objectClass)) {
113 1
            throw new InvalidArgumentException(
114 1
                sprintf('Invalid object type "%s"', $objectType),
115
                InvalidArgumentException::INVALID_OBJECT_TYPE
116 1
            );
117
        }
118
119 6
        return $objectClass;
120
    }
121
}
122