Passed
Push — 0.7.0 ( e442b6...679d5f )
by Alexander
11:21 queued 12s
created

ControllerDispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2021 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
 
23
namespace Syscodes\Controller;
24
25
use Syscodes\Routing\Route;
26
use Syscodes\Contracts\Container\Container;
27
use Syscodes\Routing\Concerns\RouteDependencyResolver;
28
use Syscodes\Controller\Contracts\ControllerDispatcher as ControllerDispatcherContract;
29
30
/**
31
 * Dispatch requests when called a given controller and method.
32
 * 
33
 * @author Alexander Campo <[email protected]>
34
 */
35
class ControllerDispatcher implements ControllerDispatcherContract
36
{
37
    use RouteDependencyResolver;
0 ignored issues
show
Bug introduced by
The trait Syscodes\Routing\Concerns\RouteDependencyResolver requires the property $name which is not provided by Syscodes\Controller\ControllerDispatcher.
Loading history...
38
39
    /**
40
     * The container instance.
41
     * 
42
     * @var \Syscodes\Contracts\Container\Container  $container
43
     */
44
    protected $container;
45
46
    /**
47
     * Constructor. The ControllerDispatcher class instance.
48
     * 
49
     * @param  \Syscodes\Contracts\Container\Container  $container
50
     * 
51
     * @return void
52
     */
53
    public function __construct(Container $container)
54
    {
55
        $this->container = $container;
56
    }
57
58
    /**
59
     * Dispatch a request to a given controller and method.
60
     * 
61
     * @param  \Syscodes\Routing\Route  $route
62
     * @param  mixed  $controller
63
     * @param  string  $method
64
     * 
65
     * @return mixed
66
     */
67
    public function dispatch(Route $route, $controller, $method)
68
    {
69
        $parameters = $this->resolveObjectMethodDependencies(
70
            $route->parametersWithouNulls(), $controller, $method
71
        );
72
        
73
        if (method_exists($controller, 'callAction')) {
74
            return $controller->callAction($method, $parameters);
75
        }
76
        
77
        return $controller->{$method}(...array_values($parameters));
78
    }
79
80
    /**
81
     * Get the middleware for the controller instance.
82
     * 
83
     * @param  \Syscodes\Controller\Controller  $controller
84
     * @param  string  $method
85
     * 
86
     * @return array
87
     */
88
    public function getMiddleware($controller, $method)
89
    {
90
        if ( ! method_exists($controller, 'getMiddleware'))
91
        {
92
            return [];
93
        }
94
95
        $middleware = $controller->getMiddleware();
96
97
        return collect($middleware)->filter(function ($data) use ($method) {
98
            return static::methodExcludedByOptions($method, $data['options']);
0 ignored issues
show
Bug Best Practice introduced by
The method Syscodes\Controller\Cont...thodExcludedByOptions() is not static, but was called statically. ( Ignorable by Annotation )

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

98
            return static::/** @scrutinizer ignore-call */ methodExcludedByOptions($method, $data['options']);
Loading history...
99
        })->all();
100
    }
101
102
    /**
103
     * Determine if the given options exclude a particular method.
104
     * 
105
     * @param  string  $method
106
     * @param  array  $options
107
     * 
108
     * @return bool
109
     */
110
    protected function methodExcludedByOptions($method, array $options)
111
    {
112
        return (isset($options['only']) && ! in_array($method, (array) $options['only'])) ||
113
            ( ! empty($options['except']) && in_array($method, (array) $options['except']));
114
    }
115
}