Passed
Pull Request — main (#4)
by Michael
11:30
created

CallProxy::__set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 5
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\EnhancedContainer\Core;
6
7
use Illuminate\Support\Traits\ForwardsCalls;
8
use MichaelRubel\EnhancedContainer\Call;
9
use MichaelRubel\EnhancedContainer\Traits\InteractsWithContainer;
10
11
class CallProxy implements Call
12
{
13
    use InteractsWithContainer, ForwardsCalls;
0 ignored issues
show
introduced by
The trait MichaelRubel\EnhancedCon...\InteractsWithContainer requires some properties which are not provided by MichaelRubel\EnhancedContainer\Core\CallProxy: $contextual, $map
Loading history...
14
15
    /**
16
     * @var object
17
     */
18
    protected object $instance;
19
20
    /**
21
     * CallProxy constructor.
22
     *
23
     * @param  object|string  $class
24
     * @param  array  $dependencies
25
     * @param  string|null  $context
26
     */
27 56
    public function __construct(
28
        object | string $class,
29
        array $dependencies = [],
30
        ?string $context = null
31
    ) {
32 56
        $this->instance = $this->resolvePassedClass($class, $dependencies, $context);
33
    }
34
35
    /**
36
     * Gets the internal property by name.
37
     *
38
     * @param  string  $property
39
     *
40
     * @return mixed
41
     */
42 7
    public function getInternal(string $property): mixed
43
    {
44 7
        return $this->{$property};
45
    }
46
47
    /**
48
     * Perform the container call.
49
     *
50
     * @param  object  $service
51
     * @param  string  $method
52
     * @param  array  $parameters
53
     *
54
     * @return mixed
55
     */
56 49
    protected function containerCall(object $service, string $method, array $parameters): mixed
57
    {
58
        try {
59 49
            return app()->call(
60 49
                [$service, $method],
61 49
                $this->getPassedParameters(
62
                    $service,
63
                    $method,
64
                    $parameters
65
                )
66
            );
67 9
        } catch (\ReflectionException) {
68 3
            return $this->forwardCallTo($service, $method, $parameters);
69
        }
70
    }
71
72
    /**
73
     * @return void
74
     */
75 11
    protected function findForwardedInstance(): void
76
    {
77 11
        $clue = $this->instance::class . Forwarding::CONTAINER_KEY;
78
79 11
        $instance = rescue(fn () => app($clue), report: false);
80
81 11
        transform($instance, fn () => $this->instance = $instance);
82
    }
83
84
    /**
85
     * Pass the call through container.
86
     *
87
     * @param  string  $method
88
     * @param  array  $parameters
89
     *
90
     * @return mixed
91
     */
92 49
    public function __call(string $method, array $parameters): mixed
93
    {
94 49
        if (! method_exists($this->instance, $method)) {
95 8
            $this->findForwardedInstance();
96
        }
97
98 49
        return $this->containerCall($this->instance, $method, $parameters);
99
    }
100
101
    /**
102
     * Get the instance's property.
103
     *
104
     * @param  string  $name
105
     *
106
     * @return mixed
107
     */
108 5
    public function __get(string $name): mixed
109
    {
110 5
        if (! property_exists($this->instance, $name)) {
111 1
            $this->findForwardedInstance();
112
        }
113
114 5
        return $this->instance->{$name};
115
    }
116
117
    /**
118
     * Set the instance's property.
119
     *
120
     * @param  string  $name
121
     * @param  mixed  $value
122
     */
123 4
    public function __set(string $name, mixed $value): void
124
    {
125 4
        if (! property_exists($this->instance, $name)) {
126 2
            $this->findForwardedInstance();
127
        }
128
129 4
        $this->instance->{$name} = $value;
130
    }
131
}
132