Passed
Pull Request — main (#4)
by Michael
04:19 queued 55s
created

Forwarding   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 3
Metric Value
eloc 11
c 5
b 0
f 3
dl 0
loc 63
ccs 12
cts 12
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A to() 0 8 1
A from() 0 5 1
A resolve() 0 3 2
A enable() 0 3 1
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