MagicServiceLocatorTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 25
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A serviceLocator() 0 7 2
A make() 0 4 1
1
<?php
2
3
/*
4
 *  @copyright (c) 2019 Mendel <[email protected]>
5
 *  @license see license.txt
6
 */
7
8
namespace drycart\di;
9
10
/**
11
 * Sugar functions for service locator use for DI
12
 * i.e. instantiate some services, and selfmake
13
 * 
14
 * Best pattern is use DI directly, because its flexible,
15
 * but sometimes it easy to use as service locator 
16
 * 
17
 * @author mendel
18
 */
19
trait MagicServiceLocatorTrait
20
{
21
    /**
22
     * Self make, i.e. instantiate current class using DI
23
     * @return mixed
24
     */
25
    public static function make()
26
    {
27
        $di = Di::getInstance();
28
        return $di->get(static::class);
29
    }
30
    
31
    /**
32
     * Instantiate some service or other object using our DI
33
     * @param string $id
34
     * @param array $parameters
35
     * @return mixed
36
     */
37
    protected function serviceLocator(string $id, ?array $parameters = null)
38
    {
39
        $di = Di::getInstance();
40
        if (is_null($parameters)) {
41
            return $di->get($id);
42
        } else {
43
            return $di->make($id, $parameters);
44
        }
45
    }
46
}
47