ExtensionRegistry::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Action;
13
14
use CakeDC\Api\Service\Exception\MissingExtensionException;
15
use Cake\Core\App;
16
use Cake\Core\ObjectRegistry;
17
use Cake\Event\EventDispatcherTrait;
18
19
/**
20
 * Class ExtensionRegistry
21
 *
22
 * @package CakeDC\Api\Service\Action
23
 */
24
class ExtensionRegistry extends ObjectRegistry
25
{
26
27
    use EventDispatcherTrait;
28
29
    /**
30
     * The Action that this collection was initialized with.
31
     *
32
     * @var Action
33
     */
34
    protected $_action = null;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param Action $action Action instance.
40
     */
41 123
    public function __construct(Action $action = null)
42
    {
43 123
        if ($action) {
44 123
            $this->_action = $action;
45 123
        }
46 123
    }
47
    /**
48
     * Should resolve the classname for a given object type.
49
     *
50
     * @param string $class The class to resolve.
51
     * @return string|false The resolved name or false for failure.
52
     */
53 51
    protected function _resolveClassName($class)
54
    {
55 51
        $result = App::className($class, 'Service/Action/Extension', 'Extension');
56 51
        if ($result || strpos($class, '.') !== false) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
57 50
            return $result;
58
        }
59
60 1
        return App::className('CakeDC/Api.' . $class, 'Service/Action/Extension', 'Extension');
61
    }
62
63
    /**
64
     * Throw an exception when the requested object name is missing.
65
     *
66
     * @param string $class The class that is missing.
67
     * @param string $plugin The plugin $class is missing from.
68
     * @return void
69
     * @throws \Exception
70
     */
71 1
    protected function _throwMissingClassError($class, $plugin)
72
    {
73 1
        throw new MissingExtensionException([
74 1
            'class' => $class . 'Extension',
75
            'plugin' => $plugin
76 1
        ]);
77
    }
78
79
    /**
80
     * Create an instance of a given classname.
81
     *
82
     * This method should construct and do any other initialization logic
83
     * required.
84
     *
85
     * @param string $class The class to build.
86
     * @param string $alias The alias of the object.
87
     * @param array $config The Configuration settings for construction
88
     * @return mixed
89
     */
90 50
    protected function _create($class, $alias, $config)
91
    {
92 50
        if (empty($config['action'])) {
93 50
            $config['action'] = $this->_action;
94 50
        }
95 50
        $instance = new $class($this, $config);
96 50
        $this->getEventManager()->on($instance);
97
98 50
        return $instance;
99
    }
100
}
101