Passed
Push — master ( 40eec0...6215ec )
by Darío
02:02
created

Layout::setView()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Mvc;
12
13
use Drone\Mvc\AbstractController;
14
use Drone\Mvc\Exception;
15
16
/**
17
 * Layout class
18
 *
19
 * This class manages templates from views
20
 */
21
class Layout
22
{
23
    use \Drone\Util\ParamTrait;
24
25
    /**
26
     * Controller instance
27
     *
28
     * @var AbstractController
29
     */
30
    private $controller;
31
32
    /**
33
     * View path
34
     *
35
     * @var string
36
     */
37
    private $view;
38
39
    /**
40
     * Returns the instance of current controller
41
     *
42
     * @return AbstractController
43
     */
44
    public function getController()
45
    {
46
        return $this->controller;
47
    }
48
49
    /**
50
     * Returns the view
51
     *
52
     * @return string
53
     */
54
    public function getView()
55
    {
56
        return $this->view;
57
    }
58
59
    /**
60
     * Sets the view
61
     *
62
     * @param AbstractionModule $module
0 ignored issues
show
Bug introduced by
The type Drone\Mvc\AbstractionModule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
63
     * @param string $view
64
     *
65
     * @return null
66
     */
67
    public function setView($module, $view)
68
    {
69
        $config = $module->getConfig();
70
71
        if (!array_key_exists($view, $config["view_manager"]["view_map"]) || !file_exists($config["view_manager"]["view_map"][$view]))
72
            throw new Exception\ViewNotFoundException("The 'view' template " . $view . " does not exists");
73
74
        $this->view = $config["view_manager"]["view_map"][$view];
75
    }
76
77
    /**
78
     * Loads a view from a controller
79
     *
80
     * @throws Exception\PageNotFoundException
81
     *
82
     * @param AbstractController
83
     *
84
     * @return null
85
     */
86
    public function fromController(AbstractController $controller)
87
    {
88
        $this->setParams($controller->getParams());
89
        $this->controller = $controller;
90
91
        if (is_null($controller->getModule()))
92
            throw new \RuntimeException("No module instance found in controller '" . get_class($controller) . "'");
93
94
        if ($controller->getShowView())
95
            $this->view =
96
                $controller->getModule()->getModulePath() .'/'. $controller->getModule()->getModuleName() .'/'.
97
                $controller->getModule()->getViewPath()                .'/'.
98
                basename(str_replace('\\','/',get_class($controller))) .'/'.
99
                $controller->getMethod() . '.phtml';
100
101
        if ($controller->getTerminal())
102
        {
103
            if (file_exists($this->view))
104
                include $this->view;
105
        }
106
        else
107
        {
108
            if (!is_null($this->view) && !file_exists($this->view))
0 ignored issues
show
introduced by
The condition is_null($this->view) is always false.
Loading history...
109
                throw new Exception\ViewNotFoundException("The 'view' template " . $this->view . " does not exists");
110
111
            $config = $controller->getModule()->getConfig();
112
            $layout = $controller->getLayout();
113
114
            if (!array_key_exists($controller->getLayout(), $config["view_manager"]["template_map"]))
115
                throw new Exception\PageNotFoundException("The 'template' " . $layout . " was not defined in module.config.php");
116
117
            $template = $config["view_manager"]["template_map"][$controller->getLayout()];
118
119
            if (!file_exists($template))
120
                throw new Exception\PageNotFoundException("The 'template' " . $template . " does not exists");
121
122
            include $template;
123
        }
124
    }
125
126
    /**
127
     * Loads a view from a template file
128
     *
129
     * @throws Exception\PageNotFoundException
130
     *
131
     * @param AbstractionModule $module
132
     * @param string $template
133
     *
134
     * @return null
135
     */
136
    public function fromTemplate($module, $template)
137
    {
138
        $config = $module->getConfig();
139
        include $config["view_manager"]["template_map"][$template];
140
    }
141
142
    /**
143
     * Includes the file view
144
     *
145
     * @return null
146
     */
147
    public function content()
148
    {
149
        if (!file_exists($this->view))
150
            throw new Exception\ViewNotFoundException("The 'view' template " . $this->view . " does not exists");
151
152
        include $this->view;
153
    }
154
155
    /**
156
     * Alias to return the base path of the application
157
     *
158
     * @return string
159
     */
160
    public function basePath()
161
    {
162
        return $this->controller->getModule()->getRouter()->getBasePath();
0 ignored issues
show
Bug introduced by
The method getRouter() does not exist on Drone\Mvc\AbstractModule. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

162
        return $this->controller->getModule()->/** @scrutinizer ignore-call */ getRouter()->getBasePath();

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...
163
    }
164
}