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

ServiceAwareTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 73
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setServiceLocator() 0 3 1
A getServiceLocator() 0 8 2
A loadService() 0 21 5
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\ObjectRegistry;
19
20
/**
21
 * Service Aware Trait
22
 *
23
 * CakePHP style locator to load service classes
24
 */
25
trait ServiceAwareTrait
26
{
27
    /**
28
     * Service Locator Registry
29
     *
30
     * @var \Cake\Core\ObjectRegistry
31
     */
32
    protected $serviceLocator;
33
34
    /**
35
     * Default Service Locator Class
36
     *
37
     * @var string
38
     */
39
    protected $defaultServiceLocator = ServiceLocator::class;
40
41
    /**
42
     * Load a service
43
     *
44
     * If the service is in a subfolder, this folder name will be stripped for the property name when assigned.
45
     *
46
     * @param string $service Service Name
47
     * @param array $constructorArgs Constructor Args
48
     * @param bool $assignProperty Assigns the service to a class property of the same name as  the service
49
     * @return object
50
     */
51
    public function loadService($service, array $constructorArgs = [], $assignProperty = true)
52
    {
53
        list(, $name) = pluginSplit($service);
54
55
        if (strpos($name, '/') !== false) {
56
            $name = substr($name, strrpos($name, '/') + 1);
57
        }
58
59
        if ($assignProperty && isset($this->{$name})) {
60
            return $this->{$name};
61
        }
62
63
        $serviceInstance = $this->getServiceLocator()->load($service, $constructorArgs);
64
65
        if (!$assignProperty) {
66
            return $serviceInstance;
67
        }
68
69
        $this->{$name} = $serviceInstance;
70
71
        return $serviceInstance;
72
    }
73
74
    /**
75
     * Get the service locator
76
     *
77
     * @return \Cake\Core\ObjectRegistry
78
     */
79
    public function getServiceLocator()
80
    {
81
        if (empty($this->serviceLocator)) {
82
            $class = $this->defaultServiceLocator;
83
            $this->serviceLocator = new $class();
84
        }
85
86
        return $this->serviceLocator;
87
    }
88
89
    /**
90
     * Sets the service locator
91
     *
92
     * @param \Cake\Core\ObjectRegistry $locator Locator
93
     * @return void
94
     */
95
    public function setServiceLocator(ObjectRegistry $locator)
96
    {
97
        $this->serviceLocator = $locator;
98
    }
99
}
100