TypeService::supportsType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
/**
4
 * apparat/object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Server
8
 * @subpackage  Apparat\Object\Application\Service
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\Service;
38
39
use Apparat\Object\Application\Contract\ObjectTypesInterface;
40
use Apparat\Object\Domain\Contract\TypeServiceInterface;
41
42
/**
43
 * Type service
44
 *
45
 * @package Apparat\Object
46
 * @subpackage Apparat\Object\Application
47
 */
48
class TypeService implements TypeServiceInterface, ObjectTypesInterface
49
{
50
    /**
51
     * Enabled types
52
     *
53
     * @var array
54
     */
55
    protected static $enabledTypes = [
56
        self::ARTICLE => false,
57
        self::AUDIO => false,
58
        self::BOOKMARK => false,
59
        self::CHECKIN => false,
60
        self::CITE => false,
61
        self::CODE => false,
62
        self::CONTACT => false,
63
        self::ADDRESS => false,
64
        self::EVENT => false,
65
        self::FAVOURITE => false,
66
        self::GEO => false,
67
        self::IMAGE => false,
68
        self::ITEM => false,
69
        self::LIKE => false,
70
        self::NOTE => false,
71
        self::PROJECT => false,
72
        self::REPLY => false,
73
        self::REVIEW => false,
74
        self::RSVP => false,
75
        self::VENUE => false,
76
        self::VIDEO => false,
77
    ];
78
79
    /**
80
     * Return whether a particular object type is supported
81
     *
82
     * Non-static variant for domain usage
83
     *
84
     * @param string $type Object type
85
     * @return boolean Object type is supported
86
     */
87 118
    public function supportsType($type)
88
    {
89 118
        return self::isEnabled($type);
90
    }
91
92
    /**
93
     * Enable an object type
94
     *
95
     * @param $type
96
     * @throws OutOfBoundsException If the object type is invalid
97
     */
98 3
    public static function enableType($type)
99
    {
100 3
        self::$enabledTypes[self::validateType($type)] = true;
101 2
    }
102
103
    /**
104
     * Return whether a particular object type is enabled
105
     *
106
     * @param string $type Object type
107
     * @return boolean Object type is enabled
108
     */
109 119
    public static function isEnabled($type)
110
    {
111
        try {
112 119
            return self::$enabledTypes[self::validateType($type)];
113 22
        } catch (OutOfBoundsException $e) {
114 22
            return false;
115
        }
116
    }
117
118
    /**
119
     * Return all object types that are currently supported
120
     *
121
     * @return array Supported object types
122
     */
123 9
    public static function getSupportedTypes()
124
    {
125 9
        $supportedTypes = array_keys(array_filter(self::$enabledTypes));
126 9
        return array_combine($supportedTypes, $supportedTypes);
127
    }
128
129
    /**
130
     * Validate a type
131
     *
132
     * @param string $type Object type
133
     * @return string Validated object type
134
     * @throws OutOfBoundsException If the object type is invalid
135
     */
136 121
    protected static function validateType($type)
137
    {
138 121
        $type = trim($type);
139
140
        // If the object type is invalid
141 121
        if (!strlen($type) || !array_key_exists($type, self::$enabledTypes)) {
142 23
            throw new OutOfBoundsException(
143 23
                sprintf('Invalid object type "%s"', $type),
144 23
                OutOfBoundsException::INVALID_OBJECT_TYPE
145
            );
146
        }
147
148 103
        return $type;
149
    }
150
}
151