Completed
Push — master ( d0d021...195fbe )
by Ventaquil
10:35
created

DijkstraCache::loadOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
namespace Algorithms;
3
4
abstract class DijkstraCache
5
{
6
    protected static function cacheMany($currentData, $many)
7
    {
8
        if (empty($currentData)) {
9
            $currentData = $many();
10
        } else {
11
            foreach ($many() as $point => $tracks) {
12
                $currentData = self::cacheOne($currentData, $point, function () use ($tracks) {
13
                    return $tracks;
14
                });
15
            }
16
        }
17
18
        return $currentData;
19
    }
20
21
    protected static function cacheOne($currentData, $point, $one)
22
    {
23
        if (empty($currentData[$point])) {
24
            $currentData[$point] = $one();
25
        }
26
27
        return $currentData;
28
    }
29
30
    public static function loadMany(&$currentData, $callback)
31
    {
32
        return self::cacheMany($currentData, $callback);
33
    }
34
35
    public static function loadOne(&$currentData, $point, $callback)
36
    {
37
        $currentData = self::cacheOne($currentData, $point, $callback);
38
39
        return $currentData[$point];
40
    }
41
}
42