2222use Authorization \Policy \ResolverInterface ;
2323use Authorization \Policy \Result ;
2424use Authorization \Policy \ResultInterface ;
25+ use Closure ;
2526
2627class AuthorizationService implements AuthorizationServiceInterface
2728{
@@ -84,33 +85,22 @@ protected function performCheck(?IdentityInterface $user, string $action, mixed
8485 $ result = $ policy ->before ($ user , $ resource , $ action );
8586
8687 if ($ result !== null ) {
87- return $ this -> resultTypeCheck ( $ result) ;
88+ return $ result ;
8889 }
8990 }
9091
9192 $ handler = $ this ->getCanHandler ($ policy , $ action );
9293 $ result = $ handler ($ user , $ resource );
9394
94- return $ this ->resultTypeCheck ($ result );
95- }
96-
97- /**
98- * Check result type.
99- *
100- * @param mixed $result Result from policy class instance.
101- * @return \Authorization\Policy\ResultInterface|bool
102- * @throws \Authorization\Exception\Exception If $result argument is not a boolean or ResultInterface instance.
103- */
104- protected function resultTypeCheck (mixed $ result ): ResultInterface |bool
105- {
106- if (is_bool ($ result ) || $ result instanceof ResultInterface) {
107- return $ result ;
108- }
95+ assert (
96+ is_bool ($ result ) || $ result instanceof ResultInterface,
97+ new Exception (sprintf (
98+ 'Authorization check method must return `%s` or `bool`. ' ,
99+ ResultInterface::class
100+ ))
101+ );
109102
110- throw new Exception (sprintf (
111- 'Pre-authorization check must return `%s`, `bool` or `null`. ' ,
112- ResultInterface::class
113- ));
103+ return $ result ;
114104 }
115105
116106 /**
@@ -130,37 +120,39 @@ public function applyScope(?IdentityInterface $user, string $action, $resource):
130120 *
131121 * @param mixed $policy Policy object.
132122 * @param string $action Action name.
133- * @return callable
123+ * @return \Closure
134124 * @throws \Authorization\Policy\Exception\MissingMethodException
135125 */
136- protected function getCanHandler (mixed $ policy , string $ action ): callable
126+ protected function getCanHandler (mixed $ policy , string $ action ): Closure
137127 {
138128 $ method = 'can ' . ucfirst ($ action );
139129
140- if (!method_exists ($ policy , $ method ) && !method_exists ($ policy , '__call ' )) {
141- throw new MissingMethodException ([$ method , $ action , get_class ($ policy )]);
142- }
130+ assert (
131+ method_exists ($ policy , $ method ) || method_exists ($ policy , '__call ' ),
132+ new MissingMethodException ([$ method , $ action , get_class ($ policy )])
133+ );
143134
144- return [$ policy , $ method ];
135+ return [$ policy , $ method ](...) ;
145136 }
146137
147138 /**
148139 * Returns a policy scope action handler.
149140 *
150141 * @param mixed $policy Policy object.
151142 * @param string $action Action name.
152- * @return callable
143+ * @return \Closure
153144 * @throws \Authorization\Policy\Exception\MissingMethodException
154145 */
155- protected function getScopeHandler (mixed $ policy , string $ action ): callable
146+ protected function getScopeHandler (mixed $ policy , string $ action ): Closure
156147 {
157148 $ method = 'scope ' . ucfirst ($ action );
158149
159- if (!method_exists ($ policy , $ method )) {
160- throw new MissingMethodException ([$ method , $ action , get_class ($ policy )]);
161- }
150+ assert (
151+ method_exists ($ policy , $ method ) || method_exists ($ policy , '__call ' ),
152+ new MissingMethodException ([$ method , $ action , get_class ($ policy )])
153+ );
162154
163- return [$ policy , $ method ];
155+ return [$ policy , $ method ](...) ;
164156 }
165157
166158 /**
0 commit comments