Test Setup Failed
Pull Request — master (#28)
by
unknown
02:17
created

ExtensionRegistry::_resolveClassName()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 25
ccs 0
cts 9
cp 0
crap 42
rs 8.439
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 118
    public function __construct(Service $service = null)
42
    {
43 118
        if ($service) {
44 118
            $this->_service = $service;
45 118
        }
46 118
    }
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
        
57
        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...
58
            return $result;
59
        }
60
61
        $result = App::className('CakeDC/Api.' . $class, 'Service/Extension', 'Extension');
62
        
63
        if(class_exists($result)) {
64
65
            return $result;
66
        }
67
        else {
68
69
            $result = App::className($class, 'Service/Action/Extension', 'Extension');
70
            
71
            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...
72
                return $result;
73
            }
74
75
            return App::className('CakeDC/Api.' . $class, 'Service/Action/Extension', 'Extension');
76
        }
77
    }
78
79
    /**
80
     * Throw an exception when the requested object name is missing.
81
     *
82
     * @param string $class The class that is missing.
83
     * @param string $plugin The plugin $class is missing from.
84
     * @return void
85
     * @throws \Exception
86
     */
87
    protected function _throwMissingClassError($class, $plugin)
88
    {
89
        throw new MissingExtensionException([
90
            'class' => $class . 'Extension',
91
            'plugin' => $plugin
92
        ]);
93
    }
94
95
    /**
96
     * Create an instance of a given classname.
97
     *
98
     * This method should construct and do any other initialization logic
99
     * required.
100
     *
101
     * @param string $class The class to build.
102
     * @param string $alias The alias of the object.
103
     * @param array $config The Configuration settings for construction
104
     * @return mixed
105
     */
106
    protected function _create($class, $alias, $config)
107
    {
108
        if (empty($config['service'])) {
109
            $config['service'] = $this->_service;
110
        }
111
        $instance = new $class($this, $config);
112
        $this->eventManager()->on($instance);
113
114
        return $instance;
115
    }
116
}
117