StructureDefinitionHierarchy::insert()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
/**
4
 * \AppserverIo\Doppelgaenger\Entities\Definitions\StructureDefinitionHierarchy
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Bernhard Wick <[email protected]>
15
 * @copyright 2015 TechDivision GmbH - <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/doppelgaenger
18
 * @link      http://www.appserver.io/
19
 */
20
21
namespace AppserverIo\Doppelgaenger\Entities\Definitions;
22
23
use AppserverIo\Doppelgaenger\Interfaces\StructureDefinitionInterface;
24
25
/**
26
 * Keeps track of structure definitions which are directly or indirectly related to each other
27
 *
28
 * @author    Bernhard Wick <[email protected]>
29
 * @copyright 2015 TechDivision GmbH - <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/appserver-io/doppelgaenger
32
 * @link      http://www.appserver.io/
33
 */
34
class StructureDefinitionHierarchy
35
{
36
    /**
37
     * @var array $nodes The array holding the different structure definitions
38
     */
39
    protected $nodes = array();
40
41
    /**
42
     * Will insert a structure definition into our hierarchy
43
     *
44
     * @param \AppserverIo\Doppelgaenger\Interfaces\StructureDefinitionInterface $node The structure definition to insert
45
     *
46
     * @return bool
47
     */
48
    public function insert(StructureDefinitionInterface $node)
49
    {
50
        // Already here? Nothing to do then
51
        $qualifiedName = $node->getQualifiedName();
52
        if (!empty($this->nodes[$qualifiedName])) {
53
            return true;
54
        }
55
56
        // Add the node
57
        $this->nodes[$qualifiedName] = $node;
58
59
        // Add empty entries for the dependencies so we can check if all where added
60
        $dependencies = $node->getDependencies();
61
        foreach ($dependencies as $dependency) {
62
            if (!empty($this->nodes[$dependency])) {
63
                continue;
64
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
65
            } else {
66
                $this->nodes[$dependency] = null;
67
            }
68
        }
69
70
        // Still here? Sounds great
71
        return true;
72
    }
73
74
    /**
75
     * Will return an entry specified by it's name
76
     *
77
     * @param string $entryName Name of the entries we search for
78
     *
79
     * @return bool
80
     */
81 View Code Duplication
    public function getEntry($entryName)
82
    {
83
        if (!isset($this->nodes[$entryName]) || !is_null($this->nodes[$entryName])) {
84
            return false;
85
        }
86
87
        return $this->nodes[$entryName];
88
    }
89
90
    /**
91
     * Check if a certain entry exists
92
     *
93
     * @param string $entryName Name of the entries we search for
94
     *
95
     * @return bool
96
     */
97 View Code Duplication
    public function entryExists($entryName)
98
    {
99
        if (!isset($this->nodes[$entryName]) || !is_null($this->nodes[$entryName])) {
100
            return false;
101
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
102
        } else {
103
            return true;
104
        }
105
    }
106
107
    /**
108
     * Will return true if all possible node entries contain data
109
     *
110
     * @return bool
111
     */
112
    public function isComplete()
113
    {
114
        foreach ($this->nodes as $node) {
115
            if (is_null($node)) {
116
                return false;
117
            }
118
        }
119
120
        return true;
121
    }
122
}
123