PuzzleSolution::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PuzzleSolver;
4
5
use function assert;
6
use function implode;
7
use const PHP_EOL;
8
9
/**
10
 * Puzzle Solution
11
 *
12
 * The goal of this whole endeavour. Contains the solved state and all the
13
 * moves that led to that state, as well as a reference to the original state
14
 * of the puzzle that got solved.
15
 *
16
 * @author Stratadox
17
 */
18
final class PuzzleSolution implements Solution
19
{
20
    /** @var Moves */
21
    private $moves;
22
    /** @var Puzzle */
23
    private $state;
24
    /** @var Puzzle */
25
    private $original;
26
27
    public function __construct(Moves $moves, Puzzle $state, Puzzle $original)
28
    {
29
        $this->moves = $moves;
30
        $this->state = $state;
31
        $this->original = $original;
32
    }
33
34
    public static function fromSolved(Puzzle $puzzle, Puzzle $original): self
35
    {
36
        assert($puzzle->isSolved());
37
        return new self($puzzle->movesSoFar(), $puzzle, $original);
38
    }
39
40
    public function moves(): Moves
41
    {
42
        return $this->moves;
43
    }
44
45
    public function state(): Puzzle
46
    {
47
        return $this->state;
48
    }
49
50
    public function cost(): float
51
    {
52
        return $this->moves->cost();
53
    }
54
55
    public function original(): Puzzle
56
    {
57
        return $this->original;
58
    }
59
60
    public function __toString(): string
61
    {
62
        return implode(PHP_EOL, $this->moves->items());
63
    }
64
}
65