Completed
Push — develop ( b9b8dc...f49312 )
by Baptiste
02:08
created

Definitions::fetchArgumentValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 8
nc 4
nop 1
crap 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose;
5
6
use Innmind\Compose\{
7
    Definition\Name,
8
    Definition\Service,
9
    Definition\Service\Argument,
10
    Exception\ArgumentNotProvided,
11
    Exception\ArgumentNotDefined,
12
    Exception\AtLeastOneServiceMustBeExposed
13
};
14
use Innmind\Immutable\{
15
    Sequence,
16
    MapInterface,
17
    Map,
18
    Pair
19
};
20
21
final class Definitions
22
{
23
    private $definitions;
24
    private $exposed;
25
    private $arguments;
26
27 23
    public function __construct(Arguments $arguments, Service ...$definitions)
28
    {
29 23
        $this->arguments = $arguments;
30 23
        $this->definitions = Sequence::of(...$definitions)->reduce(
31 23
            new Map('string', Service::class),
32 23
            static function(Map $definitions, Service $definition): Map {
33 22
                return $definitions->put(
34 22
                    (string) $definition->name(),
35 22
                    $definition
36
                );
37 23
            }
38
        );
39 23
        $this->exposed = $this
40 23
            ->definitions
41 23
            ->filter(static function(string $name, Service $definition): bool {
42 22
                return $definition->exposed();
43 23
            })
44 23
            ->map(static function(string $name, Service $definition): Pair {
45 22
                return new Pair(
46 22
                    (string) $definition->exposedAs(),
47 22
                    $definition
48
                );
49 23
            });
50
51 23
        if ($this->exposed->size() === 0) {
52 1
            throw new AtLeastOneServiceMustBeExposed;
53
        }
54 22
    }
55
56
    /**
57
     * @param MapInterface<string, mixed> $arguments
58
     */
59 15
    public function inject(MapInterface $arguments): self
60
    {
61 15
        $self = clone $this;
62 15
        $self->arguments = $self->arguments->bind($arguments);
63
64 15
        return $self;
65
    }
66
67 9
    public function build(Name $name): object
68
    {
69 9
        return $this->get($name)->build($this);
70
    }
71
72 14
    public function arguments(): Arguments
73
    {
74 14
        return $this->arguments;
75
    }
76
77 6
    public function has(Name $name): bool
78
    {
79 6
        if ($this->definitions->contains((string) $name)) {
80 4
            return true;
81
        }
82
83 4
        return $this->exposed->contains((string) $name);
84
    }
85
86 13
    public function get(Name $name): Service
87
    {
88
        try {
89 13
            return $this->definitions->get((string) $name);
90 5
        } catch (\Exception $e) {
91 5
            return $this->exposed->get((string) $name);
92
        }
93
    }
94
}
95