Completed
Pull Request — master (#15)
by Vytautas
10:02
created

TypesManager::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Svycka\Settings\Type;
4
5
use Zend\ServiceManager\AbstractPluginManager;
6
use Zend\ServiceManager\Exception\InvalidServiceException;
7
use Zend\ServiceManager\Factory\InvokableFactory;
8
9
/**
10
 * Class TypesManager
11
 *
12
 * TypesManager implementation for managing settings type
13
 *
14
 * @method SettingTypeInterface get($name)
15
 *
16
 * @author Vytautas Stankus <[email protected]>
17
 * @license MIT
18
 */
19
class TypesManager extends AbstractPluginManager
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $factories = [
25
        InArrayType::class => InvokableFactory::class,
26
        RegexType::class => InvokableFactory::class,
27
        IntegerType::class => InvokableFactory::class,
28
        FloatType::class => InvokableFactory::class,
29
        StringType::class => InvokableFactory::class,
30
31
        // v2 canonical FQCNs
32
33
        'svyckasettingstypeinarraytype' => InvokableFactory::class,
34
        'svyckasettingstyperegextype' => InvokableFactory::class,
35
        'svyckasettingstypeintegertype' => InvokableFactory::class,
36
        'svyckasettingstypefloattype' => InvokableFactory::class,
37
        'svyckasettingstypestringtype' => InvokableFactory::class,
38
    ];
39
40
    /**
41
     * @var array
42
     */
43
    protected $aliases = [
44
        'inarray' => InArrayType::class,
45
        'regex'   => RegexType::class,
46
        'integer' => IntegerType::class,
47
        'float' => FloatType::class,
48
        'string' => StringType::class,
49
50
        /**
51
         * @deprecated only for BC reasons, will be removed when possible
52
         */
53
        'in_array' => 'inarray',
54
    ];
55
56
    /**
57
     * Whether or not to share by default; default to false
58
     *
59
     * @var bool
60
     */
61
    protected $shareByDefault = false;
62
63
    /**
64
     * Validate the plugin
65
     *
66
     * Checks that the type loaded is instance of SettingTypeInterface.
67
     *
68
     * @param  SettingTypeInterface $type
69
     *
70
     * @return void
71
     * @throws \RuntimeException if invalid
72
     */
73 13
    public function validate($type)
74
    {
75 13
        if ($type instanceof SettingTypeInterface) {
76 12
            return; // we're okay
77
        }
78 1
        throw new InvalidServiceException(sprintf(
79 1
            'SettingType of type "%s" is invalid; must implement "%s"',
80 1
            (is_object($type) ? get_class($type) : gettype($type)),
81
            SettingTypeInterface::class
82 1
        ));
83
    }
84
85
    /**
86
     * Validate the plugin
87
     *
88
     * Checks that the type loaded is instance of SettingTypeInterface.
89
     *
90
     * @param  SettingTypeInterface $type
91
     *
92
     * @return void
93
     * @throws \RuntimeException if invalid
94
     */
95 13
    public function validatePlugin($type)
96
    {
97 13
        $this->validate($type);
98 12
    }
99
}
100