Passed
Push — master ( b6b0b7...148695 )
by Gabriel
03:18
created

ControllerViewHydrator::populatePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 4
c 2
b 0
f 1
dl 0
loc 8
rs 10
ccs 5
cts 5
cp 1
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Nip\Controllers\View;
4
5
use Nip\Controllers\Controller;
6
use Nip\Http\Request;
7
use Nip\Utility\Container;
8
9
/**
10
 * Class ControllerViewHydrator
11
 * @package Nip\Controllers\View
12
 */
13
class ControllerViewHydrator
14
{
15
16
    /**
17
     * @param $view
18
     * @param null $controller
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $controller is correct as it would always require null to be passed?
Loading history...
19
     * @return mixed
20
     */
21 1
    public static function initContentBlocks($view, $controller = null)
22
    {
23 1
        $request = static::detectRequest($controller);
0 ignored issues
show
Unused Code introduced by
The assignment to $request is dead and can be removed.
Loading history...
24 1
        $view->setBlock(
25 1
            'content',
26 1
            $controller->getName() . '/' . $controller->getAction()
0 ignored issues
show
Bug introduced by
The method getName() does not exist on null. ( Ignorable by Annotation )

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

26
            $controller->/** @scrutinizer ignore-call */ 
27
                         getName() . '/' . $controller->getAction()

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...
27
        );
28
29 1
        return $view;
30
    }
31
32
    /**
33
     * @param $view
34
     * @param null|Controller $controller
35
     * @return mixed
36
     */
37 1
    public static function initVars($view, $controller = null)
38
    {
39 1
        $request = static::detectRequest($controller);
40 1
        if (method_exists($view, 'setRequest')) {
41 1
            if ($request instanceof Request) {
42
                $view->setRequest($request);
43
            }
44
        }
45
46 1
        $view->set('controller', $controller->getName());
47 1
        $view->set('action', $controller->getAction());
48
49 1
        return $view;
50
    }
51
52
    /**
53
     * @param $view
54
     * @param void|Controller $controller
55
     * @return mixed
56
     */
57 2
    public static function populatePath($view, $controller = null)
58
    {
59 2
        $path = ViewPathDetector::for($controller);
60 2
        if (is_dir($path)) {
61 2
            $view->setBasePath($path);
62
        }
63
64 2
        return $view;
65
    }
66
67
    /**
68
     * @param null|Controller $controller
69
     * @return array|Request|\Nip\Request|string|null
70
     */
71 1
    protected static function detectRequest($controller = null)
72
    {
73 1
        if ($controller instanceof Controller && $controller->hasRequest()) {
74
            return $controller->getRequest();
75
        }
76 1
        if (function_exists('request') && Container::container()->has('request')) {
77
            $request = request();
78
            if ($request instanceof Request) {
79
                return $request;
80
            }
81
        }
82 1
        return null;
83
    }
84
}
85