Completed
Push — master ( ea37f6...c87bc5 )
by Gabriel
11:13
created

HasViewTrait::getView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Nip\Controllers\Traits;
4
5
use Nip\View;
6
7
/**
8
 * Trait HasViewTrait
9
 * @package Nip\Controllers\Traits
10
 */
11
trait HasViewTrait
12
{
13
    use AbstractControllerTrait;
14
15
    /**
16
     * @var View
17
     */
18
    protected $view;
19
20
    /**
21
     * @var string
22
     */
23
    protected $layout = 'default';
24
25
    public function loadView()
26
    {
27
        echo $this->getView()->load($this->getLayoutPath());
28
    }
29
30
    /**
31
     * @return View
32
     */
33
    public function getView()
34
    {
35
        if ( ! $this->view) {
36
            $this->view = $this->initView();
37
        }
38
39
        return $this->view;
40
    }
41
42
    /**
43
     * @param View $view
44
     */
45
    public function setView($view)
46
    {
47
        $this->view = $view;
48
    }
49
50
    /**
51
     * @return View
52
     */
53
    protected function initView()
54
    {
55
        $view = $this->getViewObject();
56
        $view = $this->populateView($view);
57
58
        return $view;
59
    }
60
61
    /**
62
     * @return View
63
     */
64
    protected function getViewObject()
65
    {
66
        return new View();
67
    }
68
69
    /**
70
     * @param View $view
71
     *
72
     * @return View
73
     */
74
    protected function populateView($view)
75
    {
76
        $this->populateViewPath($view);
77
78
        $view = $this->initViewVars($view);
79
        $view = $this->initViewContentBlocks($view);
80
81
        return $view;
82
    }
83
84
    /**
85
     * @param View $view
86
     */
87
    protected function populateViewPath($view)
88
    {
89
        if ( ! defined('MODULES_PATH')) {
90
            return;
91
        }
92
        $path = MODULES_PATH . $this->getRequest()->getModuleName() . '/views/';
93
        if ( ! is_dir($path)) {
94
            return;
95
        }
96
        $view->setBasePath(MODULES_PATH . $this->getRequest()->getModuleName() . '/views/');
97
    }
98
99
    /**
100
     * @param View $view
101
     *
102
     * @return View
103
     */
104
    protected function initViewVars($view)
105
    {
106
        $view->setRequest($this->getRequest());
107
        $view->set('controller', $this->getName());
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
108
        $view->set('action', $this->getRequest()->getActionName());
109
        $view->set('options', (isset($this->options) ? $this->options : null));
0 ignored issues
show
Bug introduced by
The property options 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...
110
111
        return $view;
112
    }
113
114
    /**
115
     * @param View $view
116
     *
117
     * @return View
118
     */
119
    protected function initViewContentBlocks($view)
120
    {
121
        $view->setBlock('content',
122
            $this->getRequest()->getControllerName() . '/' . $this->getRequest()->getActionName());
123
124
        return $view;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getLayoutPath()
131
    {
132
        return '/layouts/' . $this->getLayout();
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getLayout()
139
    {
140
        return $this->layout;
141
    }
142
143
    /**
144
     * @param string $layout
145
     */
146
    public function setLayout($layout)
147
    {
148
        $this->layout = $layout;
149
    }
150
}