Passed
Push — main ( 94aee3...feda97 )
by Michael
03:31
created

MethodForwarder::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace MichaelRubel\EnhancedContainer\Core;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use MichaelRubel\EnhancedContainer\Traits\HelpsProxies;
8
9
class MethodForwarder
10
{
11
    use HelpsProxies;
0 ignored issues
show
Bug introduced by
The trait MichaelRubel\EnhancedContainer\Traits\HelpsProxies requires the property $contextual which is not provided by MichaelRubel\EnhancedCon...er\Core\MethodForwarder.
Loading history...
12
13
    /**
14
     * @const CLASS_SEPARATOR
15
     */
16
    public const CLASS_SEPARATOR = '\\';
17
18
    /**
19
     * @param object|string $class
20
     * @param array         $dependencies
21
     */
22 58
    public function __construct(
23
        public object | string $class,
24
        public array $dependencies
25
    ) {
26 58
    }
27
28
    /**
29
     * Forward the method.
30
     *
31
     * @return object|null
32
     */
33 58
    public function getClass(): ?object
34
    {
35 58
        $forwardsTo = $this->forwardsTo();
36
37 58
        return class_exists($forwardsTo) || interface_exists($forwardsTo)
38 30
            ? $this->resolvePassedClass($forwardsTo, $this->dependencies)
39 58
            : null;
40
    }
41
42
    /**
43
     * Parse the class where to forward the call.
44
     *
45
     * @return string
46
     */
47 58
    public function forwardsTo(): string
48
    {
49
        /** @var string */
50 58
        $naming_from = config('enhanced-container.from.naming') ?? 'pluralStudly';
51
52
        /** @var string */
53 58
        $naming_to = config('enhanced-container.to.naming') ?? 'pluralStudly';
54
55
        /** @var string */
56 58
        $layer_from = config('enhanced-container.from.layer') ?? 'Service';
57
58
        /** @var string */
59 58
        $layer_to = config('enhanced-container.to.layer') ?? 'Repository';
60
61 58
        return collect(
62 58
            $this->convertToNamespace($this->class)
63 58
        )->pipe(
64 58
            fn (Collection $class): Collection => collect(
65 58
                explode(self::CLASS_SEPARATOR, $class->first())
66
            )
67 58
        )->pipe(
68 58
            fn (Collection $delimited): Collection => $delimited->map(
69 58
                fn ($item) => str_replace(
70 58
                    Str::{$naming_from}($layer_from),
71 58
                    Str::{$naming_to}($layer_to),
72
                    $item
73
                )
74 58
            )
75 58
        )->pipe(
76 58
            fn (Collection $structure): string => implode(
77 58
                self::CLASS_SEPARATOR,
78 58
                $structure->put(
79 58
                    $structure->keys()->last(),
80 58
                    str_replace(
81 58
                        $layer_from,
82
                        $layer_to,
83 58
                        $structure->last() ?? ''
84
                    )
85 58
                )->all()
86
            )
87 58
        );
88
    }
89
90
    /**
91
     * Get the instance's property.
92
     *
93
     * @param string $name
94
     *
95
     * @return mixed
96
     */
97
    public function __get(string $name): mixed
98
    {
99
        return $this->{$name};
100
    }
101
102
    /**
103
     * Set the instance's property.
104
     *
105
     * @param string $name
106
     * @param mixed  $value
107
     *
108
     * @return void
109
     */
110
    public function __set(string $name, mixed $value): void
111
    {
112
        $this->{$name} = $value;
113
    }
114
}
115