Forwarding::to()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
eloc 4
c 2
b 1
f 1
nc 1
nop 1
dl 0
loc 8
ccs 6
cts 6
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 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