Skip to content

Commit 82a4569

Browse files
authored
Merge pull request #63 from Sammyjo20/feature/authentication-alias
Feature | Spring cleaning & added authentication alias
2 parents e4cff07 + d04d781 commit 82a4569

15 files changed

+238
-165
lines changed

src/Http/SaloonConnector.php

Lines changed: 16 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,35 @@
22

33
namespace Sammyjo20\Saloon\Http;
44

5-
use Illuminate\Support\Collection;
65
use GuzzleHttp\Promise\PromiseInterface;
76
use Sammyjo20\Saloon\Clients\MockClient;
87
use Sammyjo20\Saloon\Traits\CollectsData;
98
use Sammyjo20\Saloon\Traits\MocksRequests;
10-
use Sammyjo20\Saloon\Helpers\RequestHelper;
119
use Sammyjo20\Saloon\Traits\CollectsConfig;
1210
use Sammyjo20\Saloon\Traits\CollectsHeaders;
11+
use Sammyjo20\Saloon\Traits\GuessesRequests;
1312
use Sammyjo20\Saloon\Traits\CollectsHandlers;
14-
use Sammyjo20\Saloon\Helpers\ReflectionHelper;
1513
use Sammyjo20\Saloon\Traits\HasCustomResponses;
1614
use Sammyjo20\Saloon\Traits\CollectsQueryParams;
1715
use Sammyjo20\Saloon\Traits\CollectsInterceptors;
1816
use Sammyjo20\Saloon\Traits\AuthenticatesRequests;
19-
use Sammyjo20\Saloon\Helpers\ProxyRequestNameHelper;
2017
use Sammyjo20\Saloon\Exceptions\ClassNotFoundException;
2118
use Sammyjo20\Saloon\Interfaces\SaloonConnectorInterface;
2219
use Sammyjo20\Saloon\Exceptions\SaloonInvalidRequestException;
2320
use Sammyjo20\Saloon\Exceptions\SaloonConnectorMethodNotFoundException;
2421

2522
abstract 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
}

src/Http/SaloonRequest.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020

2121
abstract class SaloonRequest implements SaloonRequestInterface
2222
{
23-
use CollectsData,
24-
CollectsQueryParams,
25-
CollectsHeaders,
26-
CollectsConfig,
27-
CollectsHandlers,
28-
CollectsInterceptors,
29-
AuthenticatesRequests,
30-
HasCustomResponses,
31-
SendsRequests,
32-
MocksRequests;
23+
use CollectsData;
24+
use CollectsQueryParams;
25+
use CollectsHeaders;
26+
use CollectsConfig;
27+
use CollectsHandlers;
28+
use CollectsInterceptors;
29+
use AuthenticatesRequests;
30+
use HasCustomResponses;
31+
use SendsRequests;
32+
use MocksRequests;
3333

3434
/**
3535
* Define the method that the request will use.
@@ -62,17 +62,6 @@ public function defineEndpoint(): string
6262
return '';
6363
}
6464

65-
/**
66-
* Instantiate a new class with the arguments.
67-
*
68-
* @param mixed ...$arguments
69-
* @return SaloonRequest
70-
*/
71-
public static function make(...$arguments): static
72-
{
73-
return new static(...$arguments);
74-
}
75-
7665
/**
7766
* Define anything that should be added to the request
7867
* before it is sent.
@@ -126,6 +115,7 @@ private function bootConnector(): void
126115
*
127116
* @return SaloonConnector
128117
* @throws SaloonInvalidConnectorException
118+
* @throws \ReflectionException
129119
*/
130120
public function getConnector(): SaloonConnector
131121
{
@@ -153,7 +143,7 @@ public function setConnector(SaloonConnector $connector): static
153143
* Build up the final request URL.
154144
*
155145
* @return string
156-
* @throws SaloonInvalidConnectorException
146+
* @throws SaloonInvalidConnectorException|\ReflectionException
157147
*/
158148
public function getFullRequestUrl(): string
159149
{
@@ -168,7 +158,7 @@ public function getFullRequestUrl(): string
168158
*
169159
* @param string $trait
170160
* @return bool
171-
* @throws SaloonInvalidConnectorException
161+
* @throws SaloonInvalidConnectorException|\ReflectionException
172162
*/
173163
public function traitExistsOnConnector(string $trait): bool
174164
{
@@ -181,7 +171,8 @@ public function traitExistsOnConnector(string $trait): bool
181171
* @param $method
182172
* @param $parameters
183173
* @return mixed
184-
* @throws SaloonMethodNotFoundException
174+
* @throws SaloonInvalidConnectorException
175+
* @throws SaloonMethodNotFoundException|\ReflectionException
185176
*/
186177
public function __call($method, $parameters)
187178
{
@@ -191,4 +182,15 @@ public function __call($method, $parameters)
191182

192183
return $this->getConnector()->{$method}(...$parameters);
193184
}
185+
186+
/**
187+
* Instantiate a new class with the arguments.
188+
*
189+
* @param mixed ...$arguments
190+
* @return SaloonRequest
191+
*/
192+
public static function make(...$arguments): static
193+
{
194+
return new static(...$arguments);
195+
}
194196
}

src/Traits/AuthenticatesRequests.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,32 @@ public function getAuthenticator(): ?AuthenticatorInterface
4242
* @param AuthenticatorInterface $authenticator
4343
* @return $this
4444
*/
45-
public function withAuth(AuthenticatorInterface $authenticator): self
45+
public function withAuth(AuthenticatorInterface $authenticator): static
4646
{
4747
$this->authenticator = $authenticator;
4848

4949
return $this;
5050
}
5151

52+
/**
53+
* Register an authenticator
54+
*
55+
* @param AuthenticatorInterface $authenticator
56+
* @return $this
57+
*/
58+
public function authenticate(AuthenticatorInterface $authenticator): static
59+
{
60+
return $this->withAuth($authenticator);
61+
}
62+
5263
/**
5364
* Attach an Authorization token to the request.
5465
*
5566
* @param string $token
5667
* @param string $prefix
5768
* @return $this
5869
*/
59-
public function withTokenAuth(string $token, string $prefix = 'Bearer'): self
70+
public function withTokenAuth(string $token, string $prefix = 'Bearer'): static
6071
{
6172
return $this->withAuth(new TokenAuthenticator($token, $prefix));
6273
}
@@ -68,7 +79,7 @@ public function withTokenAuth(string $token, string $prefix = 'Bearer'): self
6879
* @param string $password
6980
* @return $this
7081
*/
71-
public function withBasicAuth(string $username, string $password): self
82+
public function withBasicAuth(string $username, string $password): static
7283
{
7384
return $this->withAuth(new BasicAuthenticator($username, $password));
7485
}
@@ -81,7 +92,7 @@ public function withBasicAuth(string $username, string $password): self
8192
* @param string $digest
8293
* @return $this
8394
*/
84-
public function withDigestAuth(string $username, string $password, string $digest): self
95+
public function withDigestAuth(string $username, string $password, string $digest): static
8596
{
8697
return $this->withAuth(new DigestAuthenticator($username, $password, $digest));
8798
}

src/Traits/CollectsConfig.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function defaultConfig(): array
3636
* @param mixed ...$configCollection
3737
* @return $this
3838
*/
39-
public function mergeConfig(array ...$configCollection): self
39+
public function mergeConfig(array ...$configCollection): static
4040
{
4141
foreach ($configCollection as $config) {
4242
$this->customConfig = array_merge($this->customConfig, $config);
@@ -52,7 +52,7 @@ public function mergeConfig(array ...$configCollection): self
5252
* @param array $config
5353
* @return $this
5454
*/
55-
public function setConfig(array $config): self
55+
public function setConfig(array $config): static
5656
{
5757
$this->ignoreDefaultConfig();
5858

@@ -68,7 +68,7 @@ public function setConfig(array $config): self
6868
* @param $value
6969
* @return $this
7070
*/
71-
public function addConfig(string $item, $value): self
71+
public function addConfig(string $item, $value): static
7272
{
7373
$this->customConfig[$item] = $value;
7474

@@ -99,7 +99,7 @@ public function getConfig(string $key = null): mixed
9999
*
100100
* @return $this
101101
*/
102-
public function ignoreDefaultConfig(): self
102+
public function ignoreDefaultConfig(): static
103103
{
104104
$this->includeDefaultConfig = false;
105105

0 commit comments

Comments
 (0)