Passed
Push — develop ( 68c5f2...f2fa17 )
by Jens
02:39
created

MultiComponent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by: Jens
4
 * Date: 25-3-2018
5
 */
6
7
namespace CloudControl\Cms\components;
8
9
10
use CloudControl\Cms\cc\Request;
11
use CloudControl\Cms\components\CachableBaseComponent;
12
use CloudControl\Cms\storage\Storage;
13
use CloudControl\Cms\components;
14
15
class MultiComponent extends CachableBaseComponent
16
{
17
    /**
18
     * MultiComponent constructor.
19
     * Applies parameters in the namespace "self" to parent
20
     *
21
     * @param string $template
22
     * @param Request $request
23
     * @param array $parameters
24
     * @param $matchedSitemapItem
25
     */
26
    public function __construct($template = '', Request $request, $parameters = array(), $matchedSitemapItem)
27
    {
28
        $selfParameters = $this->getParametersForNameSpace('self');
29
        parent::__construct($template, $request, $selfParameters, $matchedSitemapItem);
30
    }
31
32
    /**
33
     * Runs all components that are set
34
     *
35
     * @param Storage $storage
36
     */
37
    public function run(Storage $storage)
38
    {
39
        parent::run($storage);
40
41
        $components = $this->getParametersWithoutNamespace();
42
43
        foreach ($components as $namespace => $component) {
44
            $this->loadComponent($namespace, $component);
45
        }
46
    }
47
48
    /**
49
     * Tries to determine component class name and wheter it exists
50
     *
51
     * @param $namespace String
52
     * @param $component String
53
     */
54
    private function loadComponent($namespace, $component)
55
    {
56
        $fullyQualifiedCloudControlComponent = '\\CloudControl\\Cms\\components\\' . $component;
57
        $fullyQualifiedComponent = '\\components\\' . $component;
58
        if (class_exists($fullyQualifiedCloudControlComponent, true)) {
59
            $this->runComponent($namespace, $fullyQualifiedCloudControlComponent);
60
        } elseif (class_exists($fullyQualifiedComponent, true)) {
61
            $this->runComponent($namespace, $fullyQualifiedComponent);
62
        }
63
    }
64
65
    /**
66
     * Instantiates the component and runs it
67
     *
68
     * @param $namespace String
69
     * @param $fullyQualifiedComponent String
70
     */
71
    private function runComponent($namespace, $fullyQualifiedComponent)
72
    {
73
        $parameters = $this->getParametersForNameSpace($namespace);
74
        $component = new $fullyQualifiedComponent('', $this->request, $parameters, $this->matchedSitemapItem);
75
        if ($component instanceof components\Component) {
76
            $component->run($this->storage);
77
            $parameters = $component->getParameters();
78
            foreach ($parameters as $name => $value) {
79
                $this->parameters[$namespace . '_' . $name] = $value;
80
            }
81
        }
82
    }
83
84
    /**
85
     * Retrieves all parameters for the given namespace
86
     *
87
     * @param $namespace
88
     * @return array
89
     */
90
    private function getParametersForNameSpace($namespace)
91
    {
92
        $parameters = array();
93
        foreach($this->parameters as $key => $value) {
94
            if (0 === strpos($key, $namespace . ':')) {
95
                $parameters[substr($key, strlen($namespace) + 1)] = $value;
96
            }
97
        }
98
        return $parameters;
99
    }
100
101
    /**
102
     * Retrieves all parameters that have no namespace
103
     *
104
     * @return array
105
     */
106
    private function getParametersWithoutNamespace()
107
    {
108
        $parameters = array();
109
        foreach($this->parameters as $key => $value) {
110
            if (strpos($key, ':') === false) {
111
                $parameters[$key] = $value;
112
            }
113
        }
114
        return $parameters;
115
    }
116
}