Passed
Push — master ( 0362f6...c4bc93 )
by Arthur
04:24 queued 11s
created

Action   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 86.27%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 19
eloc 48
c 3
b 1
f 0
dl 0
loc 114
ccs 44
cts 51
cp 0.8627
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A createFrom() 0 3 1
A __construct() 0 6 2
A runAs() 0 9 2
A actingAs() 0 5 1
A resolveBeforeHook() 0 6 2
A runUnauthorized() 0 3 1
A delegateTo() 0 3 1
A reset() 0 5 1
A execute() 0 3 1
A run() 0 19 3
A user() 0 3 1
A make() 0 3 1
A runningAs() 0 3 2
1
<?php
2
3
namespace Larapie\Actions;
4
5
use Illuminate\Routing\Controller;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Str;
8
use Throwable;
9
10
abstract class Action extends Controller
11
{
12
    use Concerns\HasAttributes;
13
    use Concerns\ResolvesMethodDependencies;
0 ignored issues
show
Bug introduced by
The trait Larapie\Actions\Concerns...olvesMethodDependencies requires the property $name which is not provided by Larapie\Actions\Action.
Loading history...
14
    use Concerns\ResolvesAuthorization;
15
    use Concerns\ResolvesValidation;
16
    use Concerns\ResolveIncludes;
17
    use Concerns\ResolveDefaults;
18
    use Concerns\ResolveCasting;
19
    use Concerns\RunsAsController;
20
    use Concerns\SuccessHook;
21
    use Concerns\FailHook;
22
23
    protected $actingAs;
24
    protected $runningAs = 'object';
25
26 66
    public function __construct(array $attributes = [])
27
    {
28 66
        $this->fill($attributes);
29
30 66
        if (method_exists($this, 'register')) {
31 27
            $this->register();
0 ignored issues
show
Bug introduced by
The method register() does not exist on Larapie\Actions\Action. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

31
            $this->/** @scrutinizer ignore-call */ 
32
                   register();
Loading history...
32
        }
33 66
    }
34
35 6
    public static function createFrom(self $action)
36
    {
37 6
        return (new static())->fill($action->all());
38
    }
39
40 6
    public function runAs(self $action)
41
    {
42 6
        if ($action->runningAs('controller')) {
43 1
            return $this->runAsController($action->getRequest());
44
        }
45
46 5
        $this->actingAs($action->user());
47
48 5
        return $this->run();
49
    }
50
51
    public function runUnauthorized(array $attributes = [])
52
    {
53
        $this->run($attributes, false);
54
    }
55
56 51
    public function run(array $attributes = [], $authorization = true)
57
    {
58 51
        $this->resolveDefaults();
59 51
        $this->fill($attributes);
60 51
        $this->resolveIncludes();
61 51
        $this->resolveBeforeHook();
62
63 51
        if ($authorization)
64 51
            $this->resolveAuthorization();
65
66 49
        $this->resolveValidation();
67
        try {
68 45
            $value = $this->resolveAndCall($this, 'handle');
69 6
        } catch (Throwable $exception) {
70 6
            $this->failHook($exception);
71
        }
72
73
        return tap($value, function ($value) {
74 39
            $this->successHook($value);
75 39
        });
76
    }
77
78 51
    public function resolveBeforeHook()
79
    {
80 51
        $method = 'as' . Str::studly($this->runningAs);
81
82 51
        if (method_exists($this, $method)) {
83 1
            return $this->resolveAndCall($this, $method);
84
        }
85 50
    }
86
87 29
    public function runningAs($matches)
88
    {
89 29
        return in_array($this->runningAs, is_array($matches) ? $matches : func_get_args());
90
    }
91
92 7
    public function actingAs($user)
93
    {
94 7
        $this->actingAs = $user;
95
96 7
        return $this;
97
    }
98
99 11
    public function user()
100
    {
101 11
        return $this->actingAs ?? Auth::user();
102
    }
103
104 13
    public function reset($user = null)
105
    {
106 13
        $this->actingAs = $user;
107 13
        $this->attributes = [];
108 13
        $this->validator = null;
109 13
    }
110
111 6
    public function delegateTo($actionClass)
112
    {
113 6
        return $actionClass::createFrom($this)->runAs($this);
114
    }
115
116
    public static function make(array $attributes = [])
117
    {
118
        return new static($attributes);
119
    }
120
121
    public static function execute(array $attributes = [], $authorized = true)
122
    {
123
        return self::make()->run($attributes, $authorized);
124
    }
125
}
126