Completed
Push — master ( a373f3...4d04c4 )
by Joschi
03:00
created

ApparatObjectFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 52
ccs 7
cts 7
cp 1
rs 10
wmc 3
lcom 1
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 14 3
1
<?php
2
3
/**
4
 * apparat/object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Infrastructure\Factory
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\Infrastructure\Factory;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Domain\Contract\ObjectTypesInterface;
41
use Apparat\Object\Domain\Model\Object\ObjectInterface;
42
use Apparat\Object\Ports\Contract\ApparatObjectInterface;
43
use Apparat\Object\Ports\Exceptions\InvalidArgumentException as PortsInvalidArgumentException;
44
use Apparat\Object\Ports\Object\Article;
45
use Apparat\Object\Ports\Object\Contact;
46
47
/**
48
 * Apparat object factory
49
 *
50
 * @package Apparat\Object
51
 * @subpackage Apparat\Object\Infrastructure
52
 */
53
class ApparatObjectFactory implements ObjectTypesInterface
54
{
55
    /**
56
     * Type classes
57
     *
58
     * @var array
59
     */
60
    protected static $typeClasses = [
61
        self::ARTICLE => Article::class,
62
        self::AUDIO => false,
63
        self::BOOKMARK => false,
64
        self::CHECKIN => false,
65
        self::CITE => false,
66
        self::CODE => false,
67
        self::CONTACT => Contact::class,
68
        self::ADDRESS => false,
69
        self::EVENT => false,
70
        self::FAVOURITE => false,
71
        self::GEO => false,
72
        self::IMAGE => false,
73
        self::ITEM => false,
74
        self::LIKE => false,
75
        self::NOTE => false,
76
        self::PROJECT => false,
77
        self::REPLY => false,
78
        self::REVIEW => false,
79
        self::RSVP => false,
80
        self::VENUE => false,
81
        self::VIDEO => false,
82
    ];
83
84
    /**
85
     * Create and return an apparat object decorator
86
     *
87
     * @param ObjectInterface $object Object
88
     * @return ApparatObjectInterface Apparat object
89
     */
90 10
    public static function create(ObjectInterface $object)
91
    {
92 10
        $objectType = $object->getType()->getType();
93
94
        // If the object type doesn't map to known apparat object class
95 10
        if (!array_key_exists($objectType, static::$typeClasses) || !static::$typeClasses[$objectType]) {
96 1
            throw new PortsInvalidArgumentException(
97 1
                sprintf('Unknown apparat object type "%s"', $objectType),
98 1
                PortsInvalidArgumentException::UNKNOWN_APPARAT_OBJECT_TYPE
99
            );
100
        }
101
102 9
        return Kernel::create(self::$typeClasses[$objectType], [$object]);
103
    }
104
}
105