Crossing   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A allCrossingsFor() 0 7 1
A bringAlong() 0 3 1
A __toString() 0 6 3
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PuzzleSolver\Puzzle\WolfGoatCabbage;
4
5
use Stratadox\PuzzleSolver\Move;
6
use Stratadox\PuzzleSolver\Moves;
7
use function array_map;
8
use function sprintf;
9
10
final class Crossing implements Move
11
{
12
    /** @var bool */
13
    private $towardsGoal;
14
    /** @var Purchase|null */
15
    private $bringAlong;
16
17
    public function __construct(bool $towardsGoal, ?Purchase $bringAlong)
18
    {
19
        $this->towardsGoal = $towardsGoal;
20
        $this->bringAlong = $bringAlong;
21
    }
22
23
    public static function allCrossingsFor(Riverbank $riverbank): Moves
24
    {
25
        return new Moves(
26
            new Crossing($riverbank->isStart(), null),
27
            ...array_map(static function (Purchase $purchase) use ($riverbank) {
28
                return new Crossing($riverbank->isStart(), $purchase);
29
            }, $riverbank->purchases())
30
        );
31
    }
32
33
    public function bringAlong(): ?Purchase
34
    {
35
        return $this->bringAlong;
36
    }
37
38
    public function __toString(): string
39
    {
40
        return sprintf(
41
            '%s with %s',
42
            $this->towardsGoal ? 'forwards' : 'backwards',
43
            $this->bringAlong ?: 'nothing'
44
        );
45
    }
46
}
47