AbstractController::getEntityManager()   A
last analyzed

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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * Koch Framework
5
 * Jens-André Koch © 2005 - onwards.
6
 *
7
 * This file is part of "Koch Framework".
8
 *
9
 * License: GNU/GPL v2 or any later version, see LICENSE file.
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace Koch\Module;
26
27
use Koch\Http\HttpRequest;
28
use Koch\Http\HttpRequestInterface;
29
use Koch\Http\HttpResponseInterface;
30
31
/**
32
 * ModuleController.
33
 *
34
 * The ModuleController is an abstract class (parent class)
35
 * to share some common features on/for all (Module/Action)-Controllers.
36
 * It`s abstract, because it should only be extended, not instantiated.
37
 */
38
abstract class AbstractController
39
{
40
    /**
41
     * @var object The rendering engine / view object
42
     */
43
    public $view = null;
44
45
    /**
46
     * @var string Name of the rendering engine
47
     */
48
    public $renderEngineName = null;
49
50
    /**
51
     * @var string The name of the template to render
52
     */
53
    public $template = null;
54
55
    /**
56
     * @var \Koch\Http\HttpResponse
57
     */
58
    public $response = null;
59
60
    /**
61
     * @var \Koch\Http\HttpRequest
62
     */
63
    public $request = null;
64
65
    /**
66
     * @var \Doctrine\ORM\EntityManager
67
     */
68
    public $entityManager = null;
69
70
    /**
71
     * @var array The Module Configuration Array
72
     */
73
    public static $moduleconfig = null;
74
75
    public function __construct(HttpRequestInterface $request, HttpResponseInterface $response)
76
    {
77
        $this->request  = $request;
0 ignored issues
show
Documentation Bug introduced by
$request is of type object<Koch\Http\HttpRequestInterface>, but the property $request was declared to be of type object<Koch\Http\HttpRequest>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
78
        $this->response = $response;
0 ignored issues
show
Documentation Bug introduced by
$response is of type object<Koch\Http\HttpResponseInterface>, but the property $response was declared to be of type object<Koch\Http\HttpResponse>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
79
    }
80
81
    /**
82
     * Returns the Doctrine Entity Manager.
83
     *
84
     * @return \Doctrine\ORM\EntityManager
85
     */
86
    public function getEntityManager()
87
    {
88
        return $this->entityManager;
89
    }
90
91
    public function setEntityManager($em)
92
    {
93
        $this->entityManager = $em;
94
    }
95
96
    /**
97
     * The name of the entity extracted from the classname.
98
     *
99
     * @param string Classname
100
     *
101
     * @return string The name of the entity extracted from classname
102
     */
103
    public function getEntityNameFromClassname()
104
    {
105
        $matches = [];
106
107
        // takes classname, e.g. "Application\Modules\News\Controller\NewsController"
108
        $class = get_class($this);
109
        preg_match("~Controller\\\(.*)Controller~is", $class, $matches);
110
111
        // and returns the entity name, e.g. "Entity\News"
112
        $this->entityName = 'Entity\\' . $matches[1];
0 ignored issues
show
Bug introduced by
The property entityName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
113
114
        return $this->entityName;
115
    }
116
117
    /**
118
     * Proxy/Convenience Getter Method for the Repository of the current Module.
119
     *
120
     *
121
     * @param string $entityName Name of an Entity, like "\Entity\User".
0 ignored issues
show
Documentation introduced by
Should the type for parameter $entityName not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
122
     *
123
     * @return \Doctrine\ORM\EntityRepository
124
     */
125
    public function getModel($entityName = null)
126
    {
127
        if (null === $entityName) {
128
            $entityName = $this->getEntityNameFromClassname();
129
        }
130
131
        return $this->entityManager->getRepository($entityName);
132
    }
133
134
    /**
135
     * Saves this and all others models (calls persist + flush)
136
     * Save (save one)
137
     * Flush (save all).
138
     *
139
     * @param \Doctrine\ORM\Mapping\Entity $model Entity.
140
     * @param bool                         $flush Uses flush on true, save on false. Defaults to flush (true).
141
     */
142
    public function saveModel(\Doctrine\ORM\Mapping\Entity $model, $flush = true)
143
    {
144
        $this->entityManager->persist($model);
145
146
        if ($flush === true) {
147
            $this->entityManager->flush();
148
        } else {
149
            $this->entityManager->save();
0 ignored issues
show
Bug introduced by
The method save() does not seem to exist on object<Doctrine\ORM\EntityManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
        }
151
    }
152
153
    /**
154
     * Initializes the model (active records/entities/repositories) of the module.
155
     *
156
     * @param $modulename Modulname
157
     * @param $recordname Recordname
158
     */
159
    public static function setModel($modulename = null, $entity = null)
160
    {
161
        $module_models_path = '';
0 ignored issues
show
Coding Style introduced by
$module_models_path does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Unused Code introduced by
$module_models_path is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
162
163
        /*
164
         * Load the Records for the current module, if no modulename is specified.
165
         * This is for lazy usage in the modulecontroller: $this->initModel();
166
         */
167
        if ($modulename === null) {
168
            $modulename = HttpRequest::getRoute()->getModuleName();
169
        }
170
171
        $module_models_path = APPLICATION_MODULES_PATH . mb_strtolower($modulename) . '/model/';
0 ignored issues
show
Coding Style introduced by
$module_models_path does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
172
173
        // check if the module has a models dir
174
        if (is_dir($module_models_path)) {
0 ignored issues
show
Coding Style introduced by
$module_models_path does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
175
            if ($entity !== null) {
176
                // use second parameter of method
177
                $entity = $module_models_path . 'Entities/' . ucfirst($entity) . '.php';
0 ignored issues
show
Coding Style introduced by
$module_models_path does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
178
            } else {
179
                // build entity filename by modulename
180
                $entity = $module_models_path . 'Entities/' . ucfirst($modulename) . '.php';
0 ignored issues
show
Coding Style introduced by
$module_models_path does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
181
            }
182
183 View Code Duplication
            if (is_file($entity) && class_exists('Entity\\' . ucfirst($modulename), false)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
                include $entity;
185
            }
186
187
            $repos = $module_models_path . 'Repositories/' . ucfirst($modulename) . 'Repository.php';
0 ignored issues
show
Coding Style introduced by
$module_models_path does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
188
189 View Code Duplication
            if (is_file($repos) && class_exists('Entity\\' . ucfirst($modulename), false)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
                include $repos;
191
            }
192
        }
193
        // else Module has no Model Data
194
    }
195
196
    /**
197
     * Gets a Module Config.
198
     *
199
     * @param string $modulename Modulename.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $modulename not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
200
     *
201
     * @return array configuration array of module
202
     */
203
    public static function getModuleConfig($modulename = null)
204
    {
205
        $config = self::getInjector()->instantiate('\Koch\Config\Config');
206
207
        return self::$moduleconfig = $config->readModuleConfig($modulename);
208
    }
209
210
    /**
211
     * Gets a Config Value or sets a default value.
212
     *
213
     * @example
214
     * Usage for one default variable:
215
     * self::getConfigValue('items_newswidget', '8');
216
     * Gets the value for the key items_newswidget from the moduleconfig or sets the value to 8.
217
     *
218
     * Usage for two default variables:
219
     * self::getConfigValue('items_newswidget', $_GET['numberNews'], '8');
220
     * Gets the value for the key items_newswidget from the moduleconfig or sets the value
221
     * incomming via GET, if nothing is incomming, sets the default value of 8.
222
     *
223
     * @param string $keyname     The keyname to find in the array.
224
     * @param mixed  $default_one A default value returned, when keyname was not found.
225
     * @param mixed  $default_two A default value returned, when keyname was not found and default_one is null.
226
     *
227
     * @return mixed
228
     */
229
    public static function getConfigValue($keyname, $default_one = null, $default_two = null)
0 ignored issues
show
Coding Style introduced by
$default_one does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
230
    {
231
        // if we don't have a moduleconfig array yet, get it
232
        if (self::$moduleconfig === null) {
233
            self::$moduleconfig = self::getModuleConfig();
234
        }
235
236
        // try a lookup of the value by keyname
237
        $value = \Koch\Functions\Functions::array_find_element_by_key($keyname, self::$moduleconfig);
238
239
        // return value or default
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
240 View Code Duplication
        if (empty($value) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
            return $value;
242
        } elseif ($default_one !== null) {
0 ignored issues
show
Coding Style introduced by
$default_one does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
243
            return $default_one;
0 ignored issues
show
Coding Style introduced by
$default_one does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
244
        } elseif ($default_two !== null) {
0 ignored issues
show
Coding Style introduced by
$default_two does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
245
            return $default_two;
0 ignored issues
show
Coding Style introduced by
$default_two does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
246
        } else {
247
            return;
248
        }
249
    }
250
251
    /**
252
     * Get the dependency injector.
253
     *
254
     * @return Returns a static reference to the Dependency Injector
255
     */
256
    public static function getInjector()
257
    {
258
        return \Clansuite\Application::getInjector();
259
    }
260
261
    /**
262
     * Set view.
263
     *
264
     * @param object $view RenderEngine Object
265
     */
266
    public function setView($view)
267
    {
268
        $this->view = $view;
269
    }
270
271
    /**
272
     * Get view returns the render engine.
273
     *
274
     * @param string $renderEngineName Name of the render engine, like smarty, phptal.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $renderEngineName not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
275
     *
276
     * @return Returns the View Object (Rendering Engine)
277
     */
278
    public function getView($renderEngineName = null)
279
    {
280
        // set the renderengine name
281
        if ($renderEngineName !== null) {
282
            $this->setRenderEngine($renderEngineName);
283
        }
284
285
        // if already set, get the rendering engine from the view variable
286
        if ($this->view !== null) {
287
            return $this->view;
288
        } else {
289
            // else, set the RenderEngine to the view variable and return it
290
            $this->view = $this->getRenderEngine();
291
292
            return $this->view;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->view; (Koch\View\Renderer) is incompatible with the return type documented by Koch\Module\AbstractController::getView of type Koch\Module\Returns.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
293
        }
294
    }
295
296
    /**
297
     * sets the Rendering Engine.
298
     *
299
     * @param string $renderEngineName Name of the RenderEngine
300
     */
301
    public function setRenderEngine($renderEngineName)
302
    {
303
        $this->renderEngineName = $renderEngineName;
304
305
        $this->request->getRoute()->setRenderEngine($renderEngineName);
306
    }
307
308
    /**
309
     * Returns the Name of the Rendering Engine.
310
     * Returns Json if an XMLHttpRequest is given.
311
     * Returns Smarty as default if no rendering engine is set.
312
     *
313
     * @return string object, smarty as default
314
     */
315
    public function getRenderEngineName()
316
    {
317
        // check if the requesttype is xmlhttprequest (ajax) is incomming, then we will return data in json format
318
        if ($this->request->isAjax()) {
319
            $this->setRenderEngine('json');
320
        }
321
322
        // use smarty as default, if renderEngine is not set and it's not an ajax request
323
        if (empty($this->renderEngineName)) {
324
            $this->setRenderEngine('smarty');
325
        }
326
327
        return $this->renderEngineName;
328
    }
329
330
    /**
331
     * Returns the Rendering Engine Object via view_factory.
332
     *
333
     * @return renderengine object
0 ignored issues
show
Documentation introduced by
Should the return type not be \Koch\View\Renderer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
334
     */
335
    public function getRenderEngine()
336
    {
337
        return \Koch\View\Factory::getRenderer($this->getRenderEngineName(), self::getInjector());
338
    }
339
340
    /**
341
     * Sets the Render Mode.
342
     *
343
     * @param string $mode The RenderModes are LAYOUT or PARTIAL.
344
     */
345
    public function setRenderMode($mode)
346
    {
347
        $this->getView()->renderMode = $mode;
348
    }
349
350
    /**
351
     * Get the Render Mode.
352
     *
353
     * @return string LAYOUT|PARTIAL
354
     */
355
    public function getRenderMode()
356
    {
357
        if (empty($this->getView()->renderMode)) {
358
            $this->getView()->renderMode = 'LAYOUT';
359
        }
360
361
        return $this->getView()->renderMode;
362
    }
363
364
    /**
365
     * modulecontroller->display();.
366
     *
367
     * All Output is done via the Response Object.
368
     * ModelData -> View -> Response Object
369
     *
370
     * 1. getTemplateName() - get the template to render.
371
     * 2. getView() - gets an instance of the render engine.
372
     * 3. assign model data to that view object (a,b,c)
373
     * 5. set data to response object
374
     *
375
     * @param $templates mixed|array|string Array with keys 'layout_template' / 'content_template' and templates
376
     * as values or just content template name.
377
     */
378
    public function display($templates = null)
379
    {
380
        // get the view
381
        $this->view = $this->getView();
382
383
        // get the view mapper
384
        $view_mapper = $this->view->getViewMapper();
0 ignored issues
show
Coding Style introduced by
$view_mapper does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
385
386
        // set layout and content template by parameter array
387
        if (is_array($templates)) {
388
            if ($templates['layout_template'] !== null) {
389
                $view_mapper->setLayoutTemplate($templates['layout_template']);
0 ignored issues
show
Coding Style introduced by
$view_mapper does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
390
            }
391
392
            if ($templates['content_template'] !== null) {
393
                $view_mapper->setTemplate($templates['content_template']);
0 ignored issues
show
Coding Style introduced by
$view_mapper does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
394
            }
395
        }
396
397
        // only the "content template" was set
398
        if (is_string($templates)) {
399
            $view_mapper->setTemplate($templates);
0 ignored issues
show
Coding Style introduced by
$view_mapper does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
400
        }
401
402
        // get templatename
403
        $template = $view_mapper->getTemplateName();
0 ignored issues
show
Coding Style introduced by
$view_mapper does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
404
405
        // Debug display of Layout Template and Content Template
406
        //\Koch\Debug\Debug::firebug('Layout/Wrapper Template: ' . $this->view->getLayoutTemplate() . '<br />');
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
407
        //\Koch\Debug\Debug::firebug('Template Name: ' . $template . '<br />');
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
408
409
        // render the content / template
410
        $content = $this->view->render($template);
411
412
        // push content to the response object
413
        $this->response->setContent($content);
414
415
        unset($content, $template);
416
    }
417
418
    /**
419
     * This loads and initializes a formular from the module directory.
420
     *
421
     * @param string $formname       The name of the formular.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $formname not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
422
     * @param string $module         The name of the action.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $module not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
423
     * @param bool   $assign_to_view If true, the form is directly assigned as formname to the view
424
     */
425
    public function loadForm($formname = null, $module = null, $action = null, $assign_to_view = true)
0 ignored issues
show
Coding Style introduced by
$assign_to_view does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
426
    {
427
        if (null === $module) {
428
            $module = $this->request->getRoute()->getModule();
429
        }
430
431
        if (null === $action) {
432
            $action = $this->request->getRoute()->getAction();
433
        }
434
435
        if (null === $formname) {
436
            // construct form name like "news"_"action_show"
437
            $formname = ucfirst($module) . '_' . ucfirst($action); // @todo adjust to PSR0
438
        }
439
440
        // construct formname, classname, filename, load file, instantiate the form
441
        $classname = 'Koch\Form\\' . $formname;
442
        $filename  = mb_strtolower($formname) . 'Form.php';
443
        $directory = APPLICATION_MODULES_PATH . mb_strtolower($module) . '/Form/';
444
445
        Loader::requireFile($directory . $filename, $classname);
446
447
        // form preparation stage (combine description and add additional formelements)
448
        $form = new $classname();
449
450
        // assign form object directly to the view or return to work with it
451
        if ($assign_to_view === true) {
0 ignored issues
show
Coding Style introduced by
$assign_to_view does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
452
            // do not call $form->render(), it's already done
453
            $this->getView()->assign('form', $form);
454
        } else {
455
            return $form;
456
        }
457
    }
458
459
    /**
460
     * Redirect to Referer.
461
     */
462
    public function redirectToReferer()
463
    {
464
        $referer = $this->request->getReferer();
465
466
        // we have a referer in the environment
467
        if (empty($referer) === false) {
468
            $this->redirect(SERVER_URL . $referer);
469
        } else { // build referer on base of the current module
470
            $route = $this->request->getRoute();
471
472
            // we use internal rewrite style here: /module/action
473
            $redirect_to = '/' . $route->getModuleName();
0 ignored issues
show
Coding Style introduced by
$redirect_to does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
474
475
            $submodule = $route->getSubModuleName();
476
477
            if (empty($submodule) === false) {
478
                $redirect_to .= '/' . $submodule;
0 ignored issues
show
Coding Style introduced by
$redirect_to does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
479
            }
480
481
            // redirect() builds the url
482
            $this->response->redirect($redirect_to);
0 ignored issues
show
Coding Style introduced by
$redirect_to does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
483
        }
484
    }
485
486
    /**
487
     * Shortcut for Redirect with an 404 Response Code.
488
     *
489
     * @param string $url  Redirect to this URL
490
     * @param int    $time seconds before redirecting (for the html tag "meta refresh")
491
     */
492
    public function redirect404($url, $time = 5)
493
    {
494
        $this->response->redirect($url, $time, 404, _('The URL you requested is not available.'));
495
    }
496
497
    /**
498
     * Redirects to a new URL.
499
     * It's a proxy method using the HttpResponse Object.
500
     *
501
     * @param string $url        Redirect to this URL.
502
     * @param int    $time       Seconds before redirecting (for the html tag "meta refresh")
503
     * @param int    $statusCode Http status code, default: '303' => 'See other'
504
     * @param string $message    Text of redirect message.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $message not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
505
     * @param string $mode       The redirect mode: LOCATION, REFRESH, JS, HTML.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $mode not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
506
     */
507
    public function redirect($url, $time = 0, $statusCode = 303, $message = null, $mode = null)
508
    {
509
        $this->response->redirect($url, $time, $statusCode, $message, $mode);
510
    }
511
512
    /**
513
     * addEvent (shortcut for usage in modules).
514
     *
515
     * @param string Name of the Event
516
     * @param object Eventobject
517
     */
518
    public function addEvent($eventName, \Koch\Event\Event $event)
519
    {
520
        \Koch\Event\Dispatcher::instantiate()->addEventHandler($eventName, $event);
521
    }
522
523
    /**
524
     * triggerEvent is shortcut/convenience method for Eventdispatcher->triggerEvent.
525
     *
526
     * @param mixed (string|object) $event   Name of Event or Event object to trigger.
527
     * @param object                $context Context of the event triggering, often simply ($this). Default Null.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $context not be object|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
528
     * @param string                $info    Some pieces of information. Default Null.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $info not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
529
     */
530
    public function triggerEvent($event, $context = null, $info = null)
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $info is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
531
    {
532
        \Koch\Event\Dispatcher::instantiate()->triggerEvent($event, $context = null, $info = null);
533
    }
534
535
    /**
536
     * Shortcut to set a Flashmessage.
537
     *
538
     * @param string $type    string error, warning, notice, success, debug
539
     * @param string $message string A textmessage.
540
     */
541
    public static function setFlashmessage($type, $message)
542
    {
543
        \Koch\Session\Flashmessages::setMessage($type, $message);
544
    }
545
546
    /**
547
     * Adds a new breadcrumb.
548
     *
549
     * @param string $title                  Name of the trail element. Use Gettext _('Title')!
550
     * @param string $link                   Link of the trail element
551
     * @param string $replace_array_position Position in the array to replace with name/trail. Start = 0.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $replace_array_position not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
552
     */
553
    public static function addBreadcrumb($title, $link = '', $replace_array_position = null)
0 ignored issues
show
Coding Style introduced by
$replace_array_position does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
554
    {
555
        \Koch\View\Helper\Breadcrumb::add($title, $link, $replace_array_position);
0 ignored issues
show
Coding Style introduced by
$replace_array_position does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
556
    }
557
558
    /**
559
     * Returns the HttpRequest Object.
560
     *
561
     * @return HttpRequest
562
     */
563
    public function getHttpRequest()
564
    {
565
        return $this->request;
566
    }
567
568
    /**
569
     * Returns the HttpResponse Object.
570
     *
571
     * @return \Koch\Http\HttpResponse
572
     */
573
    public function getHttpResponse()
574
    {
575
        return $this->response;
576
    }
577
}
578