DispatcherAwareTrait::call()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.288

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 9
c 4
b 0
f 1
dl 0
loc 18
ccs 8
cts 10
cp 0.8
rs 9.2222
cc 6
nc 6
nop 0
crap 6.288
1
<?php
2
3
namespace Nip\Controllers\Traits;
4
5
use Nip\Container\Container;
6
use Nip\Controllers\Controller;
7
use Nip\Dispatcher\Dispatcher;
8
use Nip\Request;
9
10
/**
11
 * Trait DispatcherAwareTrait
12
 * @package Nip\Controllers\Traits
13
 */
14
trait DispatcherAwareTrait
15
{
16
    /**
17
     * @return mixed
18
     * @throws \Exception
19
     */
20 2
    public function call()
21
    {
22 2
        $arguments = func_get_args();
23 2
        if (count($arguments) >= 3) {
24
            return $this->callMCA(...$arguments);
0 ignored issues
show
Bug introduced by
$arguments is expanded, but the parameter $action of Nip\Controllers\Traits\D...erAwareTrait::callMCA() does not expect variable arguments. ( Ignorable by Annotation )

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

24
            return $this->callMCA(/** @scrutinizer ignore-type */ ...$arguments);
Loading history...
25
        }
26
27 2
        if (count($arguments) == 2 && is_array($arguments[1])) {
28 1
            if (is_string($arguments[0])) {
29 1
                return $this->{$arguments[0]}(...$arguments[1]);
30
            }
31
        }
32
33 1
        if (count($arguments) == 1) {
34 1
            return $this->{$arguments[0]}();
35
        }
36
37
        throw new \Exception("Controller call method invoked with invalid parameters");
38
    }
39
40
    /**
41
     * @param bool $action
42
     * @param bool $controller
43
     * @param bool $module
44
     * @param array $params
45
     * @return mixed
46
     * @throws \Exception
47
     */
48
    protected function callMCA($action = false, $controller = false, $module = false, $params = [])
49
    {
50
        /** @var Request $newRequest */
51
        $newRequest = $this->getRequest()->duplicateWithParams($action, $controller, $module, $params);
0 ignored issues
show
Bug introduced by
It seems like getRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

51
        $newRequest = $this->/** @scrutinizer ignore-call */ getRequest()->duplicateWithParams($action, $controller, $module, $params);
Loading history...
52
53
        /** @noinspection PhpUnhandledExceptionInspection */
54
        return $this->getDispatcher()->callFromRequest($newRequest, $params);
55
    }
56
57
    /**
58
     * @param bool $action
59
     * @param bool $controller
60
     * @param bool $module
61
     * @param array $params
62
     * @throws \Nip\Dispatcher\Exceptions\ForwardException
63
     */
64
    protected function forward($action = false, $controller = false, $module = false, $params = [])
65
    {
66
        $this->getDispatcher()->forward($action, $controller, $module, $params);
67
    }
68
69
    /**
70
     * @return Dispatcher
71
     */
72
    protected function getDispatcher()
73
    {
74
        if (function_exists('app')) {
75
            return app('dispatcher');
76
        }
77
78
        return Container::getInstance()->get('dispatcher');
79
    }
80
}
81