Skip to content

Commit 8c445f6

Browse files
committed
Apms by region request
1 parent f31a8c4 commit 8c445f6

12 files changed

+433
-5
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ should be added automatically to your `config/bundles.php` file by Symfony Flex.
2323
answear_box_now:
2424
clientId: yourClientId
2525
clientSecret: yourClientSecret
26-
apiUrl: apiUrl #default: 'https://api-stage.boxnow.gr'
26+
apiUrl: apiUrl #default: 'https://locationapi-stage.boxnow.gr'
2727
logger: customLogger #default: null
2828
```
29-
Logger service must implement Psr\Log\LoggerInterface interface.
3029
30+
Logger service must implement Psr\Log\LoggerInterface interface.
3131
3232
## Usage
3333
3434
### Authorization
35+
3536
```php
3637
/** @var \Answear\BoxNowBundle\Service\AuthorizationService $authorizationService **/
3738
$auth = $authorizationService->authorize();
@@ -40,16 +41,26 @@ $auth->getAccessToken();
4041
$auth->getExpiresIn();
4142
$auth->getTokenType();
4243
```
43-
will return `\Answear\BoxNowBundle\Response\AuthorizationResponse`.
4444

45+
will return `\Answear\BoxNowBundle\Response\AuthorizationResponse`.
4546

4647
### Pickup points
48+
4749
```php
4850
/** @var \Answear\BoxNowBundle\Service\PickupPointService $pickupPoints **/
4951
$pickupPoints->getAll(token: 'accessToken');
5052
```
53+
5154
will return `\Answear\BoxNowBundle\DTO\PickupPointDTO[]`.
5255

56+
Or with locationapi (ex. https://locationapi-stage.boxnow.gr/v1/apms_el-GR.json)
57+
```php
58+
/** @var \Answear\BoxNowBundle\Service\PickupPointService $pickupPoints **/
59+
$pickupPoints->getAllByRegion(\Answear\BoxNowBundle\Enum\RegionEnum::Cyprus);
60+
```
61+
62+
will return `\Answear\BoxNowBundle\DTO\PickupPointDTO[]` only for Cyprus (available Greece, Cyprus, Croatia).
63+
5364
Final notes
5465
------------
5566

grumphp.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
grumphp:
22
process_timeout: 120
3+
environment:
4+
variables:
5+
PHP_CS_FIXER_IGNORE_ENV: "1"
36
ascii:
47
failed: ~
58
succeeded: ~

src/ConfigProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class ConfigProvider
88
{
9-
private const API_URL = 'https://api-stage.boxnow.gr';
9+
private const API_URL = 'https://locationapi-stage.boxnow.gr';
1010

1111
public function __construct(
1212
public readonly string $clientId,

src/DTO/PickupPointDTO.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
public function __construct(
88
public string $id,
99
public string $type,
10+
public string $state,
1011
public string $name,
1112
public string $address,
1213
public ?string $title = null,

src/Enum/RegionEnum.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Answear\BoxNowBundle\Enum;
6+
7+
enum RegionEnum: string
8+
{
9+
case Croatia = 'hr-HR';
10+
case Cyprus = 'el-CY';
11+
case Greece = 'el-GR';
12+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Answear\BoxNowBundle\Request;
6+
7+
use Answear\BoxNowBundle\Enum\RegionEnum;
8+
9+
readonly class GetPickupPointsByRegionRequest implements RequestInterface
10+
{
11+
/* please use locationapi url */
12+
private const string ENDPOINT = '/v1/apms_REGION_PLACEHOLDER.json';
13+
private const string HTTP_METHOD = 'GET';
14+
15+
public function __construct(private RegionEnum $region)
16+
{
17+
}
18+
19+
public function getEndpoint(): string
20+
{
21+
return str_replace('REGION_PLACEHOLDER', $this->region->value, self::ENDPOINT);
22+
}
23+
24+
public function getMethod(): string
25+
{
26+
return self::HTTP_METHOD;
27+
}
28+
29+
public function getUrlQuery(): ?string
30+
{
31+
return null;
32+
}
33+
34+
public function getHeaders(): array
35+
{
36+
return [];
37+
}
38+
}

src/Response/GetPickupPointsAmpsResponse.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function handleData(array $data): void
2525
$this->pickupPoints[] = new PickupPointDTO(
2626
$pickupPoint['id'],
2727
$pickupPoint['state'],
28+
$pickupPoint['state'],
2829
$pickupPoint['name'],
2930
$pickupPoint['addressLine1'],
3031
$pickupPoint['title'] ?? null,

src/Response/GetPickupPointsResponse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public function handleData(array $data): void
2525
$this->pickupPoints[] = new PickupPointDTO(
2626
$pickupPoint['id'],
2727
$pickupPoint['type'],
28-
$pickupPoint['name'],
28+
$pickupPoint['state'] ?? '',
29+
trim($pickupPoint['name']),
2930
$pickupPoint['addressLine1'],
3031
$pickupPoint['title'] ?? null,
3132
$pickupPoint['image'] ?? null,

src/Service/PickupPointService.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Answear\BoxNowBundle\Client\Client;
66
use Answear\BoxNowBundle\DTO\PickupPointDTO;
7+
use Answear\BoxNowBundle\Enum\RegionEnum;
78
use Answear\BoxNowBundle\Request\GetPickupPointsAmpsRequest;
9+
use Answear\BoxNowBundle\Request\GetPickupPointsByRegionRequest;
810
use Answear\BoxNowBundle\Request\GetPickupPointsRequest;
911
use Answear\BoxNowBundle\Response\GetPickupPointsAmpsResponse;
1012
use Answear\BoxNowBundle\Response\GetPickupPointsResponse;
@@ -45,4 +47,18 @@ public function getAllWithRegion(string $token, string $region): array
4547

4648
return $pickupPointsResponse->getPickupPoints();
4749
}
50+
51+
/**
52+
* @return PickupPointDTO[]
53+
*/
54+
public function getAllByRegion(RegionEnum $region): array
55+
{
56+
$response = $this->client->request(new GetPickupPointsByRegionRequest($region));
57+
58+
$pickupPointsResponse = GetPickupPointsResponse::fromArray(
59+
$this->serializer->decodeResponse($response)
60+
);
61+
62+
return $pickupPointsResponse->getPickupPoints();
63+
}
4864
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Answear\BoxNowBundle\Tests\Integration\Service;
6+
7+
use Answear\BoxNowBundle\Enum\RegionEnum;
8+
use Answear\BoxNowBundle\Service\PickupPointService;
9+
use Answear\BoxNowBundle\Tests\Util\FileTestUtil;
10+
use GuzzleHttp\Psr7\Response;
11+
use PHPUnit\Framework\Attributes\Test;
12+
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
13+
use Webmozart\Assert\InvalidArgumentException;
14+
15+
class PickupPointByRegionServiceTest extends ServiceTestCase
16+
{
17+
#[Test]
18+
public function successfulListAllPickupPoints(): void
19+
{
20+
$this->setClient(withLogger: true);
21+
$service = $this->getService();
22+
23+
$this->mockGuzzleResponse(
24+
new Response(
25+
status: SymfonyResponse::HTTP_OK,
26+
body: FileTestUtil::getFileContents(__DIR__ . '/../../data/pickup-points/success-by-region.json')
27+
)
28+
);
29+
30+
$pickupPoints = $service->getAllByRegion(RegionEnum::Croatia);
31+
$actualData = [];
32+
33+
foreach ($pickupPoints as $pickupPoint) {
34+
$actualData[] = [
35+
'id' => $pickupPoint->id,
36+
'type' => $pickupPoint->type,
37+
'name' => $pickupPoint->name,
38+
'address' => $pickupPoint->address,
39+
'title' => $pickupPoint->title,
40+
'image' => $pickupPoint->image,
41+
'latitude' => $pickupPoint->latitude,
42+
'longitude' => $pickupPoint->longitude,
43+
'postalCode' => $pickupPoint->postalCode,
44+
'country' => $pickupPoint->country,
45+
'note' => $pickupPoint->note,
46+
'additionalAddress' => $pickupPoint->additionalAddress,
47+
'expectedDeliveryTime' => $pickupPoint->expectedDeliveryTime,
48+
'region' => $pickupPoint->region,
49+
];
50+
51+
self::assertSame(
52+
$pickupPoint->address . ', ' . $pickupPoint->additionalAddress,
53+
$pickupPoint->getFullAddress(', ')
54+
);
55+
}
56+
57+
$this->assertJsonStringEqualsJsonString(
58+
json_encode($actualData, JSON_THROW_ON_ERROR),
59+
FileTestUtil::getFileContents(__DIR__ . '/../../data/pickup-points/assert-valid-data-by-region.json'),
60+
);
61+
}
62+
63+
#[Test]
64+
public function invalidPickupPointIdFieldValue(): void
65+
{
66+
$this->expectException(InvalidArgumentException::class);
67+
$this->expectExceptionMessage('Field id expected to be a string. Got: ');
68+
69+
$this->setClient();
70+
$service = $this->getService();
71+
72+
$this->mockGuzzleResponse(
73+
new Response(
74+
status: SymfonyResponse::HTTP_OK,
75+
body: FileTestUtil::getFileContents(__DIR__ . '/../../data/pickup-points/invalid-id-field-value.json')
76+
)
77+
);
78+
79+
$service->getAllByRegion(RegionEnum::Cyprus);
80+
}
81+
82+
protected function getLoggerStream(): array
83+
{
84+
return [
85+
'[BOXNOW] Request - /v1/apms_hr-HR.json' => [
86+
'endpoint' => '/v1/apms_hr-HR.json',
87+
'uri' => [
88+
'path' => '/v1/apms_hr-HR.json',
89+
'query' => [],
90+
],
91+
'body' => [],
92+
],
93+
'[BOXNOW] Response - /v1/apms_hr-HR.json' => [
94+
'endpoint' => '/v1/apms_hr-HR.json',
95+
'uri' => [
96+
'path' => '/v1/apms_hr-HR.json',
97+
'query' => [],
98+
],
99+
'response' => '--- HUGE CONTENT SKIPPED ---',
100+
],
101+
];
102+
}
103+
104+
private function getService(): PickupPointService
105+
{
106+
return new PickupPointService($this->getClient(), $this->getSerializer());
107+
}
108+
}

0 commit comments

Comments
 (0)