MultiComponent   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 34
c 4
b 2
f 0
dl 0
loc 105
rs 10
wmc 15

6 Methods

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