Passed
Pull Request — main (#4)
by Michael
03:23
created

CallProxy   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 35
Bugs 5 Features 0
Metric Value
eloc 27
c 35
b 5
f 0
dl 0
loc 121
ccs 27
cts 27
cp 1
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A containerCall() 0 13 2
A __construct() 0 6 1
A getInternal() 0 3 1
A findForwardedInstance() 0 8 2
A __call() 0 7 2
A __set() 0 7 2
A __get() 0 7 2
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
        if (! is_null($instance)) {
82 8
            $this->instance = $instance;
83
        }
84
    }
85
86
    /**
87
     * Pass the call through container.
88
     *
89
     * @param  string  $method
90
     * @param  array  $parameters
91
     *
92
     * @return mixed
93
     */
94 49
    public function __call(string $method, array $parameters): mixed
95
    {
96 49
        if (! method_exists($this->instance, $method)) {
97 8
            $this->findForwardedInstance();
98
        }
99
100 49
        return $this->containerCall($this->instance, $method, $parameters);
101
    }
102
103
    /**
104
     * Get the instance's property.
105
     *
106
     * @param  string  $name
107
     *
108
     * @return mixed
109
     */
110 6
    public function __get(string $name): mixed
111
    {
112 6
        if (! property_exists($this->instance, $name)) {
113 1
            $this->findForwardedInstance();
114
        }
115
116 6
        return $this->instance->{$name};
117
    }
118
119
    /**
120
     * Set the instance's property.
121
     *
122
     * @param  string  $name
123
     * @param  mixed  $value
124
     */
125 4
    public function __set(string $name, mixed $value): void
126
    {
127 4
        if (! property_exists($this->instance, $name)) {
128 2
            $this->findForwardedInstance();
129
        }
130
131 4
        $this->instance->{$name} = $value;
132
    }
133
}
134