ProvisionNode   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 110
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A reprovision() 0 13 1
A getDatasource() 0 3 1
A getInstallation() 0 3 1
A merge() 0 24 5
A injectDatasource() 0 3 1
1
<?php
2
3
/**
4
 * AppserverIo\Provisioning\Api\Node\ProvisionNode
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    Tim Wagner <[email protected]>
15
 * @copyright 2018 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/provisioning
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Provisioning\Api\Node;
22
23
use AppserverIo\Description\Annotations as DI;
24
use AppserverIo\Description\Api\Node\AbstractNode;
25
use AppserverIo\Provisioning\Configuration\ProvisionConfigurationInterface;
26
use AppserverIo\Psr\ApplicationServer\Configuration\DatasourceConfigurationInterface;
27
28
/**
29
 * DTO to transfer a applications provision configuration.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2018 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/appserver-io/provisioning
35
 * @link      http://www.appserver.io
36
 */
37
class ProvisionNode extends AbstractNode implements ProvisionConfigurationInterface
38
{
39
40
    /**
41
     * The node containing datasource information.
42
     *
43
     * @var \AppserverIo\Psr\ApplicationServer\Configuration\DatasourceConfigurationInterface
44
     * @DI\Mapping(nodeName="datasource", nodeType="AppserverIo\Description\Api\Node\DatasourceNode")
45
     */
46
    protected $datasource;
47
48
    /**
49
     * The node containing installation information.
50
     *
51
     * @var \AppserverIo\Provisioning\Api\Node\InstallationNode
52
     * @DI\Mapping(nodeName="installation", nodeType="AppserverIo\Provisioning\Api\Node\InstallationNode")
53
     */
54
    protected $installation;
55
56
    /**
57
     * Injects the datasource node.
58
     *
59
     * @param \AppserverIo\Psr\ApplicationServer\Configuration\DatasourceConfigurationInterface $datasource The datasource node to inject
60
     *
61
     * @return void
62
     */
63
    public function injectDatasource(DatasourceConfigurationInterface $datasource)
64
    {
65
        $this->datasource = $datasource;
66
    }
67
68
    /**
69
     * Returns the node containing datasource information.
70
     *
71
     * @return \AppserverIo\Psr\ApplicationServer\Configuration\DatasourceConfigurationInterface The node containing datasource information
72
     */
73
    public function getDatasource()
74
    {
75
        return $this->datasource;
76
    }
77
78
    /**
79
     * Returns the node containing installation information.
80
     *
81
     * @return \AppserverIo\Provisioning\Api\Node\InstallationNode The node containing installation information
82
     */
83
    public function getInstallation()
84
    {
85
        return $this->installation;
86
    }
87
88
    /**
89
     * This method reprovisions the provision node with the data from the file passed as parameter.
90
     *
91
     * Before reinitializing the provisioning node, the file will be reinterpreted with be invoking
92
     * the PHP parser again, what again gives you the possibility to replace content by calling the
93
     * PHP methods of this class.
94
     *
95
     * @param string $provisionFile The absolute pathname of the file to reprovision from
96
     *
97
     * @return void
98
     */
99
    public function reprovision($provisionFile)
100
    {
101
102
        // copy the datasource node temporarily
103
        $tmpDatasource = $this->datasource;
104
105
        // replace the variables
106
        ob_start();
107
        require $provisionFile;
108
        $this->initFromString(ob_get_clean());
0 ignored issues
show
Bug introduced by
ob_get_clean() of type string is incompatible with the type AppserverIo\Lang\String expected by parameter $string of AppserverIo\Description\...tNode::initFromString(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
        $this->initFromString(/** @scrutinizer ignore-type */ ob_get_clean());
Loading history...
109
110
        // re-attach the database node
111
        $this->datasource = $tmpDatasource;
112
    }
113
114
    /**
115
     * This method merges the installation steps of the passed provisioning node into the steps of
116
     * this instance. If a installation node with the same type already exists, the one of this
117
     * instance will be overwritten.
118
     *
119
     * @param \AppserverIo\Provisioning\Configuration\ProvisionConfigurationInterface $provisionNode The node with the installation steps we want to merge
120
     *
121
     * @return void
122
     */
123
    public function merge(ProvisionConfigurationInterface $provisionNode)
124
    {
125
126
        // inject the datasource node if available
127
        if ($datasource = $provisionNode->getDatasource()) {
128
            $this->injectDatasource($datasource);
129
        }
130
131
        // load the steps of this instance
132
        $localSteps = $this->getInstallation()->getSteps();
133
134
        // merge it with the ones found in the passed provisioning node
135
        foreach ($provisionNode->getInstallation()->getSteps() as $stepToMerge) {
136
            foreach ($localSteps as $key => $step) {
137
                if ($step->getType() === $stepToMerge->getType()) {
138
                    $localSteps[$key] = $stepToMerge;
139
                } else {
140
                    $localSteps[$stepToMerge->getUuid()] = $stepToMerge;
141
                }
142
            }
143
        }
144
145
        // set the installation steps
146
        $this->getInstallation()->setSteps($localSteps);
147
    }
148
}
149