FSKDTree::readItemsCount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Hexogen\KDTree;
4
5
use Hexogen\KDTree\Interfaces\ItemFactoryInterface;
6
use Hexogen\KDTree\Interfaces\KDTreeInterface;
7
use Hexogen\KDTree\Interfaces\NodeInterface;
8
9
class FSKDTree implements KDTreeInterface
10
{
11
    /**
12
     * @const int integer size in bytes
13
     */
14
    const INT_LENGTH = 4;
15
16
    /**
17
     * @const int float size in bytes
18
     */
19
    const FLOAT_LENGTH = 8;
20
21
    /**
22
     * @var NodeInterface
23
     */
24
    private $root;
25
26
    /**
27
     * @var array
28
     */
29
    private $maxBoundary;
30
31
    /**
32
     * @var array
33
     */
34
    private $minBoundary;
35
36
    /**
37
     * @var int number of items in the tree
38
     */
39
    private $length;
40
41
    /**
42
     * @var int
43
     */
44
    private $dimensions;
45
46
    /**
47
     * @var
48
     */
49
    private $handler;
50
51
    /**
52
     * @var ItemFactoryInterface
53
     */
54
    private $factory;
55
56
    /**
57
     * FSKDTree constructor.
58
     * @param $path
59
     * @param ItemFactoryInterface $factory
60
     */
61 48
    public function __construct($path, ItemFactoryInterface $factory)
62
    {
63 48
        $this->factory = $factory;
64 48
        $this->handler = fopen($path, 'rb');
65 48
        $this->readInitData();
66 48
    }
67
68
    /**
69
     *  FSKDTree destructor
70
     */
71 48
    public function __destruct()
72
    {
73 48
        fclose($this->handler);
74 48
    }
75
76
    /**
77
     * @return int
78
     */
79 24
    public function getItemCount(): int
80
    {
81 24
        return $this->length;
82
    }
83
84
    /**
85
     * @return NodeInterface
86
     */
87 12
    public function getRoot(): ?NodeInterface
88
    {
89 12
        return $this->root;
90
    }
91
92
    /**
93
     * @return array
94
     */
95 12
    public function getMinBoundary(): array
96
    {
97 12
        return $this->minBoundary;
98
    }
99
100
    /**
101
     * @return array
102
     */
103 12
    public function getMaxBoundary(): array
104
    {
105 12
        return $this->maxBoundary;
106
    }
107
108
    /**
109
     * @return int
110
     */
111 12
    public function getDimensionCount(): int
112
    {
113 12
        return $this->dimensions;
114
    }
115
116
    /**
117
     *  Read binary data and convert it to an object
118
     */
119 48
    private function readInitData()
120
    {
121 48
        $this->readDimensionsCount();
122 48
        $this->readItemsCount();
123 48
        $this->readUpperBound();
124 48
        $this->readLowerBound();
125 48
        $this->setRoot();
126 48
    }
127
128
    /**
129
     *  read num of dimensions in array
130
     */
131 48
    private function readDimensionsCount()
132
    {
133 48
        $binData = fread($this->handler, FSKDTree::INT_LENGTH);
134 48
        $this->dimensions = unpack('V', $binData)[1];
135 48
    }
136
137
    /**
138
     *  read number of items in the tree
139
     */
140 48
    private function readItemsCount()
141
    {
142 48
        $binData = fread($this->handler, FSKDTree::INT_LENGTH);
143 48
        $this->length = unpack('V', $binData)[1];
144 48
    }
145
146
    /**
147
     *  read upper boundary point
148
     */
149 48
    private function readUpperBound()
150
    {
151 48
        $this->maxBoundary = $this->readPoint();
152 48
    }
153
154
    /**
155
     *  read lower boundary point
156
     */
157 48
    private function readLowerBound()
158
    {
159 48
        $this->minBoundary = $this->readPoint();
160 48
    }
161
162
    /**
163
     *  set tree root
164
     */
165 48
    private function setRoot()
166
    {
167 48
        if ($this->length == 0) {
168 12
            return;
169
        }
170 36
        $position = ftell($this->handler);
171 36
        $this->root = new FSNode($this->factory, $this->handler, $position, $this->dimensions);
172 36
    }
173
174
    /**
175
     * Read point
176
     * @return array
177
     */
178 48
    private function readPoint(): array
179
    {
180 48
        $dataLength = FSKDTree::FLOAT_LENGTH * $this->dimensions;
181 48
        $binData = fread($this->handler, $dataLength);
182 48
        $dValues = unpack('d' . $this->dimensions, $binData);
183 48
        return array_values($dValues);
184
    }
185
}
186