Tree   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 47
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A count() 0 4 1
A findByIndex() 0 4 1
A findManyByIndex() 0 12 3
1
<?php
2
3
namespace Vine;
4
5
/**
6
 * Class Tree
7
 * Tree collection is exactly the same as a node collection with the difference
8
 * that a tree contains a flat index reference to each node, based on the primary key.
9
 * This allows for fast retrieval of nodes by searching on their primary key.
10
 *
11
 * @package Vine
12
 */
13
class Tree extends NodeCollection
14
{
15
    /**
16
     * Flat listing of nodes with primary id as collection key.
17
     *
18
     * @var NodeCollection
19
     */
20
    private $index;
21
22 18
    public function __construct(NodeCollection $nodeCollection, NodeCollection $index)
23
    {
24 18
        $this->nodes = $nodeCollection->all();
25 18
        $this->index = $index;
26 18
    }
27
28
    /**
29
     * Count of all nodes
30
     *
31
     * @return int
32
     */
33 3
    public function count()
34
    {
35 3
        return $this->index->count();
36
    }
37
38
    /**
39
     * @param $id
40
     * @return null|Node
41
     */
42 3
    public function findByIndex($id)
43
    {
44 3
        return $this->findManyByIndex((array)$id)->first();
45
    }
46
47 6
    public function findManyByIndex(array $ids): NodeCollection
48
    {
49 6
        $nodes = new NodeCollection;
50
51 6
        foreach($ids as $id)
52
        {
53 6
            if(!isset($this->index[$id])) continue;
54 6
            $nodes->add($this->index[$id]);
55
        }
56
57 6
        return $nodes;
58
    }
59
}
60