Passed
Push — develop ( 6c7a78...4bd14e )
by Jens
02:29
created

ApplicationRunner   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 73
rs 10
wmc 9
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A runApplicationComponents() 0 9 2
A getComponentObject() 0 19 4
A runSitemapComponents() 0 12 2
1
<?php
2
/**
3
 * Created by: Jens
4
 * Date: 12-10-2017
5
 */
6
7
namespace CloudControl\Cms\cc\application;
8
9
10
use CloudControl\Cms\cc\Request;
11
use CloudControl\Cms\components\Component;
12
use CloudControl\Cms\storage\Storage;
13
14
class ApplicationRunner
15
{
16
    private $storage;
17
    private $request;
18
19
    public function __construct(Storage $storage, Request $request)
20
    {
21
        $this->storage = $storage;
22
        $this->request = $request;
23
    }
24
25
    /**
26
     * Loop through all application components and run them
27
     *
28
     * @param $applicationComponents
29
     */
30
    public function runApplicationComponents($applicationComponents)
31
    {
32
        foreach ($applicationComponents as $key => $applicationComponent) {
33
            $class = $applicationComponent->component;
34
            $parameters = $applicationComponent->parameters;
35
            $applicationComponents[$key]->{'object'} = $this->getComponentObject($class, null, $parameters, null);
36
            $applicationComponents[$key]->{'object'}->run($this->storage);
37
        }
38
    }
39
40
    /**
41
     * @param string $class
42
     * @param string $template
43
     * @param array $parameters
44
     * @param \stdClass|null $matchedSitemapItem
45
     *
46
     * @return Component
47
     * @throws \Exception
48
     */
49
    private function getComponentObject($class = '', $template = '', $parameters = array(), $matchedSitemapItem)
50
    {
51
        $libraryComponentName = '\\CloudControl\Cms\\components\\' . $class;
52
        $userComponentName = '\\components\\' . $class;
53
54
        if (class_exists($libraryComponentName)) {
55
            $component = new $libraryComponentName($template, $this->request, $parameters, $matchedSitemapItem);
56
        } elseif (class_exists($userComponentName)) {
57
            $component = new $userComponentName($template, $this->request, $parameters, $matchedSitemapItem);
58
        } else {
59
            throw new \Exception('Could not load component ' . $class);
60
        }
61
62
        if (!$component instanceof Component) {
63
            throw new \Exception('Component not of type Component. Must inherit \CloudControl\Cms\components\Component');
64
        }
65
66
        return $component;
67
    }
68
69
    /**
70
     * Loop through all (matched) sitemap components and run them
71
     *
72
     * @param $matchedSitemapItems
73
     */
74
    public function runSitemapComponents($matchedSitemapItems)
75
    {
76
        foreach ($matchedSitemapItems as $key => $sitemapItem) {
77
            $class = $sitemapItem->component;
78
            $template = $sitemapItem->template;
79
            $parameters = $sitemapItem->parameters;
80
81
            $matchedSitemapItems[$key]->object = $this->getComponentObject($class, $template, $parameters, $sitemapItem);
82
83
            $matchedSitemapItems[$key]->object->run($this->storage);
84
        }
85
    }
86
}