ControllerViewHydrator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 19.23%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 13
eloc 25
c 3
b 0
f 2
dl 0
loc 74
ccs 5
cts 26
cp 0.1923
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initContentBlocks() 0 8 1
A initVars() 0 13 3
A populatePath() 0 13 3
A detectRequest() 0 12 6
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 = $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)) {
0 ignored issues
show
Bug introduced by
$path of type boolean is incompatible with the type string expected by parameter $filename of is_dir(). ( Ignorable by Annotation )

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

60
        if (is_dir(/** @scrutinizer ignore-type */ $path)) {
Loading history...
61 1
            $view->setBasePath($path);
62
        }
63
64 1
        if (method_exists($controller, 'registerViewPaths'))
65
        {
66
            $controller->registerViewPaths($view);
0 ignored issues
show
Bug introduced by
The method registerViewPaths() does not exist on Nip\Controllers\Controller. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

66
            $controller->/** @scrutinizer ignore-call */ 
67
                         registerViewPaths($view);
Loading history...
67
        }
68
69
        return $view;
70
    }
71
72
    /**
73
     * @param null|Controller $controller
74
     * @return array|Request|\Nip\Request|string|null
75
     */
76
    protected static function detectRequest($controller = null)
77
    {
78
        if ($controller instanceof Controller && $controller->hasRequest()) {
79
            return $controller->getRequest();
80
        }
81
        if (function_exists('request') && Container::container()->has('request')) {
82
            $request = request();
83
            if ($request instanceof Request) {
84
                return $request;
85
            }
86
        }
87
        return null;
88
    }
89
}
90