Completed
Push — master ( 012500...4754a2 )
by Arthur
03:17 queued 11s
created

Action::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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