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
![]() |
|||||||
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
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
![]() |
|||||||
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 | 41 | 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 |