Completed
Push — master ( e1f08c...557060 )
by Muhammad Kashif
24:26
created

Container::createService()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 9
nc 3
nop 1
crap 4
1
<?php
2
3
namespace Gr8abbasi\Container;
4
5
use Interop\Container\ContainerInterface;
6
use Gr8abbasi\Container\Exception\NotFoundException;
7
use Gr8abbasi\Container\Exception\ContainerException;
8
use Gr8abbasi\Container\Repository\InMemoryServiceRepository;
9
use Gr8abbasi\Container\Factory\ConfigFileServiceFactory;
10
11
/**
12
 * Container Class
13
 */
14
class Container implements ContainerInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    private $services;
20
21
    /**
22
     * @var array
23
     */
24
    private $serviceStore;
25
26
    /**
27
     * @var ServiceRepositoryInterface
28
     */
29
    private $repository;
30
31
    /**
32
     * @var ServiceFactoryInterface
33
     */
34
    private $factory;
0 ignored issues
show
Unused Code introduced by
The property $factory is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
36
    /**
37
     * Constructor for Container
38
     *
39
     * @param array $services
40
     */
41 5
    public function __construct(
42
        array $services = [],
43
        ServiceRepositoryInterface $repository = null,
44
        ServiceFactoryInterface $factory = null
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    ) {
46 5
        $this->services = $services;
47 5
        $this->serviceStore = [];
48 5
        $this->repository = $repository ?: new InMemoryServiceRepository();
0 ignored issues
show
Documentation Bug introduced by
It seems like $repository ?: new \Gr8a...moryServiceRepository() can also be of type object<Gr8abbasi\Contain...emoryServiceRepository>. However, the property $repository is declared as type object<Gr8abbasi\Contain...iceRepositoryInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
49
        // $this->factory = $factory ?: new ConfigFileServiceFactory();
50 5
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 4
    public function get($name)
56
    {
57 4
        if (!$this->has($name)) {
58 1
            throw new NotFoundException('Service not found: ' . $name);
59
        }
60
61 3
        if (!isset($this->serviceStore[$name])) {
62 3
            $this->serviceStore[$name] = $this->createService($name);
63 1
        }
64
65 1
        return $this->serviceStore[$name];
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 4
    public function has($name)
72
    {
73 4
        return isset($this->services[$name]);
74
    }
75
76
    /**
77
     * Create service instance
78
     *
79
     * @param string $name
80
     *
81
     * @return mixed created service
82
     */
83 3
    private function createService($name)
84
    {
85 3
        if (!isset($this->services[$name]) || empty($this->services[$name])) {
86 1
            throw new ContainerException(
87
                'Service should be an array with key value pair: ' . $name
88 1
            );
89
        }
90
91 2
        if (!class_exists($this->services[$name]['class'])) {
92 1
            throw new ContainerException(
93 1
                'Class does not exists: ' . $this->services[$name]['class']
94 1
            );
95
        }
96
97 1
        $service = new \ReflectionClass($this->services[$name]['class']);
98 1
        return $service->newInstance();
99
    }
100
}
101