Passed
Pull Request — main (#4)
by Michael
04:16 queued 44s
created

CallProxy::getInternal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 2
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($service, $method, $parameters)
62
            );
63 9
        } catch (\ReflectionException) {
64 3
            return $this->forwardCallTo($service, $method, $parameters);
65
        }
66
    }
67
68
    /**
69
     * @return void
70
     */
71 11
    protected function findForwardedInstance(): void
72
    {
73 11
        $clue = $this->instance::class . Forwarding::CONTAINER_KEY;
74
75 11
        $instance = rescue(fn () => app($clue), report: false);
76
77 11
        if (! is_null($instance)) {
78 8
            $this->instance = $instance;
79
        }
80
    }
81
82
    /**
83
     * Pass the call through container.
84
     *
85
     * @param  string  $method
86
     * @param  array  $parameters
87
     *
88
     * @return mixed
89
     */
90 49
    public function __call(string $method, array $parameters): mixed
91
    {
92 49
        if (! method_exists($this->instance, $method)) {
93 8
            $this->findForwardedInstance();
94
        }
95
96 49
        return $this->containerCall($this->instance, $method, $parameters);
97
    }
98
99
    /**
100
     * Get the instance's property.
101
     *
102
     * @param  string  $name
103
     *
104
     * @return mixed
105
     */
106 6
    public function __get(string $name): mixed
107
    {
108 6
        if (! property_exists($this->instance, $name)) {
109 1
            $this->findForwardedInstance();
110
        }
111
112 6
        return $this->instance->{$name};
113
    }
114
115
    /**
116
     * Set the instance's property.
117
     *
118
     * @param  string  $name
119
     * @param  mixed  $value
120
     */
121 4
    public function __set(string $name, mixed $value): void
122
    {
123 4
        if (! property_exists($this->instance, $name)) {
124 2
            $this->findForwardedInstance();
125
        }
126
127 4
        $this->instance->{$name} = $value;
128
    }
129
}
130