Passed
Push — master ( 82761a...afd45d )
by Jan
03:53
created

TreeBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A typeToTree() 0 14 2
A elementToTreeNode() 0 17 3
A __construct() 0 4 1
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Services;
33
34
use App\Entity\StructuralDBElement;
35
use App\Helpers\TreeViewNode;
36
use App\Repository\StructuralDBElementRepository;
37
use Doctrine\ORM\EntityManagerInterface;
38
39
/**
40
 *  This service gives you multiple possibilities to generate trees.
41
 *
42
 * @package App\Controller
43
 */
44
class TreeBuilder
45
{
46
    protected $url_generator;
47
    protected $em;
48
49
    public function __construct(EntityURLGenerator $URLGenerator, EntityManagerInterface $em)
50
    {
51
        $this->url_generator = $URLGenerator;
52
        $this->em = $em;
53
    }
54
55
    /**
56
     * Generates a tree for the given Element. The given element is the top node, all children are child nodes.
57
     * @param StructuralDBElement $element The element for which the tree should be generated.
58
     * @param string $href_type The type of the links that should be used for the links. Set to null, to disable links.
59
     *                          See EntityURLGenerator::getURL for possible types.
60
     * @return TreeViewNode The Node for the given Element.
61
     */
62
    public function elementToTreeNode(StructuralDBElement $element, ?string $href_type = 'list_parts') : TreeViewNode
63
    {
64
        $children = $element->getSubelements();
65
66
        $children_nodes = null;
67
        foreach ($children as $child) {
68
            $children_nodes[] = $this->elementToTreeNode($child, $href_type);
69
        }
70
71
        //Check if we need to generate a href type
72
        $href = null;
73
74
        if (!empty($href_type)) {
75
            $href = $this->url_generator->getURL($element, $href_type);
76
        }
77
78
        return new TreeViewNode($element->getName(), $href, $children_nodes);
79
    }
80
81
    /**
82
     * Generates a tree for all elements of the given type
83
     * @param StructuralDBElement $class_name The class name of the StructuralDBElement class for which the tree should
84
     *                                          be generated.
85
     * @param string $href_type The type of the links that should be used for the links. Set to null, to disable links.
86
     *                          See EntityURLGenerator::getURL for possible types.
87
     * @return TreeViewNode[] Returns an array, containing all nodes. It is empty if the given class has no elements.
88
     */
89
    public function typeToTree(string $class_name, ?string $href_type = 'list_parts') : array
90
    {
91
        /**
92
         * @var $repo StructuralDBElementRepository
93
         */
94
        $repo = $this->em->getRepository($class_name);
95
        $root_nodes = $repo->findRootNodes();
96
97
        $array = array();
98
        foreach ($root_nodes as $node) {
99
            $array[] = $this->elementToTreeNode($node, $href_type);
100
        }
101
102
        return $array;
103
    }
104
}
105