Passed
Push — main ( 84fe19...8f2905 )
by Michael
03:44
created

CallProxy::shouldForward()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
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\HelpsProxies;
10
11
class CallProxy implements Call
12
{
13
    use HelpsProxies, ForwardsCalls;
0 ignored issues
show
introduced by
The trait MichaelRubel\EnhancedContainer\Traits\HelpsProxies requires some properties which are not provided by MichaelRubel\EnhancedContainer\Core\CallProxy: $contextual, $map
Loading history...
14
15
    /**
16
     * @var object
17
     */
18
    private object $instance;
19
20
    /**
21
     * @var object|null
22
     */
23
    private ?object $forwardsTo = null;
24
25
    /**
26
     * CallProxy constructor.
27
     *
28
     * @param object|string $class
29
     * @param array         $dependencies
30
     * @param string|null   $context
31
     */
32 62
    public function __construct(
33
        private object | string $class,
34
        private array $dependencies = [],
35
        private ?string $context = null
36
    ) {
37 62
        $this->instance = ! is_object($class)
38 49
            ? $this->resolvePassedClass(
39 49
                $this->class,
0 ignored issues
show
Bug introduced by
It seems like $this->class can also be of type object; however, parameter $class of MichaelRubel\EnhancedCon...y::resolvePassedClass() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
                /** @scrutinizer ignore-type */ $this->class,
Loading history...
40 49
                $this->dependencies,
41 49
                $this->context
42
            )
43 16
            : $class;
44
45 62
        if (isForwardingEnabled()) {
46 18
            $this->forwardsTo = app(MethodForwarder::class, [
47 18
                'class'        => $this->class,
48 18
                'dependencies' => $this->dependencies,
49 18
            ])->getClass();
50
        }
51 62
    }
52
53
    /**
54
     * Perform the container call.
55
     *
56
     * @param object $service
57
     * @param string $method
58
     * @param array  $parameters
59
     *
60
     * @return mixed
61
     * @throws \ReflectionException
62
     */
63 56
    public function containerCall(object $service, string $method, array $parameters): mixed
64
    {
65
        try {
66 56
            return app()->call(
67 56
                [$service, $method],
68 56
                $this->getPassedParameters(
69 56
                    $service,
70
                    $method,
71
                    $parameters
72
                )
73
            );
74 11
        } catch (\ReflectionException $e) {
75 2
            if (config('enhanced-container.manual_forwarding') ?? false) {
76 1
                return $this->forwardCallTo($service, $method, $parameters);
77
            }
78
79 1
            throw $e;
80
        }
81
    }
82
83
    /**
84
     * Gets the internal property by name.
85
     *
86
     * @param string $property
87
     *
88
     * @return mixed
89
     */
90 4
    public function getInternal(string $property): mixed
91
    {
92 4
        return $this->{$property};
93
    }
94
95
    /**
96
     * Pass the call through container.
97
     *
98
     * @param string $method
99
     * @param array  $parameters
100
     *
101
     * @return mixed
102
     * @throws \ReflectionException
103
     */
104 56
    public function __call(string $method, array $parameters): mixed
105
    {
106 56
        if (! is_null($this->forwardsTo) && ! method_exists($this->instance, $method)) {
107 10
            return $this->containerCall($this->forwardsTo, $method, $parameters);
108
        }
109
110 46
        return $this->containerCall($this->instance, $method, $parameters);
111
    }
112
113
    /**
114
     * Get the instance's property.
115
     *
116
     * @param string $name
117
     *
118
     * @return mixed
119
     */
120 5
    public function __get(string $name): mixed
121
    {
122 5
       if (! is_null($this->forwardsTo)) {
123 1
           return $this->forwardsTo->{$name};
124
       }
125
126 4
       return $this->instance->{$name};
127
    }
128
129
    /**
130
     * Set the instance's property.
131
     *
132
     * @param string $name
133
     * @param mixed  $value
134
     */
135 3
    public function __set(string $name, mixed $value): void
136
    {
137 3
        if (! is_null($this->forwardsTo)) {
138 2
            $this->forwardsTo->{$name} = $value;
139
140 2
            return;
141
        }
142
143 1
        $this->instance->{$name} = $value;
144 1
    }
145
}
146