Passed
Push — develop ( 73ee1c...b9875a )
by Jens
06:23
created

BaseComponent::getTemplatePathFromApplication()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 11
rs 9.4285
1
<?php
2
3
namespace CloudControl\Cms\components {
4
5
    use CloudControl\Cms\cc\Application;
6
    use CloudControl\Cms\cc\Request;
7
    use CloudControl\Cms\storage\Storage;
8
9
    class BaseComponent implements Component
10
    {
11
        /**
12
         * @var string
13
         */
14
        protected $template;
15
        /**
16
         * @var \CloudControl\Cms\cc\Request
17
         */
18
        protected $request;
19
        /**
20
         * @var Storage
21
         */
22
        protected $storage;
23
        /**
24
         * @var mixed
25
         */
26
        protected $renderedContent;
27
        /**
28
         * @var array
29
         */
30
        protected $parameters = array();
31
        /***
32
         * @var \stdClass
33
         */
34
        protected $matchedSitemapItem;
35
36
        /**
37
         * @var Application
38
         */
39
        protected $application = null;
40
41
        /**
42
         * BaseComponent constructor.
43
         *
44
         * @param string $template
45
         * @param Request $request
46
         * @param array $parameters
47
         * @param         $matchedSitemapItem
48
         */
49
        public function __construct($template = '', Request $request, $parameters = array(), $matchedSitemapItem)
50
        {
51
            $this->template = $template;
52
            $this->request = $request;
53
            $this->parameters = (array)$parameters;
54
            $this->matchedSitemapItem = $matchedSitemapItem;
55
        }
56
57
        /**
58
         * Hook for implementation in derived classes
59
         *
60
         * @param Storage $storage
61
         */
62
        public function run(Storage $storage)
63
        {
64
            $this->storage = $storage;
65
        }
66
67
        /**
68
         * Renders the template
69
         *
70
         * @param null|Application $application
71
         *
72
         * @throws \Exception
73
         */
74
        public function render($application = null)
75
        {
76
            $this->application = $application;
77
            $this->renderedContent = $this->renderTemplate($this->template, true, $application);
78
        }
79
80
        /**
81
         * Returns the rendered content
82
         *
83
         * @return mixed
84
         */
85
        public function get()
86
        {
87
            return $this->renderedContent;
88
        }
89
90
        /**
91
         * Decoupled render method, for usage in derived classes
92
         *
93
         * @param string $template
94
         *
95
         * @param bool $obClean
96
         * @param null | Application $application
97
         * @param Application $application
98
         *
99
         * @return string
100
         * @throws \Exception
101
         */
102
        public function renderTemplate($template = '', $obClean = true, $application = null)
103
        {
104
            $templatePath = $this->getTemplatePath($template, $application);
0 ignored issues
show
Bug introduced by
It seems like $application defined by parameter $application on line 102 can be null; however, CloudControl\Cms\compone...nent::getTemplatePath() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
105
            if (realpath($templatePath) !== false) {
106
                if ($obClean) {
107
                    ob_clean();
108
                }
109
                $this->parameters['request'] = $this->request;
110
                if ($application !== null) {
111
                    $acParameters = $application->getAllApplicationComponentParameters();
112
                    foreach ($acParameters as $parameters) {
113
                        extract($parameters);
114
                    }
115
                }
116
                extract($this->parameters);
117
                if ($obClean) {
118
                    include($templatePath);
119
                    return ob_get_contents();
120
                } else {
121
                    return include($templatePath);
122
                }
123
124
            } else {
125
                if ($template !== null) { // If template is null, its a application component, which doesnt have a template
126
                    throw new \Exception('Couldnt find template ' . $templatePath);
127
                }
128
            }
129
        }
130
131
        /**
132
         * Alias for renderTemplate for usage to include templates in other templates
133
         *
134
         * @param string $template
135
         *
136
         * @param array $parameters
137
         *
138
         * @return string
139
         * @throws \Exception
140
         */
141
        public function includeTemplate($template = '', $parameters = array())
142
        {
143
            if (is_array($parameters)) {
144
                foreach ($parameters as $name => $value) {
145
                    $this->parameters[$name] = $value;
146
                }
147
            }
148
            return $this->renderTemplate($template, false);
149
        }
150
151
        public function getParameters()
152
        {
153
            return $this->parameters;
154
        }
155
156
        /**
157
         * @param $template
158
         * @param null | Application $application
159
         * @return string
160
         */
161
        protected function getTemplateDir($template, $application = null)
162
        {
163
            $templatePath = '';
164
            if ($application !== null) {
165
                $templatePath = $application->getTemplateDir();
166
            }
167
            $templatePath = $templatePath . $template . '.php';
168
            return $templatePath;
169
        }
170
171
        /**
172
         * @param string $template
173
         * @param Application $application
174
         * @return string
175
         */
176
        private function getTemplatePath($template, $application)
177
        {
178
            $templateDir = $this->getTemplateDir($template, $application);
179
            if ($application !== null) {
180
                $templatePath = $this->getTemplatePathFromApplication($application, $templateDir);
181
            } elseif ($this->application !== null) {
182
                $templatePath = $this->getTemplatePathFromApplication($this->application, $templateDir);
183
            } else {
184
                $templatePath = $templateDir;
185
            }
186
            return $templatePath;
187
        }
188
189
        /**
190
         * @param Application $application
191
         * @param string $templateDir
192
         * @return string
193
         */
194
        private function getTemplatePathFromApplication($application, $templateDir)
195
        {
196
            $rootDir = $application->getRootDir();
197
198
            if (strpos($templateDir, $rootDir) === false) {
199
                $templatePath = $rootDir . DIRECTORY_SEPARATOR . $templateDir;
200
            } else {
201
                $templatePath = $templateDir;
202
            }
203
            return $templatePath;
204
        }
205
    }
206
}