Completed
Push — master ( 32191e...8fb922 )
by Nate
03:04 queued 11s
created

RouteAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 35
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 24 5
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/jwt/license
6
 * @link       https://www.flipboxfactory.com/jwt/organization/
7
 */
8
9
namespace flipbox\craft\jwt\actions;
10
11
use flipbox\craft\jwt\Jwt;
12
use yii\base\Action;
13
use Craft;
14
use yii\web\NotFoundHttpException;
15
use yii\web\Response;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class RouteAction extends Action
22
{
23
    /**
24
     * @param string $token
25
     * @return \craft\web\Response|int|\yii\console\Response|Response|null
26
     * @throws NotFoundHttpException
27
     * @throws \craft\errors\SiteNotFoundException
28
     * @throws \yii\base\InvalidRouteException
29
     * @throws \yii\console\Exception
30
     */
31
    public function run(string $token)
32
    {
33
        if (false === ($route = Jwt::getInstance()->getRoute()->claim($token))) {
34
            throw new NotFoundHttpException("Invalid token.");
35
        }
36
37
        $params = [];
38
        if (is_array($route)) {
39
            list($route, $params) = $route;
40
        }
41
42
        $result = Craft::$app->runAction($route, $params);
43
44
        if ($result instanceof Response) {
45
            return $result;
46
        }
47
48
        $response = Craft::$app->getResponse();
49
        if ($result !== null) {
50
            $response->data = $result;
51
        }
52
53
        return $response;
54
    }
55
}
56