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

Forwarding::from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\EnhancedContainer\Core;
6
7
class Forwarding
8
{
9
    /**
10
     * @const
11
     */
12
    public const CONTAINER_KEY = '_forwarding';
13
14
    /**
15
     * @var string
16
     */
17
    public string $pendingClass;
18
19
    /**
20
     * Initialize the forwarding.
21
     *
22
     * @return self
23
     */
24 15
    public static function enable(): self
25
    {
26 15
        return new self();
27
    }
28
29
    /**
30
     * Define the pending class.
31
     *
32
     * @param  string  $class
33
     *
34
     * @return $this
35
     */
36 15
    public function from(string $class): static
37
    {
38 15
        $this->pendingClass = $this->resolve($class);
39
40 15
        return $this;
41
    }
42
43
    /**
44
     * Define the forwarding for the pending class set previously.
45
     *
46
     * @param  string  $destination
47
     *
48
     * @return $this
49
     */
50 15
    public function to(string $destination): static
51
    {
52 15
        app()->singleton(
53 15
            $this->pendingClass . static::CONTAINER_KEY,
54 15
            $this->resolve($destination)
55
        );
56
57 15
        return $this;
58
    }
59
60
    /**
61
     * Extract an implementation from the interface if passed.
62
     *
63
     * @param  string  $class
64
     *
65
     * @return string
66
     */
67 15
    protected function resolve(string $class): string
68
    {
69 15
        return ! interface_exists($class) ? $class : app($class)::class;
70
    }
71
}
72