FastCalculator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B calculate() 0 45 8
1
<?php
2
3
namespace GGGGino\WarehousePath\Calculator;
4
5
use GGGGino\WarehousePath\Entity\Place;
6
7
class FastCalculator implements CalculatorInterface
8
{
9
    /**
10
     * @inheritdoc
11
     */
12
    public function calculate($arrayNodes, $matrix)
13
    {
14
        /** @var Place $startingPoint */
15
        $startingPoint = $arrayNodes[0];
16
17
        $arraySorted = array($startingPoint);
18
19
        while (count($matrix) > 1) {
20
            $arrayNodeKey = null;
21
22
            // Walk every item in the PlaceArray and if I find one equal I use that as strating point
23
            foreach ($arrayNodes as $key => $node) {
24
                if ( $node === $startingPoint ) {
25
                    $arrayNodeKey = $key;
26
                    break;
27
                }
28
            }
29
30
            /** @var int $minimumFoundKey */
31
            $minimumFoundKey = $arrayNodeKey;
32
            /** @var int $minimumFound */
33
            $minimumFound = INF;
34
35
            /** @var int $nodeWeight */
36
            foreach ($matrix[$arrayNodeKey] as $key => $nodeWeight) {
37
                if( $key == $arrayNodeKey )
38
                    continue;
39
40
                if( $minimumFound > $nodeWeight ){
41
                    $minimumFound = $nodeWeight;
42
                    $minimumFoundKey = $key;
43
                }
44
            }
45
46
            $startingPoint = $arrayNodes[$minimumFoundKey];
47
            $arraySorted[] = $startingPoint;
48
            unset($matrix[$arrayNodeKey]);
49
            /** @var int $nodeWeight */
50
            foreach ($matrix as $key => $nodeWeight) {
51
                unset($matrix[$key][$arrayNodeKey]);
52
            }
53
            unset($arrayNodes[$arrayNodeKey]);
54
        }
55
56
        return $arraySorted;
57
    }
58
}