Forwarding   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A enable() 0 3 1
A to() 0 8 1
A from() 0 5 1
A resolve() 0 3 2
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 class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
13
     */
14
    public string $pendingClass;
15
16
    /**
17
     * Initialize the forwarding.
18
     */
19 21
    public static function enable(): self
20
    {
21 21
        return new self();
22
    }
23
24
    /**
25
     * Define the pending class.
26
     */
27 22
    public function from(string $class): static
28
    {
29 22
        $this->pendingClass = $this->resolve($class);
30
31 22
        return $this;
32
    }
33
34
    /**
35
     * Define the forwarding for the pending class set previously.
36
     */
37 21
    public function to(string $destination): static
38
    {
39 21
        app()->bind(
40 21
            abstract: $this->pendingClass . static::CONTAINER_KEY,
41 21
            concrete: $this->resolve($destination)
42 21
        );
43
44 21
        return $this;
45
    }
46
47
    /**
48
     * Extract an implementation from the interface if passed.
49
     */
50 22
    protected function resolve(string $class): string
51
    {
52 22
        return ! interface_exists($class) ? $class : app($class)::class;
53
    }
54
}
55