Passed
Push — master ( 4f415b...b6b0b7 )
by Gabriel
03:01
created

ControllerViewHydrator::detectRequest()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.583

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 10
rs 9.6111
c 1
b 0
f 0
ccs 5
cts 7
cp 0.7143
cc 5
nc 5
nop 1
crap 5.583
1
<?php
2
3
namespace Nip\Controllers\View;
4
5
use Nip\Controllers\Controller;
6
use Nip\Http\Request;
7
8
/**
9
 * Class ControllerViewHydrator
10
 * @package Nip\Controllers\View
11
 */
12
class ControllerViewHydrator
13
{
14
15
    /**
16
     * @param $view
17
     * @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...
18
     * @return mixed
19
     */
20 1
    public static function initContentBlocks($view, $controller = null)
21
    {
22 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...
23 1
        $view->setBlock(
24 1
            'content',
25 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

25
            $controller->/** @scrutinizer ignore-call */ 
26
                         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...
26
        );
27
28 1
        return $view;
29
    }
30
31
    /**
32
     * @param $view
33
     * @param null|Controller $controller
34
     * @return mixed
35
     */
36 1
    public static function initVars($view, $controller = null)
37
    {
38 1
        $request = static::detectRequest($controller);
39 1
        if (method_exists($view, 'setRequest')) {
40 1
            if ($request instanceof Request) {
41
                $view->setRequest($request);
42
            }
43
        }
44
45 1
        $view->set('controller', $controller->getName());
46 1
        $view->set('action', $controller->getAction());
47
48 1
        return $view;
49
    }
50
51
    /**
52
     * @param $view
53
     * @param void|Controller $controller
54
     */
55 1
    public static function populatePath($view, $controller = null)
56
    {
57 1
        if (method_exists($controller, 'generateViewPath')) {
58 1
            $view->setBasePath($controller->generateViewPath());
0 ignored issues
show
Bug introduced by
The method generateViewPath() 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

58
            $view->setBasePath($controller->/** @scrutinizer ignore-call */ generateViewPath());
Loading history...
59 1
            return $view;
60
        }
61
62
        if (!defined('MODULES_PATH')) {
63
            return $view;
64
        }
65
        $request = static::detectRequest($controller);
66
        if (!($request instanceof Request)) {
67
            return $view;
68
        }
69
70
        $path = MODULES_PATH . $request->getModuleName() . '/views/';
0 ignored issues
show
Bug introduced by
The constant Nip\Controllers\View\MODULES_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
71
        if (!is_dir($path)) {
72
            return $view;
73
        }
74
        $view->setBasePath(MODULES_PATH . $request->getModuleName() . '/views/');
75
76
        return $view;
77
    }
78
79
    /**
80
     * @param null|Controller $controller
81
     */
82 1
    protected static function detectRequest($controller = null)
83
    {
84 1
        if ($controller instanceof Controller && $controller->hasRequest()) {
85
            return $controller->getRequest();
86
        }
87 1
        $request = function_exists('request')? request() : null;
88 1
        if ($request instanceof Request) {
89
            return $request;
90
        }
91 1
        return null;
92
    }
93
}