MultipleEdges::gather()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Pathfinder\Graph\Builder;
4
5
use function array_merge;
6
use function array_reduce;
7
use Stratadox\Pathfinder\Graph\Edges;
8
use Stratadox\Pathfinder\Graph\Roads;
9
10
final class MultipleEdges implements EdgeDefinition
11
{
12
    private $edges;
13
14
    private function __construct(EdgeDefinition ...$edges)
15
    {
16
        $this->edges = $edges;
17
    }
18
19
    public static function consistingOf(EdgeDefinition ...$edges): EdgeDefinition
20
    {
21
        return new self(...$edges);
22
    }
23
24
    public function andTo(string $target, float $cost = 1.0): EdgeDefinition
25
    {
26
        return new self(
27
            ...array_merge($this->edges, [WithEdge::to($target, $cost)])
28
        );
29
    }
30
31
    public function gather(): Edges
32
    {
33
        return array_reduce(
34
            $this->edges,
35
            function (Edges $carry, EdgeDefinition $edge): Edges {
36
                return $carry->merge($edge->gather());
37
            },
38
            Roads::none()
39
        );
40
    }
41
}
42