ServiceLocator::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Service Locator Class
5
 * @category    Ticaje
6
 * @author      Max Demian <[email protected]>
7
 */
8
9
namespace Ticaje\Contract\Application\Service;
10
11
/**
12
 * Class ServiceLocator
13
 * @package Ticaje\Base\Application\Service
14
 * This class kind of performs gateway pattern design in order to provide consistency to our Model Domain
15
 * According to DI principle it complies to separate the Model Domain from any framework or higher level platform.
16
 */
17
class ServiceLocator implements ServiceLocatorInterface
18
{
19
    protected $instances = [];
20
21
    /**
22
     * @param       $class
23
     * @param array $arguments
24
     *
25
     * @return mixed
26
     * Low level instantiation to keep me save from tightly coupling
27
     */
28
    public function create($class, array $arguments = [])
29
    {
30
        $classHash = hash('md5', $class . implode("", array_values($arguments)));
31
        if (!isset($this->instances[$classHash])) {
32
            $this->instances[$classHash] = new $class($arguments);
33
        }
34
35
        return $this->instances[$classHash];
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function get($class)
42
    {
43
        return $this->create($class);
44
    }
45
}
46