22
33namespace Sammyjo20 \Saloon \Http ;
44
5- use Illuminate \Support \Collection ;
65use GuzzleHttp \Promise \PromiseInterface ;
76use Sammyjo20 \Saloon \Clients \MockClient ;
87use Sammyjo20 \Saloon \Traits \CollectsData ;
98use Sammyjo20 \Saloon \Traits \MocksRequests ;
10- use Sammyjo20 \Saloon \Helpers \RequestHelper ;
119use Sammyjo20 \Saloon \Traits \CollectsConfig ;
1210use Sammyjo20 \Saloon \Traits \CollectsHeaders ;
11+ use Sammyjo20 \Saloon \Traits \GuessesRequests ;
1312use Sammyjo20 \Saloon \Traits \CollectsHandlers ;
14- use Sammyjo20 \Saloon \Helpers \ReflectionHelper ;
1513use Sammyjo20 \Saloon \Traits \HasCustomResponses ;
1614use Sammyjo20 \Saloon \Traits \CollectsQueryParams ;
1715use Sammyjo20 \Saloon \Traits \CollectsInterceptors ;
1816use Sammyjo20 \Saloon \Traits \AuthenticatesRequests ;
19- use Sammyjo20 \Saloon \Helpers \ProxyRequestNameHelper ;
2017use Sammyjo20 \Saloon \Exceptions \ClassNotFoundException ;
2118use Sammyjo20 \Saloon \Interfaces \SaloonConnectorInterface ;
2219use Sammyjo20 \Saloon \Exceptions \SaloonInvalidRequestException ;
2320use Sammyjo20 \Saloon \Exceptions \SaloonConnectorMethodNotFoundException ;
2421
2522abstract class SaloonConnector implements SaloonConnectorInterface
2623{
27- use CollectsHeaders,
28- CollectsData,
29- CollectsQueryParams,
30- CollectsConfig,
31- CollectsHandlers,
32- CollectsInterceptors,
33- AuthenticatesRequests,
34- HasCustomResponses,
35- MocksRequests;
24+ use CollectsHeaders;
25+ use CollectsData;
26+ use CollectsQueryParams;
27+ use CollectsConfig;
28+ use CollectsHandlers;
29+ use CollectsInterceptors;
30+ use AuthenticatesRequests;
31+ use HasCustomResponses;
32+ use MocksRequests;
33+ use GuessesRequests;
3634
3735 /**
3836 * Register Saloon requests that will become methods on the connector.
@@ -42,24 +40,6 @@ abstract class SaloonConnector implements SaloonConnectorInterface
4240 */
4341 protected array $ requests = [];
4442
45- /**
46- * Requests that have already been registered. Used as a cache for performance.
47- *
48- * @var array|null
49- */
50- private ?array $ registeredRequests = null ;
51-
52- /**
53- * Instantiate a new class with the arguments.
54- *
55- * @param mixed ...$arguments
56- * @return SaloonConnector
57- */
58- public static function make (...$ arguments ): static
59- {
60- return new static (...$ arguments );
61- }
62-
6343 /**
6444 * Define anything that should be added to any requests
6545 * with this connector before the request is sent.
@@ -72,53 +52,6 @@ public function boot(SaloonRequest $request): void
7252 //
7353 }
7454
75- /**
76- * Attempt to create a request and forward parameters to it.
77- *
78- * @param string $request
79- * @param array $args
80- * @return SaloonRequest
81- * @throws SaloonInvalidRequestException
82- * @throws \ReflectionException
83- */
84- protected function forwardCallToRequest (string $ request , array $ args = []): SaloonRequest
85- {
86- return RequestHelper::callFromConnector ($ this , $ request , $ args );
87- }
88-
89- /**
90- * Bootstrap and get the registered requests in the $requests array.
91- *
92- * @return array
93- * @throws \ReflectionException
94- */
95- public function getRegisteredRequests (): array
96- {
97- if (empty ($ this ->requests )) {
98- return [];
99- }
100-
101- if (is_array ($ this ->registeredRequests )) {
102- return $ this ->registeredRequests ;
103- }
104-
105- $ this ->registeredRequests = ProxyRequestNameHelper::generateNames ($ this ->requests );
106-
107- return $ this ->registeredRequests ;
108- }
109-
110- /**
111- * Check if a given request method exists
112- *
113- * @param string $method
114- * @return bool
115- * @throws \ReflectionException
116- */
117- public function requestExists (string $ method ): bool
118- {
119- return method_exists ($ this , $ method ) || array_key_exists ($ method , $ this ->getRegisteredRequests ());
120- }
121-
12255 /**
12356 * Prepare a new request by providing it the current instance of the connector.
12457 *
@@ -190,48 +123,13 @@ public static function __callStatic($method, $arguments)
190123 }
191124
192125 /**
193- * Attempt to guess the next request .
126+ * Instantiate a new class with the arguments .
194127 *
195- * @param $method
196- * @param $arguments
197- * @return mixed
198- * @throws ClassNotFoundException
199- * @throws SaloonConnectorMethodNotFoundException
200- * @throws SaloonInvalidRequestException
201- * @throws \ReflectionException
128+ * @param mixed ...$arguments
129+ * @return SaloonConnector
202130 */
203- protected function guessRequest ( $ method , $ arguments ): mixed
131+ public static function make (... $ arguments ): static
204132 {
205- if ($ this ->requestExists ($ method ) === false ) {
206- throw new SaloonConnectorMethodNotFoundException ($ method , $ this );
207- }
208-
209- $ requests = $ this ->getRegisteredRequests ();
210-
211- // Work out what it is. If it is an array, pass the array into AnonymousRequestCollection($requests)
212- // If it is a request, just forward the call to the request.
213-
214- $ resource = $ requests [$ method ];
215-
216- // If the request is a type of array, then it must be an anonymous request collection.
217-
218- if (is_array ($ resource )) {
219- return new AnonymousRequestCollection ($ this , $ method , $ resource );
220- }
221-
222- // Otherwise, check if it is a RequestCollection. If it is, then
223- // return that class - otherwise, just forward the request.
224-
225- if (! class_exists ($ resource )) {
226- throw new ClassNotFoundException ($ resource );
227- }
228-
229- if (ReflectionHelper::isSubclassOf ($ resource , RequestCollection::class)) {
230- return new $ resource ($ this );
231- }
232-
233- // It's just a request, so forward to that.
234-
235- return $ this ->forwardCallToRequest ($ resource , $ arguments );
133+ return new static (...$ arguments );
236134 }
237135}
0 commit comments