Skip to content
This repository was archived by the owner on Sep 1, 2025. It is now read-only.

Commit 9296bd8

Browse files
Remove repository pattern (#23)
* client should only use a http client * refactor job repoisitory * refactor pricing and eta repository * update to version 3.0.0 * update readme * Update README.md * Update README.md * reset old notation
1 parent 29c688f commit 9296bd8

21 files changed

Lines changed: 230 additions & 427 deletions

‎README.md‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,19 @@ $api_client_id = '65176d7a1f4e734f6723hd690825f166f8dadf69fb40af52fffdeac4593e4b
3434
$api_client_secret = '681ae68635c7aadef5cd1jdng8ef357a808cd9dc794811296446f19268d48fcd'; // can be found here: https://admin-sandbox.stuart.com/client/api
3535
$authenticator = new \Stuart\Infrastructure\Authenticator($environment, $api_client_id, $api_client_secret);
3636

37-
$client = new \Stuart\Client($authenticator);
37+
$httpClient = new \Stuart\Infrastructure\HttpClient($authenticator)
38+
39+
$client = new \Stuart\Client($httpClient);
3840
```
3941

40-
You can also pass your own Guzzle client instance to the `\Stuart\Client` constructor:
42+
You can also pass your own Guzzle client instance to the `\Stuart\HttpClient` constructor:
4143

4244
```php
4345
$guzzleClient = new \Guzzle\Client();
44-
$client = new \Stuart\Client($authenticator, $guzzleClient);
46+
$httpClient = new \Stuart\Infrastructure\HttpClient($authenticator, $guzzleClient);
4547
```
4648

47-
This can be useful if you need to attach middlewares to the Guzzle client used by the Stuart client.
49+
This can be useful if you need to attach middlewares to the Guzzle client.
4850

4951
### Create a Job
5052

@@ -332,13 +334,6 @@ You can also send requests on your own without relying on the `\Stuart\Client`.
332334
It allows you to use endpoints that are not yet available on the `\Stuart\Client` and enjoy the `\Stuart\Authenticator`.
333335

334336
```php
335-
$environment = \Stuart\Infrastructure\Environment::SANDBOX;
336-
$api_client_id = '65176d7a1f4e734f6723hd690825f166f8dadf69fb40af52fffdeac4593e4bc'; // can be found here: https://admin-sandbox.stuart.com/client/api
337-
$api_client_secret = '681ae68635c7aadef5cd1jdng8ef357a808cd9dc794811296446f19268d48fcd'; // can be found here: https://admin-sandbox.stuart.com/client/api
338-
$authenticator = new \Stuart\Infrastructure\Authenticator($environment, $api_client_id, $api_client_secret);
339-
340-
$httpClient = new \Stuart\Infrastructure\HttpClient($this->authenticator);
341-
342337
$apiResponse = $httpClient->performGet('/v2/jobs?page=1');
343338
$apiResponse->success();
344339
$apiResponse->getBody();

‎src/Stuart/Client.php‎

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,15 @@
33
namespace Stuart;
44

55
use Stuart\Converters\JobToJson;
6-
use Stuart\Infrastructure\HttpClient;
7-
use Stuart\Repository\JobEtaRepository;
8-
use Stuart\Repository\JobPricingRepository;
9-
use Stuart\Repository\JobRepository;
6+
use Stuart\Converters\JsonToJob;
107

118
class Client
129
{
1310
private $httpClient;
14-
private $jobRepository;
15-
private $jobPricingRepository;
16-
private $jobEtaRepository;
1711

18-
public function __construct($authenticator, \GuzzleHttp\Client $client = null)
12+
public function __construct($httpClient)
1913
{
20-
$guzzleClient = $client ?: new \GuzzleHttp\Client();
21-
$this->httpClient = new HttpClient($authenticator, $guzzleClient);
22-
$this->jobRepository = new JobRepository($this->httpClient);
23-
$this->jobPricingRepository = new JobPricingRepository($this->httpClient);
24-
$this->jobEtaRepository = new JobEtaRepository($this->httpClient);
14+
$this->httpClient = $httpClient;
2515
}
2616

2717
/**
@@ -43,12 +33,25 @@ public function validateJob(Job $job)
4333

4434
public function createJob($job)
4535
{
46-
return $this->jobRepository->save($job);
36+
$body = JobToJson::convert($job);
37+
38+
$apiResponse = $this->httpClient->performPost($body, '/v2/jobs');
39+
if ($apiResponse->success()) {
40+
return JsonToJob::convert($apiResponse->getBody());
41+
} else {
42+
return json_decode($apiResponse->getBody());
43+
}
4744
}
4845

4946
public function getJob($jobId)
5047
{
51-
return $this->jobRepository->get($jobId);
48+
$apiResponse = $this->httpClient->performGet('/v2/jobs/' . $jobId);
49+
50+
if ($apiResponse->success()) {
51+
return JsonToJob::convert($apiResponse->getBody());
52+
} else {
53+
return json_decode($apiResponse->getBody());
54+
}
5255
}
5356

5457
public function cancelJob($jobId)
@@ -62,24 +65,30 @@ public function cancelJob($jobId)
6265
}
6366
}
6467

65-
public function cancelDelivery($deliveryId)
66-
{
67-
$apiResponse = $this->httpClient->performPost('', '/v2/deliveries/' . $deliveryId . '/cancel');
68+
public function cancelDelivery($deliveryId)
69+
{
70+
$apiResponse = $this->httpClient->performPost('', '/v2/deliveries/' . $deliveryId . '/cancel');
6871

69-
if ($apiResponse->success()) {
70-
return true;
71-
} else {
72-
return json_decode($apiResponse->getBody());
73-
}
74-
}
72+
if ($apiResponse->success()) {
73+
return true;
74+
} else {
75+
return json_decode($apiResponse->getBody());
76+
}
77+
}
7578

7679
public function getPricing($job)
7780
{
78-
return $this->jobPricingRepository->save($job);
81+
$body = JobToJson::convert($job);
82+
83+
$apiResponse = $this->httpClient->performPost($body, '/v2/jobs/pricing');
84+
return json_decode($apiResponse->getBody());
7985
}
8086

8187
public function getEta($job)
8288
{
83-
return $this->jobEtaRepository->save($job);
89+
$body = JobToJson::convert($job);
90+
91+
$apiResponse = $this->httpClient->performPost($body, '/v2/jobs/eta');
92+
return json_decode($apiResponse->getBody());
8493
}
8594
}

‎src/Stuart/DropOff.php‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,36 @@ class DropOff extends Location
88
private $packageDescription;
99
private $clientReference;
1010

11-
public function setPackageType($packageType)
11+
public function getPackageType()
1212
{
13-
$this->packageType = $packageType;
14-
return $this;
13+
return $this->packageType;
1514
}
1615

17-
public function setPackageDescription($packageDescription)
16+
public function setPackageType($packageType)
1817
{
19-
$this->packageDescription = $packageDescription;
18+
$this->packageType = $packageType;
2019
return $this;
2120
}
2221

23-
public function setClientReference($clientReference)
22+
public function getPackageDescription()
2423
{
25-
$this->clientReference = $clientReference;
26-
return $this;
24+
return $this->packageDescription;
2725
}
2826

29-
public function getPackageType()
27+
public function setPackageDescription($packageDescription)
3028
{
31-
return $this->packageType;
29+
$this->packageDescription = $packageDescription;
30+
return $this;
3231
}
3332

34-
public function getPackageDescription()
33+
public function getClientReference()
3534
{
36-
return $this->packageDescription;
35+
return $this->clientReference;
3736
}
3837

39-
public function getClientReference()
38+
public function setClientReference($clientReference)
4039
{
41-
return $this->clientReference;
40+
$this->clientReference = $clientReference;
41+
return $this;
4242
}
4343
}

‎src/Stuart/Infrastructure/ApiResponseFactory.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ApiResponseFactory
77
public static function fromGuzzleHttpResponse($guzzleHttpResponse)
88
{
99
$statusCode = $guzzleHttpResponse->getStatusCode();
10-
$body = (string) $guzzleHttpResponse->getBody();
10+
$body = (string)$guzzleHttpResponse->getBody();
1111
return new ApiResponse($statusCode, $body);
1212
}
1313
}

‎src/Stuart/Infrastructure/Authenticator.php‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Stuart\Infrastructure;
44

5-
use Psr\SimpleCache\CacheInterface;
65
use League\OAuth2\Client\Provider\GenericProvider;
6+
use Psr\SimpleCache\CacheInterface;
77

88
class Authenticator
99
{
@@ -51,41 +51,41 @@ public function getAccessToken()
5151
return $this->getNewAccessToken();
5252
}
5353

54-
/**
55-
* @return \Stuart\Infrastructure\Environment
56-
*/
57-
public function getEnvironment()
54+
private function accessTokenIsCachable()
5855
{
59-
return $this->environment;
56+
return $this->cache !== null;
6057
}
6158

62-
private function getNewAccessToken()
59+
private function getAccessTokenFromCache()
6360
{
64-
$accessToken = $this->provider->getAccessToken('client_credentials');
65-
if ($this->accessTokenIsCachable()) {
66-
$this->addAccessTokenToCache($accessToken);
67-
}
68-
return $accessToken;
61+
return $this->cache->get($this->accessTokenCacheKey());
6962
}
7063

71-
private function accessTokenIsCachable()
64+
private function accessTokenCacheKey()
7265
{
73-
return $this->cache !== null;
66+
$envAsString = $this->environment === Environment::SANDBOX ? 'SANDBOX' : 'PRODUCTION';
67+
return 'STUART_' . $envAsString . '_CACHE_ACCESS_TOKEN_KEY';
7468
}
7569

76-
private function getAccessTokenFromCache()
70+
private function getNewAccessToken()
7771
{
78-
return $this->cache->get($this->accessTokenCacheKey());
72+
$accessToken = $this->provider->getAccessToken('client_credentials');
73+
if ($this->accessTokenIsCachable()) {
74+
$this->addAccessTokenToCache($accessToken);
75+
}
76+
return $accessToken;
7977
}
8078

8179
private function addAccessTokenToCache($accessToken)
8280
{
8381
$this->cache->set($this->accessTokenCacheKey(), $accessToken);
8482
}
8583

86-
private function accessTokenCacheKey()
84+
/**
85+
* @return \Stuart\Infrastructure\Environment
86+
*/
87+
public function getEnvironment()
8788
{
88-
$envAsString = $this->environment === Environment::SANDBOX ? 'SANDBOX' : 'PRODUCTION';
89-
return 'STUART_' . $envAsString . '_CACHE_ACCESS_TOKEN_KEY';
89+
return $this->environment;
9090
}
9191
}

‎src/Stuart/Infrastructure/HttpClient.php‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,16 @@ public function __construct($authenticator, $client = null)
3333
$this->client = $client === null ? new Client() : $client;
3434
}
3535

36-
3736
/**
38-
* @param $formParams
37+
* @param $body
3938
* @param $resource
4039
* @return ApiResponse
4140
*/
42-
public function performPost($formParams, $resource)
41+
public function performPost($body, $resource)
4342
{
4443
try {
4544
$response = $this->client->request('POST', $this->baseUrl . $resource, [
46-
'body' => $formParams,
45+
'body' => $body,
4746
'headers' => $this->defaultHeaders()
4847
]);
4948
} catch (RequestException $e) {
@@ -57,6 +56,18 @@ public function performPost($formParams, $resource)
5756
return ApiResponseFactory::fromGuzzleHttpResponse($response);
5857
}
5958

59+
/**
60+
* @return array
61+
*/
62+
private function defaultHeaders()
63+
{
64+
return [
65+
'Authorization' => 'Bearer ' . $this->authenticator->getAccessToken(),
66+
'User-Agent' => 'stuart-php-client/3.0.0',
67+
'Content-Type' => 'application/json'
68+
];
69+
}
70+
6071
/**
6172
* @param $resource
6273
* @return ApiResponse
@@ -77,16 +88,4 @@ public function performGet($resource)
7788

7889
return ApiResponseFactory::fromGuzzleHttpResponse($response);
7990
}
80-
81-
/**
82-
* @return array
83-
*/
84-
private function defaultHeaders()
85-
{
86-
return [
87-
'Authorization' => 'Bearer ' . $this->authenticator->getAccessToken(),
88-
'User-Agent' => 'stuart-php-client/2.8.0',
89-
'Content-Type' => 'application/json'
90-
];
91-
}
9291
}

‎src/Stuart/Location.php‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ abstract class Location
1212
private $lastName;
1313
private $company;
1414

15-
public function setAddress($address)
16-
{
17-
$this->address = $address;
18-
return $this;
19-
}
20-
21-
public function setComment($comment)
22-
{
23-
$this->comment = $comment;
24-
return $this;
25-
}
26-
2715
public function setContactPhone($phone)
2816
{
2917
$this->phone = $phone;
@@ -59,11 +47,23 @@ public function getAddress()
5947
return $this->address;
6048
}
6149

50+
public function setAddress($address)
51+
{
52+
$this->address = $address;
53+
return $this;
54+
}
55+
6256
public function getComment()
6357
{
6458
return $this->comment;
6559
}
6660

61+
public function setComment($comment)
62+
{
63+
$this->comment = $comment;
64+
return $this;
65+
}
66+
6767
public function getPhone()
6868
{
6969
return $this->phone;

‎src/Stuart/Pickup.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ class Pickup extends Location
66
{
77
private $pickupAt;
88

9-
public function setPickupAt($pickupAt)
9+
public function getPickupAt()
1010
{
11-
$this->pickupAt = $pickupAt;
12-
return $this;
11+
return $this->pickupAt;
1312
}
1413

15-
public function getPickupAt()
14+
public function setPickupAt($pickupAt)
1615
{
17-
return $this->pickupAt;
16+
$this->pickupAt = $pickupAt;
17+
return $this;
1818
}
1919
}

0 commit comments

Comments
 (0)