Safely::apply()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Pathfinder\Estimate;
4
5
use Stratadox\Pathfinder\Environment;
6
use Stratadox\Pathfinder\Heuristic;
7
8
final class Safely implements Heuristic
9
{
10
    private $heuristic;
11
    private $environment;
12
13
    private function __construct(
14
        Heuristic $heuristic,
15
        Environment $environment
16
    ) {
17
        $this->heuristic = $heuristic;
18
        $this->environment = $environment;
19
    }
20
21
    public static function apply(Heuristic $heuristic): self
22
    {
23
        return new self($heuristic, $heuristic->environment());
24
    }
25
26
    public function estimate(string $start, string $goal): float
27
    {
28
        if ($this->environment->areNeighbours($start, $goal)) {
29
            return $this->environment->movementCostBetween($start, $goal);
30
        }
31
        return $this->heuristic->estimate($start, $goal);
32
    }
33
34
    public function environment(): Environment
35
    {
36
        return $this->environment;
37
    }
38
}
39