AbstractBaseApplication   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 115
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A bootstrap() 0 13 2
B internalBootstrap() 0 29 1
A detachListeners() 0 11 2
A attachListeners() 0 6 2
B mergeConfig() 0 21 6
1
<?php
2
/**
3
 * Webino™ (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino for the canonical source repository
6
 * @copyright   Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoAppLib\Application;
12
13
use WebinoAppLib\Event\AppEvent;
14
use WebinoAppLib\Exception\DomainException;
15
use WebinoAppLib\Exception\InvalidArgumentException;
16
use WebinoAppLib\Service\Bootstrap;
17
use Zend\Config\Config;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, WebinoAppLib\Application\Config.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
19
/**
20
 * Class AbstractBaseApplication
21
 */
22
abstract class AbstractBaseApplication extends AbstractApplication
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "AbstractApplication" and comma; 1 found
Loading history...
23
    implements BaseApplicationInterface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
24
{
25
    /**
26
     * {@inheritDoc}
27
     */
28
    public function bootstrap()
29
    {
30
        if ($this->getConfig()->isReadOnly()) {
31
            throw new DomainException('Application is already bootstrapped');
32
        }
33
34
        return $this->internalBootstrap(
35
            $this->getServices()->get($this::BOOTSTRAP),
0 ignored issues
show
Documentation introduced by
$this->getServices()->get($this::BOOTSTRAP) is of type object|array, but the function expects a object<WebinoAppLib\Service\Bootstrap>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
            function () {
37
                $this->emit(AppEvent::BOOTSTRAP, $this);
0 ignored issues
show
Documentation introduced by
$this is of type this<WebinoAppLib\Applic...bstractBaseApplication>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
38
            }
39
        );
40
    }
41
42
    /**
43
     * @param object|Bootstrap $bootstrap
44
     * @param callable $trigger
45
     * @return AbstractConfiguredApplication
46
     * @triggers bootstrap
47
     */
48
    protected function internalBootstrap(Bootstrap $bootstrap, callable $trigger)
49
    {
50
        // lock core config
51
        $this->getConfig()->get(CoreConfig::CORE)->setReadOnly();
52
53
        $bootstrap->attachCoreListeners();
54
        $listeners = $this->listeners;
55
56
        // trigger bootstrap
57
        call_user_func($trigger);
58
59
        // configure application
60
        $bootstrap->configure();
61
62
        $detachedListeners = $this->detachListeners($listeners);
63
        $bootstrap->attachListeners()->detachCoreListeners();
64
65
        // trigger bootstrap again
66
        call_user_func($trigger);
67
68
        $bootstrap->attachCoreListeners();
69
        $this->attachListeners($detachedListeners);
70
71
        // lock config
72
        $this->getConfig()->setReadOnly();
73
74
        // return configured app
75
        return new ConfiguredApplication($this->getServices());
76
    }
77
78
    /**
79
     * Detach listeners from application events
80
     *
81
     * @param \Zend\EventManager\ListenerAggregateInterface[] $listeners
82
     * @return \Zend\EventManager\ListenerAggregateInterface[] Detached listeners
83
     */
84
    private function detachListeners(array $listeners)
85
    {
86
        $detachedListeners = [];
87
88
        foreach ($listeners as $listener) {
89
            $detachedListeners[] = clone $listener;
90
            $this->unbind($listener);
91
        }
92
93
        return $detachedListeners;
94
    }
95
96
    /**
97
     * Attach event listeners
98
     *
99
     * @param \Zend\EventManager\ListenerAggregateInterface[] $listeners
100
     */
101
    private function attachListeners(array $listeners)
102
    {
103
        foreach ($listeners as $listener) {
104
            $this->bind($listener);
105
        }
106
    }
107
108
    /**
109
     * Merge config into application config.
110
     *
111
     * @param array|Config|null $config
112
     * @return $this
113
     * @throws InvalidArgumentException
114
     */
115
    public function mergeConfig($config = null)
116
    {
117
        if (empty($config)) {
118
            return $this;
119
        }
120
121
        if (is_object($config) && method_exists($config, 'toArray')) {
122
            $config = $config->toArray();
123
        }
124
125
        if (is_array($config)) {
126
            $config = new Config($config);
127
        }
128
129
        if (!($config instanceof Config)) {
130
            throw new InvalidArgumentException('Expected array|Config|null');
131
        }
132
133
        $this->getConfig()->merge($config);
134
        return $this;
135
    }
136
}
137