Passed
Push — master ( 4e36c0...26f6ea )
by David
01:34
created

BreadthFirstBreadcrumb::createBreadcrumb()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 5
nop 2
dl 0
loc 33
rs 9.0444
c 0
b 0
f 0
1
<?php
2
3
namespace GGGGino\WarehousePath\Breadcrumb;
4
5
use GGGGino\WarehousePath\Entity\Place;
6
7
class BreadthFirstBreadcrumb implements BreadcrumbInterface
8
{
9
    /**
10
     * Main algorithm to find the shortest path.
11
     *
12
     * @todo: nel futuro quando ci sarà un magazzino grande, spezzare il magazzino prendendo solo il quadrato contenente i vari punti
13
     * @todo: permettere la modifica al volo del peso delle locazioni
14
     *
15
     * @param Place $startPlace
16
     * @param Place $endPlace if not null do early exit
17
     */
18
    public function createBreadcrumb(Place $startPlace, Place $endPlace = null)
19
    {
20
        $frontier = array();
21
        array_push($frontier, $startPlace);
22
23
        while (!empty($frontier)) {
24
            /** @var Place $current */
25
            $current = array_shift($frontier);
26
27
            /** @var Place $vicino */
28
            foreach ($current->getWalkableNeighbors() as $vicino) {
29
                $tempCost = $current->getCurrentWeight() + $vicino->getOriginalWeight();
30
31
                if ($vicino->isVisited() && $tempCost < $vicino->getCurrentWeight()) {
32
                    $vicino->setCurrentWeight($tempCost);
33
                    $vicino->setWalkingCameFrom($current);
34
                    array_push($frontier, $vicino);
35
                    continue;
36
                }
37
38
                if ($vicino->isVisited()) {
39
                    continue;
40
                }
41
42
                $vicino->setVisited(true);
43
44
                $vicino->increaseCurrentWeight($current->getCurrentWeight());
45
                $vicino->setWalkingCameFrom($current);
46
47
                array_push($frontier, $vicino);
48
            }
49
50
            $current->setVisited(true);
51
        }
52
    }
53
}