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

Forwarding::to()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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