Passed
Push — master ( 2915de...908b7b )
by Arthur
03:36
created

Action::bypassAuthorization()   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 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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
    protected $runAuthorized = true;
27
28 68
    public function __construct(array $attributes = [])
29
    {
30 68
        $this->fill($attributes);
31
32 68
        if (method_exists($this, 'register')) {
33 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

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