Completed
Push — master ( f89e11...f7925b )
by Arthur
03:41 queued 10s
created

Action   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 17
eloc 47
c 2
b 1
f 0
dl 0
loc 107
ccs 44
cts 48
cp 0.9167
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 17 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 63
    public function __construct(array $attributes = [])
27
    {
28 63
        $this->fill($attributes);
29
30 63
        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 63
    }
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 48
    public function run(array $attributes = [])
52
    {
53 48
        $this->resolveDefaults();
54 48
        $this->fill($attributes);
55 48
        $this->resolveIncludes();
56 48
        $this->resolveBeforeHook();
57 48
        $this->resolveAuthorization();
58 46
        $this->resolveValidation();
59 42
        $this->resolveCasting();
60
        try {
61 42
            $value = $this->resolveAndCall($this, 'handle');
62 6
        } catch (Throwable $exception) {
63 6
            $this->failHook($exception);
64
        }
65
66
        return tap($value, function ($value) {
67 36
            $this->successHook($value);
68 36
        });
69
    }
70
71 48
    public function resolveBeforeHook()
72
    {
73 48
        $method = 'as'.Str::studly($this->runningAs);
74
75 48
        if (method_exists($this, $method)) {
76 1
            return $this->resolveAndCall($this, $method);
77
        }
78 47
    }
79
80 29
    public function runningAs($matches)
81
    {
82 29
        return in_array($this->runningAs, is_array($matches) ? $matches : func_get_args());
83
    }
84
85 7
    public function actingAs($user)
86
    {
87 7
        $this->actingAs = $user;
88
89 7
        return $this;
90
    }
91
92 11
    public function user()
93
    {
94 11
        return $this->actingAs ?? Auth::user();
95
    }
96
97 13
    public function reset($user = null)
98
    {
99 13
        $this->actingAs = $user;
100 13
        $this->attributes = [];
101 13
        $this->validator = null;
102 13
    }
103
104 6
    public function delegateTo($actionClass)
105
    {
106 6
        return $actionClass::createFrom($this)->runAs($this);
107
    }
108
109
    public static function make(array $attributes = [])
110
    {
111
        return new static($attributes);
112
    }
113
114
    public static function execute(array $attributes = [])
115
    {
116
        return self::make($attributes)->run();
117
    }
118
}
119