22
33namespace Kirschbaum \Actions ;
44
5+ use Exception ;
56use Illuminate \Support \Traits \Macroable ;
67use Kirschbaum \Actions \Contracts \Actionable ;
78use Kirschbaum \Actions \Exceptions \ActionableInterfaceNotFoundException ;
@@ -13,6 +14,8 @@ class Action
1314
1415 /**
1516 * Arguments to pass into the action's constructor.
17+ *
18+ * @var array<int|string, mixed>
1619 */
1720 protected array $ arguments ;
1821
@@ -21,7 +24,7 @@ class Action
2124 *
2225 * @param mixed ...$arguments
2326 *
24- * @return mixed
27+ * @return mixed|void
2528 *
2629 * @throws Throwable
2730 */
@@ -35,6 +38,9 @@ public function act(string $action, ...$arguments)
3538 /**
3639 * Initiate the given action if the given condition is true.
3740 *
41+ * @template TValue
42+ *
43+ * @param TValue $condition
3844 * @param mixed ...$arguments
3945 *
4046 * @return mixed|void
@@ -53,6 +59,9 @@ public function actWhen($condition, string $action, ...$arguments)
5359 /**
5460 * Initiate the action if the given condition is false.
5561 *
62+ * @template TValue
63+ *
64+ * @param TValue $condition
5665 * @param mixed ...$arguments
5766 *
5867 * @return mixed|void
@@ -80,7 +89,11 @@ protected function handle(string $action)
8089 {
8190 $ action = new $ action (...$ this ->arguments );
8291
83- $ this ->checkActionForInterface ($ action );
92+ throw_unless (
93+ $ action instanceof Actionable,
94+ ActionableInterfaceNotFoundException::class
95+ );
96+
8497 $ this ->raiseBeforeActionEvent ($ action );
8598
8699 try {
@@ -103,20 +116,7 @@ protected function actionHasFailedMethod(Actionable $action): bool
103116 }
104117
105118 /**
106- * Determine if the action has the proper interface.
107- *
108- * @throws Throwable
109- */
110- protected function checkActionForInterface ($ action ): void
111- {
112- throw_unless (
113- $ action instanceof Actionable,
114- ActionableInterfaceNotFoundException::class
115- );
116- }
117-
118- /**
119- * Dispatch appropriate action event.
119+ * Dispatch the appropriate action event.
120120 */
121121 protected function dispatchEvent (string $ event , Actionable $ action ): void
122122 {
@@ -140,19 +140,28 @@ protected function eventExists(Actionable $action, string $event): bool
140140 /**
141141 * Fire failure event and/or call failed action method if they exist.
142142 *
143- *
144- * @return mixed
143+ * @return mixed|void
145144 *
146145 * @throws Throwable
147146 */
148147 protected function handleFailure (Actionable $ action , Throwable $ exception )
149148 {
150149 if ($ this ->actionHasFailedMethod ($ action )) {
151- return $ action ->failed ($ exception );
150+ /**
151+ * @var callable $callback
152+ */
153+ $ callback = [$ action , 'failed ' ];
154+
155+ return call_user_func ($ callback , $ exception );
152156 }
153157
154158 if ($ this ->hasCustomException ($ action )) {
155- $ exception = $ action ->exception ;
159+ $ properties = get_object_vars ($ action );
160+
161+ /**
162+ * @var Exception $exception
163+ */
164+ $ exception = $ properties ['exception ' ];
156165
157166 throw new $ exception ();
158167 }
@@ -161,7 +170,7 @@ protected function handleFailure(Actionable $action, Throwable $exception)
161170 }
162171
163172 /**
164- * Check if action has a custom exception.
173+ * Check if the action has a custom exception.
165174 */
166175 protected function hasCustomException (Actionable $ action ): bool
167176 {
@@ -170,15 +179,15 @@ protected function hasCustomException(Actionable $action): bool
170179 }
171180
172181 /**
173- * Raise the before action event.
182+ * Raise the " before" action event.
174183 */
175184 protected function raiseBeforeActionEvent (Actionable $ action ): void
176185 {
177186 $ this ->dispatchEvent ('before ' , $ action );
178187 }
179188
180189 /**
181- * Raise the after action event.
190+ * Raise the " after" action event.
182191 */
183192 protected function raiseAfterActionEvent (Actionable $ action ): void
184193 {
0 commit comments