22
33namespace Sammyjo20 \Saloon \Http ;
44
5- use ReflectionClass ;
6- use Illuminate \Support \Str ;
75use Illuminate \Support \Collection ;
86use GuzzleHttp \Promise \PromiseInterface ;
97use Sammyjo20 \Saloon \Clients \MockClient ;
108use Sammyjo20 \Saloon \Traits \CollectsData ;
9+ use Sammyjo20 \Saloon \Helpers \RequestHelper ;
1110use Sammyjo20 \Saloon \Traits \CollectsConfig ;
1211use Sammyjo20 \Saloon \Traits \CollectsHeaders ;
1312use Sammyjo20 \Saloon \Traits \CollectsHandlers ;
1615use Sammyjo20 \Saloon \Traits \CollectsQueryParams ;
1716use Sammyjo20 \Saloon \Traits \CollectsInterceptors ;
1817use Sammyjo20 \Saloon \Traits \AuthenticatesRequests ;
18+ use Sammyjo20 \Saloon \Helpers \ProxyRequestNameHelper ;
1919use Sammyjo20 \Saloon \Exceptions \ClassNotFoundException ;
2020use Sammyjo20 \Saloon \Interfaces \SaloonConnectorInterface ;
2121use Sammyjo20 \Saloon \Exceptions \SaloonInvalidRequestException ;
@@ -76,23 +76,12 @@ public function boot(SaloonRequest $request): void
7676 * @param string $request
7777 * @param array $args
7878 * @return SaloonRequest
79- * @throws ClassNotFoundException
8079 * @throws SaloonInvalidRequestException
8180 * @throws \ReflectionException
8281 */
8382 protected function forwardCallToRequest (string $ request , array $ args = []): SaloonRequest
8483 {
85- if (! class_exists ($ request )) {
86- throw new ClassNotFoundException ($ request );
87- }
88-
89- $ isValidRequest = ReflectionHelper::isSubclassOf ($ request , SaloonRequest::class);
90-
91- if (! $ isValidRequest ) {
92- throw new SaloonInvalidRequestException ($ request );
93- }
94-
95- return (new $ request (...$ args ))->setConnector ($ this );
84+ return RequestHelper::callFromConnector ($ this , $ request , $ args );
9685 }
9786
9887 /**
@@ -111,19 +100,9 @@ public function getRegisteredRequests(): array
111100 return $ this ->registeredRequests ;
112101 }
113102
114- $ requests = (new Collection ($ this ->requests ))->mapWithKeys (function ($ value , $ key ) {
115- if (is_string ($ key )) {
116- return [$ key => $ value ];
117- }
103+ $ this ->registeredRequests = ProxyRequestNameHelper::generateNames ($ this ->requests );
118104
119- $ guessedKey = Str::camel ((new ReflectionClass ($ value ))->getShortName ());
120-
121- return [$ guessedKey => $ value ];
122- })->toArray ();
123-
124- $ this ->registeredRequests = $ requests ;
125-
126- return $ requests ;
105+ return $ this ->registeredRequests ;
127106 }
128107
129108 /**
@@ -181,21 +160,15 @@ public function sendAsync(SaloonRequest $request, MockClient $mockClient = null)
181160 *
182161 * @param $method
183162 * @param $arguments
184- * @return SaloonRequest
163+ * @return AnonymousRequestCollection| SaloonRequest
185164 * @throws ClassNotFoundException
186- * @throws SaloonInvalidRequestException
187165 * @throws SaloonConnectorMethodNotFoundException
166+ * @throws SaloonInvalidRequestException
188167 * @throws \ReflectionException
189168 */
190169 public function __call ($ method , $ arguments )
191170 {
192- if ($ this ->requestExists ($ method ) === false ) {
193- throw new SaloonConnectorMethodNotFoundException ($ method , $ this );
194- }
195-
196- $ requests = $ this ->getRegisteredRequests ();
197-
198- return $ this ->forwardCallToRequest ($ requests [$ method ], $ arguments );
171+ return $ this ->guessRequest ($ method , $ arguments );
199172 }
200173
201174 /**
@@ -211,14 +184,52 @@ public function __call($method, $arguments)
211184 */
212185 public static function __callStatic ($ method , $ arguments )
213186 {
214- $ connector = new static ;
187+ return (new static )->guessRequest ($ method , $ arguments );
188+ }
189+
190+ /**
191+ * Attempt to guess the next request.
192+ *
193+ * @param $method
194+ * @param $arguments
195+ * @return mixed
196+ * @throws ClassNotFoundException
197+ * @throws SaloonConnectorMethodNotFoundException
198+ * @throws SaloonInvalidRequestException
199+ * @throws \ReflectionException
200+ */
201+ protected function guessRequest ($ method , $ arguments ): mixed
202+ {
203+ if ($ this ->requestExists ($ method ) === false ) {
204+ throw new SaloonConnectorMethodNotFoundException ($ method , $ this );
205+ }
206+
207+ $ requests = $ this ->getRegisteredRequests ();
208+
209+ // Work out what it is. If it is an array, pass the array into AnonymousRequestCollection($requests)
210+ // If it is a request, just forward the call to the request.
211+
212+ $ resource = $ requests [$ method ];
213+
214+ // If the request is a type of array, then it must be an anonymous request collection.
215+
216+ if (is_array ($ resource )) {
217+ return new AnonymousRequestCollection ($ this , $ method , $ resource );
218+ }
219+
220+ // Otherwise, check if it is a RequestCollection. If it is, then
221+ // return that class - otherwise, just forward the request.
222+
223+ if (! class_exists ($ resource )) {
224+ throw new ClassNotFoundException ($ resource );
225+ }
215226
216- if ($ connector -> requestExists ( $ method ) === false ) {
217- throw new SaloonConnectorMethodNotFoundException ( $ method , $ connector );
227+ if (ReflectionHelper:: isSubclassOf ( $ resource , RequestCollection::class) ) {
228+ return new $ resource ( $ this );
218229 }
219230
220- $ requests = $ connector -> getRegisteredRequests ();
231+ // It's just a request, so forward to that.
221232
222- return $ connector ->forwardCallToRequest ($ requests [ $ method ] , $ arguments );
233+ return $ this ->forwardCallToRequest ($ resource , $ arguments );
223234 }
224235}
0 commit comments