Passed
Push — 0.7.0 ( bddda8...da5a73 )
by Alexander
11:04 queued 11s
created

RouteAction::callInvokable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
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\Routing;
24
25
use LogicException;
26
use Syscodes\Support\Str;
27
use Syscodes\Collections\Arr;
28
use UnexpectedValueException;
29
30
/**
31
 * Solve the actions obtained from a route.
32
 * 
33
 * @author Alexander Campo <[email protected]>
34
 */
35
class RouteAction
36
{
37
    /**
38
     * Parse the given action into an array.
39
     * 
40
     * @param  string|array  $uri
41
     * @param  mixed  $action
42
     * 
43
     * @return array
44
     */
45
    public static function parse($uri, $action)
46
    {
47
        if (is_null($action)) {
48
            return static::usesAction($uri);
0 ignored issues
show
Bug introduced by
It seems like $uri can also be of type array; however, parameter $uri of Syscodes\Routing\RouteAction::usesAction() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

48
            return static::usesAction(/** @scrutinizer ignore-type */ $uri);
Loading history...
49
        }
50
51
        if (is_callable($action, true)) {
52
            return ! is_array($action) ? ['uses' => $action] : [
53
                    'uses' => $action[0].'@'.$action[1],
54
                    'controller' => $action[0].'@'.$action[1],
55
            ];
56
        } elseif ( ! isset($action['uses'])) {
57
            $action['uses'] = static::findClosureAction($action);
58
        }
59
        
60
        if (is_string($action['uses']) && ! Str::contains($action['uses'], '@')) {
61
            $action['uses'] = static::callInvokable($action['uses']);
62
        }
63
        
64
        return $action;
65
    }
66
    
67
    /**
68
     * Get an action for a route that has no action.
69
     * 
70
     * @param  string  $uri
71
     *
72
     * @return array
73
     * 
74
     * @throws \LogicException
75
     */
76
    protected static function usesAction($uri)
77
    {
78
        return ['uses' => function () use ($uri) {
79
            throw new LogicException(__('route.hasNoAction', ['uri' => $uri]));
80
        }];
81
    }
82
    
83
    /**
84
     * Find the callable in an action array.
85
     * 
86
     * @param  array  $action
87
     * 
88
     * @return \Closure
89
     */
90
    protected static function findClosureAction(array $action)
91
    {
92
        return Arr::first($action, function ($value, $key) {
93
            return is_callable($value) && is_numeric($key);
94
        });
95
    }
96
    
97
    /**
98
     * Call an action for an invokable controller.
99
     * @param  string  $action
100
     * 
101
     * @return string
102
     * 
103
     * @throws \UnexpectedValueException
104
     */
105
    protected static function callInvokable($action)
106
    {
107
        if ( ! method_exists($action, '__invoke')) {
108
            throw new UnexpectedValueException("Invalid route action: [{$action}].");
109
        }
110
        
111
        return $action.'@__invoke';
0 ignored issues
show
Bug introduced by
Are you sure $action of type object can be used in concatenation? ( Ignorable by Annotation )

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

111
        return /** @scrutinizer ignore-type */ $action.'@__invoke';
Loading history...
112
    }
113
}