Passed
Push — master ( 964e1c...49cc4f )
by Florian
01:49 queued 12s
created

ServiceLocator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 59
ccs 12
cts 14
cp 0.8571
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A _create() 0 9 2
A _resolveClassName() 0 3 2
A _throwMissingClassError() 0 16 2
1
<?php
2
/**
3
 * Copyright (c) Florian Krämer
4
 *
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright     Copyright (c) Florian Krämer
10
 * @link          https://github.com/burzum/cakephp-service-layer
11
 * @since         1.0.0
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
declare(strict_types = 1);
15
16
namespace Burzum\CakeServiceLayer\Service;
17
18
use Cake\Core\App;
19
use Cake\Core\ObjectRegistry;
20
use RuntimeException;
21
22
/**
23
 * Service Locator
24
 *
25
 * CakePHP style locator to load service classes
26
 */
27
class ServiceLocator extends ObjectRegistry
28
{
29
    /**
30
     * Should resolve the class name for a given object type.
31
     *
32
     * @param string $class The class to resolve.
33
     * @return string|null The resolved name or null for failure.
34
     */
35 5
    protected function _resolveClassName(string$class): ?string
36
    {
37 5
        return App::className($class, 'Service', 'Service') ?: null;
38
    }
39
40
    /**
41
     * Throw an exception when the requested object name is missing.
42
     *
43
     * @param string $class The class that is missing.
44
     * @param string $plugin The plugin $class is missing from.
45
     * @return void
46
     * @throws \Exception
47
     */
48 1
    protected function _throwMissingClassError(string $class, ?string $plugin): void
49
    {
50 1
        if (!empty($plugin)) {
51
            $message = sprintf(
52
                'Service `%s` in plugin `%s` not found.',
53
                $class,
54
                $plugin
55
            );
56
        } else {
57 1
            $message = sprintf(
58 1
                'Service class `%s` not found.',
59
                $class
60
            );
61
        }
62
63 1
        throw new RuntimeException($message);
64
    }
65
66
    /**
67
     * Create an instance of a given classname.
68
     *
69
     * The passed config array will be used as constructor args for the new
70
     * object.
71
     *
72
     * @param string $class The class to build.
73
     * @param string $alias The alias of the object.
74
     * @param array $config The Configuration settings for construction
75
     * @return object
76
     */
77 4
    protected function _create($class, $alias, $config)
78
    {
79 4
        if (empty($config)) {
80 3
            return new $class();
81
        }
82
83 1
        $args = array_values((array)$config);
84
85 1
        return new $class(...$args);
86
    }
87
}
88