BasePresenter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 53
ccs 6
cts 12
cp 0.5
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A collection() 0 6 1
render() 0 1 ?
A renderRecursiveToString() 0 13 3
template() 0 1 ?
1
<?php
2
3
namespace Vine\Presenters;
4
5
use Vine\Node;
6
use Vine\NodeCollection;
7
use Vine\Tree;
8
9
abstract class BasePresenter
10
{
11
    protected $collection;
12
13 9
    public function __construct()
14
    {
15 9
        $this->collection = new NodeCollection();
16 9
    }
17
18 9
    public function collection(NodeCollection $collection)
19
    {
20 9
        $this->collection = $collection;
21
22 9
        return $this;
23
    }
24
25
    /**
26
     * Render collection
27
     *
28
     * @return string
29
     */
30
    abstract public function render();
31
32
    /**
33
     * Render each branch and its children recursively
34
     *
35
     * @param NodeCollection $nodeCollection
36
     * @param int $level
37
     * @return string
38
     */
39
    protected function renderRecursiveToString(NodeCollection $nodeCollection, $level = 0): string
40
    {
41
        $output = '';
42
43
        foreach($nodeCollection as $node)
44
        {
45
            $output .= $this->template($node,$level);
46
47
            if(!$node->isLeaf()) $output .= $this->renderRecursiveToString($node->children(),$level+1);
48
        }
49
50
        return $output;
51
    }
52
53
    /**
54
     * Template for a single node entry.
55
     *
56
     * @param Node $node
57
     * @param int $level
58
     * @return string
59
     */
60
    abstract protected function template(Node $node,$level = 0);
61
}
62