ContextNode::merge()   F
last analyzed

Complexity

Conditions 18
Paths 10976

Size

Total Lines 107
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 0
Metric Value
cc 18
eloc 45
nc 10976
nop 1
dl 0
loc 107
ccs 0
cts 64
cp 0
crap 342
rs 0.7
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * AppserverIo\Appserver\Core\Api\Node\ContextNode
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
 * @author    Johann Zelger <[email protected]>
16
 * @copyright 2015 TechDivision GmbH <[email protected]>
17
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
18
 * @link      https://github.com/appserver-io/appserver
19
 * @link      http://www.appserver.io
20
 */
21
22
namespace AppserverIo\Appserver\Core\Api\Node;
23
24
use AppserverIo\Description\Annotations as DI;
25
use AppserverIo\Description\Api\Node\ParamNode;
26
use AppserverIo\Description\Api\Node\AbstractNode;
27
use AppserverIo\Description\Api\Node\ParamsNodeTrait;
28
use AppserverIo\Description\Api\Node\AnnotationRegistriesNodeTrait;
29
use AppserverIo\Appserver\Core\Utilities\DirectoryKeys;
30
use AppserverIo\Provisioning\Api\Node\ProvisionersNodeTrait;
31
32
/**
33
 * DTO to transfer server information.
34
 *
35
 * @author    Tim Wagner <[email protected]>
36
 * @author    Johann Zelger <[email protected]>
37
 * @copyright 2015 TechDivision GmbH <[email protected]>
38
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
39
 * @link      https://github.com/appserver-io/appserver
40
 * @link      http://www.appserver.io
41
 */
42
class ContextNode extends AbstractNode
43
{
44
45
    /**
46
     * A class loader trait.
47
     *
48
     * @var \AppserverIo\Appserver\Core\Api\Node\ClassLoadersNodeTrait
49
     */
50
    use ClassLoadersNodeTrait;
51
52
    /**
53
     * The logger trait.
54
     *
55
     * @var \AppserverIo\Appserver\Core\Api\Node\LoggersNodeTrait
56
     */
57
    use LoggersNodeTrait;
58
59
    /**
60
     * A managers node trait.
61
     *
62
     * @var \AppserverIo\Appserver\Core\Api\Node\ManagersNodeTrait
63
     */
64
    use ManagersNodeTrait;
65
66
    /**
67
     * A managers node trait.
68
     *
69
     * @var \AppserverIo\Provisioning\Api\Node\ProvisionersNodeTrait
70
     */
71
    use ProvisionersNodeTrait;
72
73
    /**
74
     * A params node trait.
75
     *
76
     * @var \AppserverIo\Description\Api\Node\ParamsNodeTrait
77
     */
78
    use ParamsNodeTrait;
79
80
    /**
81
     * A annotation registries node trait.
82
     *
83
     * @var \AppserverIo\Appserver\Core\Api\Node\AnnotationRegistriesNodeTrait
84
     */
85
    use AnnotationRegistriesNodeTrait;
86
87
    /**
88
     * The context name.
89
     *
90
     * @var string
91
     * @DI\Mapping(nodeType="string")
92
     */
93
    protected $name;
94
95
    /**
96
     * The application environment
97
     *
98
     * @var string
99
     */
100
    protected $environmentName;
101
102
    /**
103
     * The context type.
104
     *
105
     * @var string
106
     * @DI\Mapping(nodeType="string")
107
     */
108
    protected $type;
109
110
    /**
111
     * The context factory class name.
112
     *
113
     * @var string
114
     * @DI\Mapping(nodeType="string")
115
     */
116
    protected $factory;
117
118
    /**
119
     * The path to the web application.
120
     *
121
     * @var string
122
     * @DI\Mapping(nodeType="string")
123
     */
124
    protected $webappPath;
125
126
    /**
127
     * Initializes the context configuration with the passed values.
128
     *
129
     * @param string $name       The context name
130
     * @param string $type       The context class name
131
     * @param string $factory    The context factory class name
132
     * @param array  $params     The context params
133
     * @param string $webappPath The path to the web application
134
     */
135
    public function __construct($name = '', $type = '', $factory = '', array $params = array(), $webappPath = '')
136
    {
137
138
        // set name, type and factory
139
        $this->name = $name;
140
        $this->type = $type;
141
        $this->factory = $factory;
142
        $this->params = $params;
143
        $this->webappPath = $webappPath;
144
145
        // initialize the default directories
146
        $this->initDefaultDirectories();
147
    }
148
149
    /**
150
     * Initialize the default directories.
151
     *
152
     * @return void
153
     */
154
    public function initDefaultDirectories()
155
    {
156
        $this->setParam(DirectoryKeys::DATA, ParamNode::TYPE_STRING, '/data');
157
        $this->setParam(DirectoryKeys::CACHE, ParamNode::TYPE_STRING, '/cache');
158
        $this->setParam(DirectoryKeys::SESSION, ParamNode::TYPE_STRING, '/session');
159
    }
160
161
    /**
162
     * Sets the context name.
163
     *
164
     * @param string $name The context name
165
     *
166
     * @return void
167
     */
168
    public function setName($name)
169
    {
170
        $this->name = $name;
171
    }
172
173
    /**
174
     * Returns the context name.
175
     *
176
     * @return mixed
177
     */
178
    public function getName()
179
    {
180
        return $this->name;
181
    }
182
183
    /**
184
     * Returns the environment name of the application
185
     *
186
     * @return string
187
     */
188
    public function getEnvironmentName()
189
    {
190
        return $this->environmentName;
191
    }
192
193
    /**
194
     * Setter for the environment name
195
     *
196
     * @param string $environmentName The environment name to set
197
     *
198
     * @return void
199
     */
200
    public function setEnvironmentName($environmentName)
201
    {
202
        $this->environmentName = $environmentName;
203
    }
204
205
    /**
206
     * Sets the context type.
207
     *
208
     * @param string $type The context type
209
     *
210
     * @return void
211
     */
212
    public function setType($type)
213
    {
214
        $this->type = $type;
215
    }
216
217
    /**
218
     * Returns the context type.
219
     *
220
     * @return string|null The context type
221
     */
222
    public function getType()
223
    {
224
        return $this->type;
225
    }
226
227
    /**
228
     * Sets the context factory class name.
229
     *
230
     * @param string $factory The context factory class name
231
     *
232
     * @return void
233
     */
234
    public function setFactory($factory)
235
    {
236
        $this->factory = $factory;
237
    }
238
239
    /**
240
     * Returns the context factory class name.
241
     *
242
     * @return \AppserverIo\Appserver\Application\ApplicationFactory
243
     */
244
    public function getFactory()
245
    {
246
        return $this->factory;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->factory returns the type string which is incompatible with the documented return type AppserverIo\Appserver\Ap...tion\ApplicationFactory.
Loading history...
247
    }
248
249
    /**
250
     * Set's the path to the web application.
251
     *
252
     * @param string $webappPath The path to the web application
253
     *
254
     * @return void
255
     */
256
    public function setWebappPath($webappPath)
257
    {
258
        $this->webappPath = $webappPath;
259
    }
260
261
    /**
262
     * Returns the path to the web application.
263
     *
264
     * @return string
265
     */
266
    public function getWebappPath()
267
    {
268
        return $this->webappPath;
269
    }
270
271
    /**
272
     * This method merges the installation steps of the passed provisioning node into the steps of
273
     * this instance. If a installation node with the same type already exists, the one of this
274
     * instance will be overwritten.
275
     *
276
     * @param \AppserverIo\Appserver\Core\Api\Node\ContextNode $contextNode The node with the installation steps we want to merge
277
     *
278
     * @return void
279
     */
280
    public function merge(ContextNode $contextNode)
281
    {
282
283
        // merge the application type
284
        if ($type = $contextNode->getType()) {
285
            $this->setType($type);
286
        }
287
288
        // merge the application factory class name
289
        if ($factory = $contextNode->getFactory()) {
290
            $this->setFactory($factory);
0 ignored issues
show
Bug introduced by
$factory of type AppserverIo\Appserver\Ap...tion\ApplicationFactory is incompatible with the type string expected by parameter $factory of AppserverIo\Appserver\Co...ntextNode::setFactory(). ( Ignorable by Annotation )

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

290
            $this->setFactory(/** @scrutinizer ignore-type */ $factory);
Loading history...
291
        }
292
293
        // merge the application webapp path
294
        if ($webappPath = $contextNode->getWebappPath()) {
295
            $this->setWebappPath($webappPath);
296
        }
297
298
        // load the params defined in this context
299
        $localParams = $this->getParams();
300
301
        // merge them with the passed ones
302
        foreach ($contextNode->getParams() as $paramToMerge) {
303
            $isMerged = false;
304
            /** @var \AppserverIo\Appserver\Core\Api\Node\ParamNode $param */
305
            foreach ($localParams as $key => $param) {
306
                if ($param->getName() == $paramToMerge->getName()) {
307
                    $localParams[$key] = $paramToMerge;
308
                    $isMerged = true;
309
                }
310
            }
311
            if ($isMerged === false) {
312
                $localParams[$paramToMerge->getUuid()] = $paramToMerge;
313
            }
314
        }
315
316
        // set the params back to the context
317
        $this->setParams($localParams);
318
319
        // load the annotation registries defined of this context
320
        $localAnnotationRegistries = $this->getAnnotationRegistries();
321
322
        // append the annotation registries
323
        /**  @var \AppserverIo\Description\Api\Node\AnnotationRegistryNode $additionalAnnotationRegistry */
324
        foreach ($contextNode->getAnnotationRegistries() as $additionalAnnotationRegistry) {
325
            $localAnnotationRegistries[] = $additionalAnnotationRegistry;
326
        }
327
328
        // set the annotation registries back to the context
329
        $this->setAnnotationRegistries($localAnnotationRegistries);
330
331
        // load the managers defined of this context
332
        $localManagers = $this->getManagers();
333
334
        // merge them with the passed ones
335
        /**  @var \AppserverIo\Appserver\Core\Api\Node\ManagerNode $managerToMerge */
336
        foreach ($contextNode->getManagers() as $managerToMerge) {
337
            $isMerged = false;
338
            /** @var \AppserverIo\Appserver\Core\Api\Node\ManagerNode $manager */
339
            foreach ($localManagers as $key => $manager) {
340
                if ($manager->getName() === $managerToMerge->getName()) {
341
                    $manager->merge($managerToMerge);
342
                    $localManagers[$key] = $manager;
343
                    $isMerged = true;
344
                }
345
            }
346
            if ($isMerged === false) {
347
                $localManagers[$managerToMerge->getUuid()] = $managerToMerge;
348
            }
349
        }
350
351
        // set the managers back to the context
352
        $this->setManagers($localManagers);
353
354
        // load the class loaders of this context
355
        $localClassLoaders = $this->getClassLoaders();
356
357
        // merge them with the passed ones
358
        /** @var \AppserverIo\Appserver\Core\Api\Node\ClassLoaderNode $classLoaderToMerge */
359
        foreach ($contextNode->getClassLoaders() as $classLoaderToMerge) {
360
            $isMerged = false;
361
            /** @var \AppserverIo\Appserver\Core\Api\Node\ClassLoaderNode $classLoader */
362
            foreach ($localClassLoaders as $key => $classLoader) {
363
                if ($classLoader->getName() === $classLoaderToMerge->getName()) {
364
                    $localClassLoaders[$key] = $classLoaderToMerge;
365
                    $isMerged = true;
366
                }
367
            }
368
            if ($isMerged === false) {
369
                $localClassLoaders[$classLoaderToMerge->getUuid()] = $classLoaderToMerge;
370
            }
371
        }
372
373
        // set the class loaders back to the context
374
        $this->setClassLoaders($localClassLoaders);
375
376
        // load the loggers of this context
377
        $localLoggers = $this->getLoggers();
378
379
        // merge them with the passed ones (DO override already registered loggers)
380
        /** @var \AppserverIo\Appserver\Core\Api\Node\LoggerNode $loggerToMerge */
381
        foreach ($contextNode->getLoggers() as $loggerToMerge) {
382
            $localLoggers[$loggerToMerge->getName()] = $loggerToMerge;
383
        }
384
385
        // set the loggers back to the context
386
        $this->setLoggers($localLoggers);
387
    }
388
}
389