Passed
Push — master ( 148695...692a71 )
by Gabriel
04:05 queued 10s
created

HasViewTrait::initViewContentBlocks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 2
b 0
f 2
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Nip\Controllers\Traits;
4
5
use Nip\Controllers\View\ControllerViewHydrator;
6
use Nip\Http\Request;
7
use Nip\View;
8
9
/**
10
 * Trait HasViewTrait
11
 * @package Nip\Controllers\Traits
12
 */
13
trait HasViewTrait
14
{
15
    use AbstractControllerTrait;
16
17
    /**
18
     * @var View
19
     */
20
    protected $view;
21
22
    /**
23
     * @var string
24
     */
25
    protected $layout = 'default';
26
27
    /**
28
     * @param bool $return
29
     * @return bool|string|null
30
     */
31
    public function loadView($return = false)
32
    {
33
        $view = $this->getView();
34
        $this->populateView($view);
35
        $content = $view->load($this->getLayoutPath());
36
        if ($return == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
37
            return $content;
38
        }
39
        echo $content;
40
    }
41
42
    /**
43
     * @return View
44
     */
45
    public function getView()
46
    {
47
        if (!$this->view) {
48
            $this->view = $this->initView();
49
        }
50
51
        return $this->view;
52
    }
53
54
    /**
55
     * @param View $view
56
     */
57
    public function setView($view)
58
    {
59
        $this->view = $view;
60
    }
61
62
    /**
63
     * @return View
64
     */
65
    protected function initView()
66
    {
67
        $request = $this->getRequest();
68
        if ($request instanceof Request) {
0 ignored issues
show
introduced by
$request is always a sub-type of Nip\Http\Request.
Loading history...
69
            if (isset($request->_view) && $request->_view instanceof View) {
70
                return $request->_view;
71
            }
72
        }
73
74
        $view = $this->getViewObject();
75
76
        if ($request instanceof Request) {
0 ignored issues
show
introduced by
$request is always a sub-type of Nip\Http\Request.
Loading history...
77
            $request->_view = $view;
0 ignored issues
show
Bug introduced by
The property _view does not seem to exist on Nip\Request.
Loading history...
78
        }
79
80
        return $view;
81
    }
82
83
    /**
84
     * @return View
85
     */
86
    protected function getViewObject()
87
    {
88
        return new View();
89
    }
90
91
    /**
92
     * @param View $view
93
     *
94
     * @return View
95
     */
96
    public function populateView($view)
97
    {
98
        $this->populateViewPath($view);
99
100
        $view = $this->initViewVars($view);
101
102
        // @deprecated Rely on Response Payload Transformer to call this method
103
//        $view = $this->initViewContentBlocks($view);
104
105
        return $view;
106
    }
107
108
    /**
109
     * @param View $view
110
     */
111
    protected function populateViewPath($view)
112
    {
113
        return ControllerViewHydrator::populatePath($view, $this);
0 ignored issues
show
Bug introduced by
$this of type Nip\Controllers\Traits\HasViewTrait is incompatible with the type Nip\Controllers\Controller expected by parameter $controller of Nip\Controllers\View\Con...ydrator::populatePath(). ( Ignorable by Annotation )

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

113
        return ControllerViewHydrator::populatePath($view, /** @scrutinizer ignore-type */ $this);
Loading history...
114
    }
115
116
    /**
117
     * @param View $view
118
     *
119
     * @return View
120
     */
121
    protected function initViewVars($view)
122
    {
123
        return ControllerViewHydrator::initVars($view, $this);
0 ignored issues
show
Bug introduced by
$this of type Nip\Controllers\Traits\HasViewTrait is incompatible with the type Nip\Controllers\Controller|null expected by parameter $controller of Nip\Controllers\View\Con...iewHydrator::initVars(). ( Ignorable by Annotation )

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

123
        return ControllerViewHydrator::initVars($view, /** @scrutinizer ignore-type */ $this);
Loading history...
124
    }
125
126
    /**
127
     * @param View $view
128
     *
129
     * @return View
130
     */
131
    protected function initViewContentBlocks($view)
132
    {
133
        return ControllerViewHydrator::initContentBlocks($view, $this);
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    public function getLayoutPath()
140
    {
141
        return '/layouts/' . $this->getLayout();
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    public function getLayout()
148
    {
149
        return $this->layout;
150
    }
151
152
    /**
153
     * @param string $layout
154
     */
155
    public function setLayout($layout)
156
    {
157
        $this->layout = $layout;
158
    }
159
}
160