Completed
Push — master ( d2d39f...01dd42 )
by Charles
02:06
created

Action::run()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
rs 8.5806
cc 4
eloc 13
nc 6
nop 1
1
<?php
2
3
namespace yrc\rest;
4
5
use yii\web\HttpException;
6
use yii\helpers\ArrayHelper;
7
use Yii;
8
use ReflectionProperty;
9
use ReflectionClass;
10
11
abstract class Action extends \yii\base\Action
12
{
13
    /**
14
     * Action runner
15
     *
16
     * @param varadic $args
17
     * @return mixed
18
     */
19
    public function run(array $args = [])
20
    {
21
        $method = Yii::$app->request->method;
22
        $method = strtolower($method);
23
24
        $reflect = new ReflectionClass(get_called_class());
25
        $props   = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
26
27
        foreach ($props as $prop) {
28
            $name = $prop->name;
29
            if (in_array($name, ['id', 'controller'])) {
30
                break;
31
            }
32
33
            $args['class'][$name] = $this->$name;
34
        }
35
        
36
        // Make sure the method exists before trying to call it
37
        if (method_exists(get_called_class(), $method)) {
38
            return static::$method($args);
39
        }
40
41
        // Return a 405 if the method isn't implemented
42
        // When coupled with RestController, this should _never_ get called
43
        // But this is the correct response if for some reason it isn't
44
        throw new HttpException(405);
45
    }
46
47
    public static function options($params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    {
49
        Yii::$app->response->statusCode = 204;
50
        return;
51
    }
52
}