AbstractViewHelper   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 54
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renderView() 0 9 1
A setView() 0 4 1
A getView() 0 4 1
A getServiceLocator() 0 4 1
1
<?php
2
3
namespace Faulancer\View;
4
5
use Faulancer\Exception\FileNotFoundException;
6
use Faulancer\Exception\ServiceNotFoundException;
7
use Faulancer\Service\Config;
8
use Faulancer\ServiceLocator\ServiceLocator;
9
10
/**
11
 * Class AbstractViewHelper
12
 *
13
 * @package Faulancer\View
14
 * @author  Florian Knapp <[email protected]>
15
 */
16
abstract class AbstractViewHelper
17
{
18
19
    /** @var ViewController */
20
    protected $view;
21
22
    /**
23
     * Render a view with given template and variables
24
     *
25
     * @param  string $template
26
     * @param  array  $variables
27
     *
28
     * @return string
29
     *
30
     * @throws ServiceNotFoundException
31
     * @throws FileNotFoundException
32
     */
33
    protected function renderView($template = '', array $variables = []) :string
34
    {
35
        /** @var Config $config */
36
        $config = $this->getServiceLocator()->get(Config::class);
37
38
        $templatePath = $config->get('viewsRoot') . '/helper';
39
40
        return (new ViewController())->setTemplate($templatePath . $template)->setVariables($variables)->render();
41
    }
42
43
    /**
44
     * @param ViewController $view
45
     */
46
    public function setView(ViewController $view)
47
    {
48
        $this->view = $view;
49
    }
50
51
    /**
52
     * @return ViewController
53
     */
54
    public function getView()
55
    {
56
        return $this->view;
57
    }
58
59
    /**
60
     * Get instance of service locator
61
     *
62
     * @return ServiceLocator
63
     */
64
    public function getServiceLocator() :ServiceLocator
65
    {
66
        return ServiceLocator::instance();
67
    }
68
69
}