Completed
Push — master ( c6aea9...538fb8 )
by Flo
05:04 queued 02:10
created

Controller::getDb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Class Controller
4
 *
5
 * @package Faulancer\Controller
6
 * @author Florian Knapp <[email protected]>
7
 */
8
namespace Faulancer\Controller;
9
10
use Faulancer\ORM\EntityManager;
11
use Faulancer\Service\ORM;
12
use Faulancer\ServiceLocator\ServiceInterface;
13
use Faulancer\View\ViewController;
14
use Faulancer\ServiceLocator\ServiceLocator;
15
16
/**
17
 * Class Controller
18
 */
19
abstract class Controller
20
{
21
22
    /**
23
     * Holds the views per controller request
24
     * @var array
25
     */
26
    private $viewArray = [];
27
28
    /**
29
     * Returns the service locator
30
     *
31
     * @return ServiceLocator
32
     */
33
    public function getServiceLocator()
34
    {
35
        return ServiceLocator::instance();
36
    }
37
38
    /**
39
     * Returns the view controller
40
     *
41
     * @return ViewController
42
     */
43
    public function getView()
44
    {
45
        $calledClass = get_called_class();
46
47
        if (in_array($calledClass, array_keys($this->viewArray))) {
48
            return $this->viewArray[$calledClass];
49
        }
50
51
        $viewController = new ViewController();
52
        $this->viewArray[$calledClass] = $viewController;
53
54
        return $viewController;
55
    }
56
57
    /**
58
     * Returns the orm/entity manager
59
     *
60
     * @return ORM|ServiceInterface
61
     */
62
    public function getDb()
63
    {
64
        return $this->getServiceLocator()->get(ORM::class);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getServiceLocator...er\Service\ORM::class); of type Faulancer\ServiceLocator...ocator\FactoryInterface adds the type Faulancer\ServiceLocator\FactoryInterface to the return on line 64 which is incompatible with the return type documented by Faulancer\Controller\Controller::getDb of type Faulancer\ServiceLocator\ServiceInterface.
Loading history...
65
    }
66
67
    /**
68
     * Render view with given template
69
     *
70
     * @param  string $template
71
     * @param  array $variables
72
     * @return string
73
     */
74
    public function render(string $template = '', $variables = [])
75
    {
76
        return $this->getView()->setTemplate($template)->setVariables($variables)->render();
77
    }
78
79
}