Completed
Push — master ( 46c7bb...4858ea )
by Arthur
02:48
created

Action   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 91.49%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 17
eloc 46
c 2
b 1
f 0
dl 0
loc 106
ccs 43
cts 47
cp 0.9149
rs 10

12 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 delegateTo() 0 3 1
A reset() 0 5 1
A execute() 0 3 1
A run() 0 16 2
A user() 0 3 1
A runningAs() 0 3 2
A make() 0 3 1
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 51
    public function run(array $attributes = [])
52
    {
53 51
        $this->resolveDefaults();
54 51
        $this->fill($attributes);
55 51
        $this->resolveIncludes();
56 51
        $this->resolveBeforeHook();
57 51
        $this->resolveAuthorization();
58 49
        $this->resolveValidation();
59
        try {
60 45
            $value = $this->resolveAndCall($this, 'handle');
61 6
        } catch (Throwable $exception) {
62 6
            $this->failHook($exception);
63
        }
64
65
        return tap($value, function ($value) {
66 39
            $this->successHook($value);
67 39
        });
68
    }
69
70 51
    public function resolveBeforeHook()
71
    {
72 51
        $method = 'as'.Str::studly($this->runningAs);
73
74 51
        if (method_exists($this, $method)) {
75 1
            return $this->resolveAndCall($this, $method);
76
        }
77 50
    }
78
79 29
    public function runningAs($matches)
80
    {
81 29
        return in_array($this->runningAs, is_array($matches) ? $matches : func_get_args());
82
    }
83
84 7
    public function actingAs($user)
85
    {
86 7
        $this->actingAs = $user;
87
88 7
        return $this;
89
    }
90
91 11
    public function user()
92
    {
93 11
        return $this->actingAs ?? Auth::user();
94
    }
95
96 13
    public function reset($user = null)
97
    {
98 13
        $this->actingAs = $user;
99 13
        $this->attributes = [];
100 13
        $this->validator = null;
101 13
    }
102
103 6
    public function delegateTo($actionClass)
104
    {
105 6
        return $actionClass::createFrom($this)->runAs($this);
106
    }
107
108
    public static function make(array $attributes = [])
109
    {
110
        return new static($attributes);
111
    }
112
113
    public static function execute(array $attributes = [])
114
    {
115
        return self::make($attributes)->run();
116
    }
117
}
118