Completed
Push — master ( 952773...a13902 )
by Evgeny
02:39
created

ExtensionRegistry::_throwMissingClassError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 9.4285
1
<?php
2
/**
3
 * Copyright 2016, 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, 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;
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
23
 */
24
class ExtensionRegistry extends ObjectRegistry
25
{
26
27
    use EventDispatcherTrait;
28
29
    /**
30
     * The Service that this collection was initialized with.
31
     *
32
     * @var Service
33
     */
34
    protected $_service = null;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param Service $service Service instance.
40
     */
41
    public function __construct(Service $service = null)
42
    {
43
        if ($service) {
44
            $this->_service = $service;
45
        }
46
    }
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
    protected function _resolveClassName($class)
54
    {
55
        $result = App::className($class, 'Service/Extension', 'Extension');
56
        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
            return $result;
58
        }
59
60
        return App::className('CakeDC/Api.' . $class, 'Service/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
    protected function _throwMissingClassError($class, $plugin)
72
    {
73
        throw new MissingExtensionException([
74
            'class' => $class . 'Extension',
75
            'plugin' => $plugin
76
        ]);
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
    protected function _create($class, $alias, $config)
91
    {
92
        if (empty($config['service'])) {
93
            $config['service'] = $this->_service;
94
        }
95
        $instance = new $class($this, $config);
96
        $this->eventManager()->on($instance);
97
98
        return $instance;
99
    }
100
}
101