Completed
Push — master ( c8cc94...b30129 )
by Андрей
17:58 queued 15:52
created

OverrideAppConfigTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 7
dl 0
loc 67
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
setApplicationConfig() 0 1 ?
getApplicationConfig() 0 1 ?
B overrideAppConfig() 0 40 2
1
<?php
2
/**
3
 * @link     https://github.com/nnx-framework/zf2-test-toolkit
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\ZF2TestToolkit\Utils;
7
8
use Nnx\ZF2TestToolkit\Listener\CallbacksListenerAggregate;
9
use Zend\Mvc\MvcEvent;
10
use Zend\ServiceManager\ServiceManager;
11
use Zend\Stdlib\ArrayUtils;
12
use Ramsey\Uuid\Uuid;
13
14
/**
15
 * Class OverrideAppConfigTrait
16
 *
17
 * @package Nnx\ZF2TestToolkit\Utils
18
 */
19
trait OverrideAppConfigTrait
20
{
21
    /**
22
     * Устанавливает конфиг приолжения
23
     *
24
     * @param $applicationConfig
25
     *
26
     * @return void
27
     */
28
    abstract public function setApplicationConfig($applicationConfig);
29
30
    /**
31
     * Возвращает конфиг приложения
32
     *
33
     * @return array
34
     */
35
    abstract public function getApplicationConfig();
36
37
    /**
38
     * Расширяет конфиг приложения
39
     *
40
     * @param array $newConfig
41
     *
42
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
43
     * @throws \Zend\ServiceManager\Exception\InvalidServiceNameException
44
     */
45
    public function overrideAppConfig(array $newConfig = [])
46
    {
47
        $applicationConfig = $this->getApplicationConfig();
48
49
        if (is_array($applicationConfig)) {
50
            $listenerName = 'bootstrapAppHandler_' . Uuid::uuid4()->toString();
51
52
            $appConfig = ArrayUtils::merge($applicationConfig, [
53
                'listeners' => [
54
                    $listenerName
55
                ],
56
                'service_manager' => [
57
                    'services' => [
58
                        $listenerName => new CallbacksListenerAggregate([
59
                            MvcEvent::EVENT_BOOTSTRAP => [
60
                                [
61
                                    'handler' => function (MvcEvent $e) use ($newConfig) {
62
                                        /** @var ServiceManager $sm */
63
                                        $sm = $e->getApplication()->getServiceManager();
64
                                        $baseAppConfig = $sm->get('Config');
65
66
                                        $appConfig = ArrayUtils::merge($baseAppConfig, $newConfig);
0 ignored issues
show
Bug introduced by
It seems like $baseAppConfig defined by $sm->get('Config') on line 64 can also be of type object; however, Zend\Stdlib\ArrayUtils::merge() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
67
68
69
                                        $originalAllowOverride = $sm->getAllowOverride();
70
                                        $sm->setAllowOverride(true);
71
                                        $sm->setService('config', $appConfig);
72
                                        $sm->setAllowOverride($originalAllowOverride);
73
74
                                    }
75
                                ]
76
                            ]
77
                        ])
78
                    ]
79
                ]
80
            ]);
81
82
            $this->setApplicationConfig($appConfig);
83
        }
84
    }
85
}
86