Passed
Pull Request — master (#78)
by Dmitriy
10:08 queued 07:40
created

ServiceDefinition::for()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Definitions;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Definitions\Contract\DefinitionInterface;
9
10
final class ServiceDefinition implements DefinitionInterface
11
{
12
    private array $calls = [];
13
14 43
    private function __construct(
15
        private string $class,
16
        private array $constructor = [],
17
    ) {
18 43
    }
19
20 43
    public static function for(string $class, array $constructor = []): self
21
    {
22 43
        return new self($class, $constructor);
23
    }
24
25 10
    public function constructor(array $arguments): self
26
    {
27 10
        $this->constructor = $arguments;
28 10
        return $this;
29
    }
30
31 23
    public function call(string $method, array $arguments = []): self
32
    {
33 23
        $this->calls[$method . '()'] = $arguments;
34 23
        return $this;
35
    }
36
37 6
    public function calls(array $methods): self
38
    {
39 6
        foreach ($methods as $method => $arguments) {
40 5
            $this->call($method, $arguments);
41
        }
42 6
        return $this;
43
    }
44
45 3
    public function set(string $property, mixed $value): self
46
    {
47 3
        $this->calls['$' . $property] = $value;
48 3
        return $this;
49
    }
50
51 3
    public function sets(array $properties): self
52
    {
53 3
        foreach ($properties as $property => $value) {
54 2
            $this->set($property, $value);
55
        }
56 3
        return $this;
57
    }
58
59 43
    public function resolve(ContainerInterface $container): mixed
60
    {
61 43
        $config = array_merge($this->calls, [
62 43
            ArrayDefinition::CLASS_NAME => $this->class,
63 43
            ArrayDefinition::CONSTRUCTOR => $this->constructor,
64 43
        ]);
65 43
        return ArrayDefinition::fromConfig($config)->resolve($container);
66
    }
67
68
    public function merge(self $other)
69
    {
70
        // TBD
71
    }
72
}
73