Passed
Push — develop ( 7d2249...4ccadf )
by Baptiste
02:09
created

Element::service()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Visualization\Node;
5
6
use Innmind\Compose\Definition\{
7
    Name as ServiceName,
8
    Service,
9
    Service\Constructor,
10
    Argument
11
};
12
use Innmind\Graphviz\{
13
    Node,
14
    Edge,
15
    Node\Name,
16
    Node\Shape
17
};
18
use Innmind\Url\UrlInterface;
19
use Innmind\Colour\Colour;
20
use Innmind\Immutable\{
21
    MapInterface,
22
    SetInterface,
23
    Str
24
};
25
26
final class Element implements Node
27
{
28
    private $node;
29
30 35
    private function __construct(Node $node)
31
    {
32 35
        $this->node = $node;
33 35
    }
34
35 23
    public static function dependency(
36
        ServiceName $dependency,
37
        ServiceName $name,
38
        Constructor $constructor
39
    ): self {
40 23
        $constructor = Str::of((string) $constructor)->replace('\\', '\\\\');
41
42 23
        $node = self::build($dependency->add($name));
43 23
        $node->displayAs(
44 23
            (string) Str::of((string) $name)->append(
45 23
                '\n('.$constructor.')'
46
            )
47
        );
48
49 23
        return new self($node);
50
    }
51
52 2
    public static function service(Service $service): self
53
    {
54 2
        $constructor = Str::of((string) $service->constructor())->replace('\\', '\\\\');
55
56 2
        $node = self::build($service->name());
57 2
        $node->displayAs(
58 2
            (string) Str::of((string) $service->name())->append(
59 2
                '\n('.$constructor.')'
60
            )
61
        );
62
63 2
        if ($service->exposed()) {
64 1
            $node->shaped(
65 1
                Shape::house()->fillWithColor(Colour::fromString('#0f0'))
0 ignored issues
show
Bug introduced by
It seems like Innmind\Colour\Colour::fromString('#0f0') can also be of type Innmind\Colour\CMYKA and Innmind\Colour\HSLA; however, parameter $color of Innmind\Graphviz\Node\Shape::fillWithColor() does only seem to accept Innmind\Colour\RGBA, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
                Shape::house()->fillWithColor(/** @scrutinizer ignore-type */ Colour::fromString('#0f0'))
Loading history...
66
            );
67
        }
68
69 2
        return new self($node);
70
    }
71
72 14
    public static function argument(Argument $argument): self
73
    {
74 14
        $node = self::build($argument->name());
75 14
        $node->displayAs(
76 14
            (string) Str::of((string) $argument->name())
77 14
                ->append('\n('.$argument->type().')')
78
        );
79
80 14
        return new self($node);
81
    }
82
83 35
    public static function build(ServiceName $name): Node
84
    {
85 35
        return Node\Node::named(
86 35
            (string) Str::of((string) $name)->replace('.', '_')
87
        );
88
    }
89
90 7
    public function name(): Name
91
    {
92 7
        return $this->node->name();
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 5
    public function edges(): SetInterface
99
    {
100 5
        return $this->node->edges();
101
    }
102
103 2
    public function linkedTo(Node $node): Edge
104
    {
105 2
        return $this->node->linkedTo($node);
106
    }
107
108 1
    public function target(UrlInterface $url): Node
109
    {
110 1
        return $this->node->target($url);
111
    }
112 1
    public function displayAs(string $label): Node
113
    {
114 1
        return $this->node->displayAs($label);
115
    }
116
117 1
    public function shaped(Shape $shape): Node
118
    {
119 1
        return $this->node->shaped($shape);
120
    }
121
122 4
    public function hasAttributes(): bool
123
    {
124 4
        return $this->node->hasAttributes();
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 8
    public function attributes(): MapInterface
131
    {
132 8
        return $this->node->attributes();
133
    }
134
}
135