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

ControllerViewHydrator::initContentBlocks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
ccs 0
cts 5
cp 0
cc 1
nc 1
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
use Nip\View\View;
9
10
/**
11
 * Class ControllerViewHydrator
12
 * @package Nip\Controllers\View
13
 */
14
class ControllerViewHydrator
15
{
16
17
    /**
18
     * @param View $view
19
     * @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...
20
     * @return mixed
21
     */
22
    public static function initContentBlocks($view, $controller = null)
23
    {
24
        $action = inflector()->underscore($controller->getAction());
0 ignored issues
show
Bug introduced by
The method getAction() 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

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