Completed
Push — master ( 557060...19a865 )
by Muhammad Kashif
25:46
created

Container::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 7
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\Factory\ConfigFileServiceFactory;
9
use Gr8abbasi\Container\Repository\InMemoryServiceRepository;
10
11
/**
12
 * Container Class
13
 */
14
class Container implements ContainerInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    private $services;
20
21
    /**
22
     * @var ServiceRepositoryInterface
23
     */
24
    private $repository;
25
26
    /**
27
     * @var ServiceFactoryInterface
28
     */
29
    private $factory;
30
31
    /**
32
     * Constructor for Container
33
     *
34
     * @param array $services
35
     */
36 5
    public function __construct(
37
        array $services = [],
38
        ServiceRepositoryInterface $repository = null,
39
        ServiceFactoryInterface $factory = null
40
    ) {
41 5
        $this->services = $services;
42 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...
43 5
        $this->factory = $factory ?: new ConfigFileServiceFactory();
0 ignored issues
show
Documentation Bug introduced by
It seems like $factory ?: new \Gr8abba...figFileServiceFactory() can also be of type object<Gr8abbasi\Contain...nfigFileServiceFactory>. However, the property $factory is declared as type object<Gr8abbasi\Contain...erviceFactoryInterface>. 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...
44 5
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 4
    public function get($name)
50
    {
51 4
        if (!$this->has($name)) {
52 1
            throw new NotFoundException('Service not found: ' . $name);
53
        }
54
55 3
        if (is_null($this->repository->get($name))) {
56
            // foreach($this->services[$name] as $id => $service){
57
58 3
            if (isset($this->services[$name]['arguments'])) {
59 2
                foreach ($this->services[$name]['arguments'] as $argument) {
60
                    // $input = $this->container->get($argument);
61
                    /**
62
                     * TODO
63
                     * Check if dependency already resolved or not
64
                     */
65 2
                    $this->validate($name);
66 1
                    $service = $this->factory->create($this->services[$argument]['class']);
67 1
                    $this->repository->add($name, $service);
68 1
                }
69 1
            }
70
71
            // }
72 2
            $this->validate($name);
73 1
            $service = $this->factory->create($this->services[$name]['class']);
74 1
            $this->repository->add($name, $service);
75 1
        }
76
77 1
        return $this->repository->get($name);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 4
    public function has($name)
84
    {
85 4
        return isset($this->services[$name]);
86
    }
87
88
    /**
89
     * Validate requested service before
90
     * attempt to resolve
91
     *
92
     * @param string $name
93
     *
94
     * @return void
95
     */
96 3
    private function validate($name)
97
    {
98 3
        if (!isset($this->services[$name]) || empty($this->services[$name])) {
99 1
            throw new ContainerException(
100
                'Service should be an array with key value pair: ' . $name
101 1
            );
102
        }
103
104 2
        if (!class_exists($this->services[$name]['class'])) {
105 1
            throw new ContainerException(
106 1
                'Class does not exists: ' . $this->services[$name]['class']
107 1
            );
108
        }
109 1
    }
110
}
111