Completed
Push — master ( 0590e7...90801e )
by Joschi
08:32
created

ApparatObjectFactory::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.7085

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
ccs 4
cts 7
cp 0.5714
crap 3.7085
rs 9.4285
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Apparat\Object\Infrastru...nvalidArgumentException.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
44
use Apparat\Object\Ports\Object\Article;
45
46
/**
47
 * Apparat object factory
48
 *
49
 * @package Apparat\Object
50
 * @subpackage Apparat\Object\Infrastructure
51
 */
52
class ApparatObjectFactory implements ObjectTypesInterface
53
{
54
    /**
55
     * Type classes
56
     *
57
     * @var array
58
     */
59
    protected static $typeClasses = [
60
        self::ARTICLE => Article::class,
61
        self::AUDIO => false,
62
        self::BOOKMARK => false,
63
        self::CHECKIN => false,
64
        self::CITE => false,
65
        self::CODE => false,
66
        self::CONTACT => false,
67
        self::ADDRESS => false,
68
        self::EVENT => false,
69
        self::FAVOURITE => false,
70
        self::GEO => false,
71
        self::IMAGE => false,
72
        self::ITEM => false,
73
        self::LIKE => false,
74
        self::NOTE => false,
75
        self::PROJECT => false,
76
        self::REPLY => false,
77
        self::REVIEW => false,
78
        self::RSVP => false,
79
        self::VENUE => false,
80
        self::VIDEO => false,
81
    ];
82
83
    /**
84
     * Create and return an apparat object decorator
85
     *
86
     * @param ObjectInterface $object Object
87
     * @return ApparatObjectInterface Apparat object
88
     */
89 3
    public static function create(ObjectInterface $object)
90
    {
91 3
        $objectType = $object->getType()->getType();
92
93
        // If the object type doesn't map to known apparat object class
94 3
        if (!array_key_exists($objectType, self::$typeClasses) || !self::$typeClasses[$objectType]) {
95
            throw new InvalidArgumentException(
96
                sprintf('Unknown apparat object type "%s"', $objectType),
97
                InvalidArgumentException::UNKNOWN_APPARAT_OBJECT_TYPE
98
            );
99
        }
100
101 3
        return Kernel::create(self::$typeClasses[$objectType], [$object]);
102
    }
103
}
104