Passed
Push — develop ( ad1a9e...8ea77e )
by Jens
02:45
created

ApplicationRunner::runSitemapComponents()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
rs 8.8571
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\CachableBaseComponent;
12
use CloudControl\Cms\components\Component;
13
use CloudControl\Cms\storage\Storage;
14
15
class ApplicationRunner
16
{
17
    private $storage;
18
    private $request;
19
20
    public function __construct(Storage $storage, Request $request)
21
    {
22
        $this->storage = $storage;
23
        $this->request = $request;
24
    }
25
26
    /**
27
     * Loop through all application components and run them
28
     *
29
     * @param $applicationComponents
30
     */
31
    public function runApplicationComponents($applicationComponents)
32
    {
33
        foreach ($applicationComponents as $key => $applicationComponent) {
34
            $class = $applicationComponent->component;
35
            $parameters = $applicationComponent->parameters;
36
            $applicationComponents[$key]->{'object'} = $this->getComponentObject($class, null, $parameters, null);
37
            $applicationComponents[$key]->{'object'}->run($this->storage);
38
        }
39
    }
40
41
    /**
42
     * @param string $class
43
     * @param string $template
44
     * @param array $parameters
45
     * @param \stdClass|null $matchedSitemapItem
46
     *
47
     * @return Component
48
     * @throws \Exception
49
     */
50
    private function getComponentObject($class = '', $template = '', $parameters = array(), $matchedSitemapItem)
51
    {
52
        $libraryComponentName = '\\CloudControl\Cms\\components\\' . $class;
53
        $userComponentName = '\\components\\' . $class;
54
55
        if (class_exists($libraryComponentName)) {
56
            $component = new $libraryComponentName($template, $this->request, $parameters, $matchedSitemapItem);
57
        } elseif (class_exists($userComponentName)) {
58
            $component = new $userComponentName($template, $this->request, $parameters, $matchedSitemapItem);
59
        } else {
60
            throw new \Exception('Could not load component ' . $class);
61
        }
62
63
        if (!$component instanceof Component) {
64
            throw new \Exception('Component not of type Component. Must inherit \CloudControl\Cms\components\Component');
65
        }
66
67
        return $component;
68
    }
69
70
    /**
71
     * Loop through all (matched) sitemap components and run them
72
     *
73
     * @param $matchedSitemapItems
74
     */
75
    public function runSitemapComponents($matchedSitemapItems)
76
    {
77
        foreach ($matchedSitemapItems as $key => $sitemapItem) {
78
            $class = $sitemapItem->component;
79
            $template = $sitemapItem->template;
80
            $parameters = $sitemapItem->parameters;
81
82
            $matchedSitemapItems[$key]->object = $this->getComponentObject($class, $template, $parameters, $sitemapItem);
83
84
            if ($matchedSitemapItems[$key]->object instanceof CachableBaseComponent
85
                && $matchedSitemapItems[$key]->object->isCachable()
86
                && $matchedSitemapItems[$key]->object->isCacheValid()) {
87
                continue;
88
            }
89
90
            $matchedSitemapItems[$key]->object->run($this->storage);
91
        }
92
    }
93
}