Crossing::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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