LocatorAwareTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A serviceLocator() 0 12 2
A setTableLocator() 0 6 1
A getTableLocator() 0 8 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\Locator;
13
14
use CakeDC\Api\Service\ServiceRegistry;
15
16
/**
17
 * Contains method for setting and accessing LocatorInterface instance
18
 */
19
trait LocatorAwareTrait
20
{
21
22
    /**
23
     * Service locator instance
24
     *
25
     * @var \CakeDC\Api\Service\Locator\LocatorInterface
26
     */
27
    protected $_serviceLocator;
28
29
    /**
30
     * Sets the service locator.
31
     * If no parameters are passed, it will return the currently used locator.
32
     *
33
     * @param \CakeDC\Api\Service\Locator\LocatorInterface|null $serviceLocator LocatorInterface instance.
34
     * @return \CakeDC\Api\Service\Locator\LocatorInterface
35
     * @deprecated 3.6.0 Use getTableLocator()/setTableLocator() instead.
36
     */
37
    public function serviceLocator(LocatorInterface $serviceLocator = null)
38
    {
39
        deprecationWarning(
40
            get_called_class() . '::tableLocator() is deprecated. ' .
41
            'Use getTableLocator()/setTableLocator() instead.'
42
        );
43
        if ($serviceLocator !== null) {
44
            $this->setTableLocator($serviceLocator);
45
        }
46
47
        return $this->getTableLocator();
48
    }
49
50
    /**
51
     * Sets the table locator.
52
     *
53
     * @param \Cake\ORM\Locator\LocatorInterface $tableLocator LocatorInterface instance.
54
     * @return $this
55
     */
56
    public function setTableLocator(LocatorInterface $tableLocator)
57
    {
58
        $this->_tableLocator = $tableLocator;
0 ignored issues
show
Bug introduced by
The property _tableLocator does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
60
        return $this;
61
    }
62
63
    /**
64
     * Gets the table locator.
65
     *
66
     * @return \Cake\ORM\Locator\LocatorInterface
67
     */
68
    public function getTableLocator()
69
    {
70
        if (!$this->_tableLocator) {
71
            $this->_tableLocator = TableRegistry::getTableLocator();
72
        }
73
74
        return $this->_tableLocator;
75
    }
76
}
77