MemorySourceStrategy::getPreviousRank()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpScience\PageRank\Strategy;
6
7
use PhpScience\PageRank\Builder\NodeBuilder;
8
use PhpScience\PageRank\Builder\NodeCollectionBuilder;
9
use PhpScience\PageRank\Data\NodeCollectionInterface;
10
11 1
class MemorySourceStrategy implements NodeDataSourceStrategyInterface
12
{
13
    private NodeBuilder           $nodeBuilder;
14
    private NodeCollectionBuilder $nodeCollectionBuilder;
15
16
    private array $previousRanks = [];
17
    private array $nodeListMap;
18
    private ?NodeCollectionInterface $nodeCollection = null;
19
20 2
    public function __construct(
21
        NodeBuilder $nodeBuilder,
22
        NodeCollectionBuilder $nodeCollectionBuilder,
23
        array $nodeListMap
24
    ) {
25 2
        $this->nodeBuilder = $nodeBuilder;
26 2
        $this->nodeCollectionBuilder = $nodeCollectionBuilder;
27 2
        $this->nodeListMap = $nodeListMap;
28 2
    }
29
30 2
    public function getIncomingNodeIds(int $nodeId): array
31
    {
32 2
        return $this->nodeListMap[$nodeId]['in'];
33
    }
34
35 2
    public function countOutgoingNodes(int $nodeId): int
36
    {
37 2
        return count($this->nodeListMap[$nodeId]['out']);
38
    }
39
40 2
    public function getPreviousRank(int $nodeId): float
41
    {
42 2
        return $this->previousRanks[$nodeId];
43
    }
44
45 2
    public function updateNodes(NodeCollectionInterface $collection): void
46
    {
47 2
        foreach ($collection->getNodes() as $item) {
48 2
            $this->previousRanks[$item->getId()] = $item->getRank();
49
        }
50 2
    }
51
52 2
    public function getNodeCollection(): NodeCollectionInterface
53
    {
54 2
        if (null === $this->nodeCollection) {
55 2
            $nodes = [];
56
57 2
            foreach ($this->nodeListMap as $nodeMap) {
58 2
                $nodes[] = $this->nodeBuilder->build($nodeMap);
59
            }
60
61 2
            $this->nodeCollection = $this->nodeCollectionBuilder->build($nodes);
62
        }
63
64 2
        return $this->nodeCollection;
65
    }
66
67 1
    public function getHighestRank(): float
68
    {
69 1
        $highest = null;
70
71 1
        foreach ($this->getNodeCollection()->getNodes() as $node) {
72
            if (
73 1
                null === $highest
74 1
                || $node->getRank() > $highest
75
            ) {
76 1
                $highest = $node->getRank();
77
            }
78
        }
79
80 1
        return $highest;
81
    }
82
83 1
    public function getLowestRank(): float
84
    {
85 1
        $lowest = null;
86
87 1
        foreach ($this->getNodeCollection()->getNodes() as $node) {
88
            if (
89 1
                null === $lowest
90 1
                || $node->getRank() < $lowest
91
            ) {
92 1
                $lowest = $node->getRank();
93
            }
94
        }
95
96 1
        return $lowest;
97
    }
98
}
99