Passed
Push — master ( ef8c69...a86aba )
by Alexey
07:15 queued 03:20
created

ServiceInflector::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Container;
4
5
use InvalidArgumentException;
6
use ReflectionMethod;
7
use Venta\Contracts\Container\ArgumentResolver as ArgumentResolverContract;
8
use Venta\Contracts\Container\ServiceInflector as ServiceInflectorContract;
9
10
/**
11
 * Class ServiceInflector
12
 *
13
 * @package Venta\Container
14
 */
15
final class ServiceInflector implements ServiceInflectorContract
16
{
17
18
    /**
19
     * @var ArgumentResolverContract
20
     */
21
    private $argumentResolver;
22
23
    /**
24
     * A list of methods with arguments which will be invoked on the subject object.
25
     *
26
     * @var string[][]
27
     */
28
    private $inflections = [];
29
30
    /**
31
     * ServiceInflector constructor.
32
     *
33
     * @param ArgumentResolverContract $argumentResolver
34
     */
35 33
    public function __construct(ArgumentResolverContract $argumentResolver)
36
    {
37 33
        $this->argumentResolver = $argumentResolver;
38 33
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43 5
    public function add(string $id, string $method, array $arguments = [])
44
    {
45 5
        if (!method_exists($id, $method)) {
46 1
            throw new InvalidArgumentException(sprintf('Method "%s" not found in "%s".', $method, $id));
47
        }
48
49 4
        $this->inflections[$id][$method] = $arguments;
50 4
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55 23
    public function apply($object)
56
    {
57 23
        foreach ($this->inflections as $type => $methods) {
58 4
            if (!$object instanceof $type) {
59 3
                continue;
60
            }
61
62 4
            foreach ($methods as $method => $inflection) {
63
                // $inflection may be array of arguments to pass to the method
64
                // OR prepared closure to call with the provided object context.
65 4
                if (is_array($inflection)) {
66
67
                    // Reflect and resolve method arguments.
68 4
                    $arguments = $this->argumentResolver->resolve(new ReflectionMethod($type, $method), $inflection);
69
70
                    // Wrap calling method with closure to avoid reflecting / resolving each time inflection applied.
71 3
                    $this->inflections[$type][$method] = $inflection = function () use ($method, $arguments) {
72 3
                        $this->$method(...$arguments);
73 3
                    };
74
                }
75
76
                // We have callable inflection ready, so we simply swap the context to provided object and call it.
77 3
                $inflection->call($object);
78
            }
79
        }
80
81 22
        return $object;
82
    }
83
84
}