Passed
Pull Request — master (#78)
by Dmitriy
02:27
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
use Yiisoft\Definitions\Helpers\ArrayDefinitionHelper;
10
11
final class ServiceDefinition implements DefinitionInterface
12
{
13
    /**
14
     * @psalm-var array<string, mixed>
15
     */
16
    private array $calls = [];
17
18 45
    private function __construct(
19
        /**
20
         * @psalm-var class-string
21
         */
22
        private string $class,
23
        private array $constructor = [],
24
    ) {
25 45
    }
26
27
    /**
28
     * @psalm-param class-string $class
29
     */
30 45
    public static function for(string $class, array $constructor = []): self
31
    {
32 45
        return new self($class, $constructor);
33
    }
34
35 11
    public function constructor(array $arguments): self
36
    {
37 11
        $this->constructor = $arguments;
38 11
        return $this;
39
    }
40
41 24
    public function call(string $method, array $arguments = []): self
42
    {
43 24
        $this->calls[$method . '()'] = $arguments;
44 24
        return $this;
45
    }
46
47
    /**
48
     * @psalm-param array<string, array> $methods
49
     */
50 6
    public function calls(array $methods): self
51
    {
52 6
        foreach ($methods as $method => $arguments) {
53 5
            $this->call($method, $arguments);
54
        }
55 6
        return $this;
56
    }
57
58 4
    public function set(string $property, mixed $value): self
59
    {
60 4
        $this->calls['$' . $property] = $value;
61 4
        return $this;
62
    }
63
64
    /**
65
     * @psalm-param array<string, mixed> $properties
66
     */
67 3
    public function sets(array $properties): self
68
    {
69 3
        foreach ($properties as $property => $value) {
70 2
            $this->set($property, $value);
71
        }
72 3
        return $this;
73
    }
74
75 43
    public function resolve(ContainerInterface $container): mixed
76
    {
77 43
        $config = array_merge($this->calls, [
78 43
            ArrayDefinition::CLASS_NAME => $this->class,
79 43
            ArrayDefinition::CONSTRUCTOR => $this->constructor,
80 43
        ]);
81 43
        return ArrayDefinition::fromConfig($config)->resolve($container);
82
    }
83
84 2
    public function merge(self $other): self
85
    {
86 2
        $new = clone $this;
87 2
        $new->class = $other->class;
88 2
        $new->constructor = ArrayDefinitionHelper::mergeArguments($this->constructor, $other->constructor);
89
90 2
        $calls = $this->calls;
91 2
        foreach ($other->calls as $key => $item) {
92 1
            if (str_starts_with($key, '$')) {
93 1
                $calls[$key] = $item;
94 1
            } elseif (str_ends_with($key, '()')) {
95
                /** @psalm-suppress MixedArgument */
96 1
                $arguments = isset($calls[$key])
97 1
                    ? ArrayDefinitionHelper::mergeArguments($calls[$key], $item)
98 1
                    : $item;
99 1
                $calls[$key] = $arguments;
100
            }
101
        }
102 2
        $new->calls = $calls;
103
104 2
        return $new;
105
    }
106
107 1
    public function getClass(): string
108
    {
109 1
        return $this->class;
110
    }
111
112 1
    public function getConstructor(): array
113
    {
114 1
        return $this->constructor;
115
    }
116
117 1
    public function getCalls(): array
118
    {
119 1
        return $this->calls;
120
    }
121
}
122