HasViewTrait::getView()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 7
ccs 0
cts 2
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nip\Controllers\Traits;
6
7
use Nip\Controllers\View\ControllerViewHydrator;
8
use Nip\Http\Request;
9
use Nip\View;
10
11
/**
12
 * Trait HasViewTrait
13
 * @package Nip\Controllers\Traits
14
 */
15
trait HasViewTrait
16
{
17
    use AbstractControllerTrait;
18
19
    /**
20
     * @var View
21
     */
22
    protected $view;
23
24
    /**
25
     * @var string
26
     */
27
    protected $layout = 'default';
28
29
    /**
30
     * @param bool $return
31
     * @return bool|string|null
32
     */
33
    public function loadView($return = false)
34
    {
35
        $view = $this->getView();
36
        $this->populateView($view);
37
        $content = $view->load($this->getLayoutPath());
38
        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...
39
            return $content;
40
        }
41
        echo $content;
42
        return null;
43
    }
44
45
    /**
46
     * @return View
47
     */
48
    public function getView()
49
    {
50
        if (!$this->view) {
51
            $this->view = $this->initView();
52
        }
53
54
        return $this->view;
55
    }
56
57
    /**
58
     * @param View $view
59
     */
60
    public function setView($view)
61
    {
62
        $this->view = $view;
63
    }
64
65
    /**
66
     * @return View
67
     */
68
    protected function initView()
69
    {
70
        $request = $this->getRequest();
71
        if ($request instanceof Request) {
0 ignored issues
show
introduced by
$request is always a sub-type of Nip\Http\Request.
Loading history...
72
            if (isset($request->_view) && $request->_view instanceof View) {
73
                return $request->_view;
74
            }
75
        }
76
77
        $view = $this->getViewObject();
78
79
        if ($request instanceof Request) {
0 ignored issues
show
introduced by
$request is always a sub-type of Nip\Http\Request.
Loading history...
80
            $request->_view = $view;
0 ignored issues
show
Bug introduced by
The property _view does not seem to exist on Nip\Request.
Loading history...
81
        }
82
83
        return $view;
84
    }
85
86
    /**
87
     * @return View
88
     */
89
    protected function getViewObject()
90
    {
91
        return new View();
92
    }
93
94
    /**
95
     * @param View $view
96
     *
97
     * @return View
98
     */
99
    public function populateView($view)
100
    {
101
        $this->populateViewPath($view);
0 ignored issues
show
Deprecated Code introduced by
The function Nip\Controllers\Traits\H...ait::populateViewPath() has been deprecated: Rely on Response Payload Transformer to call this method. Use RegisterViewPaths if necessary ( Ignorable by Annotation )

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

101
        /** @scrutinizer ignore-deprecated */ $this->populateViewPath($view);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
102
103
        $view = $this->initViewVars($view);
104
105
        // @deprecated Rely on Response Payload Transformer to call this method
106
//        $view = $this->initViewContentBlocks($view);
107
108
        return $view;
109
    }
110
111
    public function registerViewPaths(View\View $view): void
0 ignored issues
show
Unused Code introduced by
The parameter $view is not used and could be removed. ( Ignorable by Annotation )

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

111
    public function registerViewPaths(/** @scrutinizer ignore-unused */ View\View $view): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
    }
114
115
    /**
116
     * @deprecated Rely on Response Payload Transformer to call this method. Use RegisterViewPaths if necessary
117
     */
118
    protected function populateViewPath($view)
119
    {
120
        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

120
        return ControllerViewHydrator::populatePath($view, /** @scrutinizer ignore-type */ $this);
Loading history...
121
    }
122
123
    /**
124
     * @param View $view
125
     *
126
     * @return View
127
     */
128
    protected function initViewVars($view)
129
    {
130
        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

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