Completed
Push — master ( 386469...2cc88e )
by Basarab
01:46
created

FileTreePersister   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 116
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B convert() 0 25 1
B writeNode() 0 45 5
A writeCoordinate() 0 5 1
1
<?php
2
3
namespace Hexogen\KDTree;
4
5
class FileTreePersister implements TreePersisterInterface
6
{
7
    /**
8
     * @var string path to the file
9
     */
10
    private $path;
11
12
    /**
13
     * @var resource file handler
14
     */
15
    private $handler;
16
17
    /**
18
     * @var int
19
     */
20
    private $dimensions;
21
22
    /**
23
     * @var int
24
     */
25
    private $nodeMemorySize;
26
27
    public function __construct(string $path)
28
    {
29
        $this->path = $path;
30
    }
31
32
    /**
33
     * @param KDTreeInterface $tree
34
     * @param string $identifier that identifies persisted tree(may be a filename, database name etc.)
35
     * @return mixed
36
     */
37
    public function convert(KDTreeInterface $tree, string $identifier)
38
    {
39
        $dataChunk = null;
0 ignored issues
show
Unused Code introduced by
$dataChunk is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
        $this->handler = fopen($this->path . '/' . $identifier, 'wb');
41
42
        $this->dimensions = $tree->getDimensionCount();
43
44
        $this->nodeMemorySize = $this->dimensions * 8 + 3 * 4;
45
46
        $dataChunk = pack('V', $this->dimensions);
47
        fwrite($this->handler, $dataChunk);
48
49
        $itemCount = $tree->getItemCount();
50
        $dataChunk = pack('V', $itemCount);
51
        fwrite($this->handler, $dataChunk);
52
53
        $upperBound = $tree->getMaxBoundary();
54
        $this->writeCoordinate($upperBound);
55
56
        $lowerBound = $tree->getMinBoundary();
57
        $this->writeCoordinate($lowerBound);
58
59
        $this->writeNode($tree->getRoot());
60
        fclose($this->handler);
61
    }
62
63
    /**
64
     * @param NodeInterface $node
65
     */
66
    private function writeNode(NodeInterface $node)
67
    {
68
        $position = ftell($this->handler);
69
        $coordinate = [];
70
        $item = $node->getItem();
71
72
        $itemId = $item->getId();
73
        $dataChunk = pack('V', $itemId);
74
        fwrite($this->handler, $dataChunk);
75
76
        $dataChunk = pack('V', 0); // left position currently unknown so it equal 0/null
77
        fwrite($this->handler, $dataChunk);
78
79
        $rightNode = $node->getRight();
80
81
        $rightPosition = 0;
82
        if ($rightNode) {
83
            $rightPosition = $position + $this->nodeMemorySize;
84
        }
85
        $dataChunk = pack('V', $rightPosition);
86
        fwrite($this->handler, $dataChunk);
87
88
89
        for ($i = 0; $i < $this->dimensions; $i++) {
90
            $coordinate[] = $item->getNthDimension($i);
91
        }
92
        $this->writeCoordinate($coordinate);
93
94
        if ($rightNode) {
95
            $this->writeNode($rightNode);
96
        }
97
98
        $leftNode = $node->getLeft();
99
100
        if ($leftNode == null) {
101
            return;
102
        }
103
        $leftPosition = ftell($this->handler);
104
        fseek($this->handler, $position + 4);
105
        $dataChunk = pack('V', $leftPosition);
106
        fwrite($this->handler, $dataChunk);
107
108
        fseek($this->handler, $leftPosition);
109
        $this->writeNode($leftNode);
110
    }
111
112
    /**
113
     * @param array $coordinate
114
     */
115
    private function writeCoordinate(array $coordinate)
116
    {
117
        $dataChunk = pack('d'.$this->dimensions, ...$coordinate);
118
        fwrite($this->handler, $dataChunk);
119
    }
120
}
121