Strategy::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cekta\DI;
6
7
use Cekta\DI\Exception\NotFound;
8
use Psr\Container\ContainerInterface;
9
10
class Strategy implements ContainerInterface
11
{
12
    /**
13
     * @var ContainerInterface[]
14
     */
15
    private $providers;
16
17 9
    public function __construct(ContainerInterface ...$provider)
18
    {
19 9
        $this->providers = $provider;
20 9
    }
21
22 6
    public function get($id)
23
    {
24 6
        $provider = $this->findProvider($id);
25 6
        if ($provider === null) {
26 3
            throw new NotFound($id);
27
        }
28 3
        return $provider->get($id);
29
    }
30
31 3
    public function has($id): bool
32
    {
33 3
        return $this->findProvider($id) !== null;
34
    }
35
36 9
    private function findProvider(string $id): ?ContainerInterface
37
    {
38 9
        foreach ($this->providers as $provider) {
39 9
            if ($provider->has($id)) {
40 6
                return $provider;
41
            }
42
        }
43 6
        return null;
44
    }
45
}
46