Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ should be added automatically to your `config/bundles.php` file by Symfony Flex.
answear_box_now:
clientId: yourClientId
clientSecret: yourClientSecret
apiUrl: apiUrl #default: 'https://api-stage.boxnow.gr'
apiUrl: apiUrl #default: 'https://locationapi-stage.boxnow.gr'
logger: customLogger #default: null
```
Logger service must implement Psr\Log\LoggerInterface interface.

Logger service must implement Psr\Log\LoggerInterface interface.

## Usage

### Authorization

```php
/** @var \Answear\BoxNowBundle\Service\AuthorizationService $authorizationService **/
$auth = $authorizationService->authorize();
Expand All @@ -40,16 +41,26 @@ $auth->getAccessToken();
$auth->getExpiresIn();
$auth->getTokenType();
```
will return `\Answear\BoxNowBundle\Response\AuthorizationResponse`.

will return `\Answear\BoxNowBundle\Response\AuthorizationResponse`.

### Pickup points

```php
/** @var \Answear\BoxNowBundle\Service\PickupPointService $pickupPoints **/
$pickupPoints->getAll(token: 'accessToken');
```

will return `\Answear\BoxNowBundle\DTO\PickupPointDTO[]`.

Or with locationapi (ex. https://locationapi-stage.boxnow.gr/v1/apms_el-GR.json)
```php
/** @var \Answear\BoxNowBundle\Service\PickupPointService $pickupPoints **/
$pickupPoints->getAllByRegion(\Answear\BoxNowBundle\Enum\RegionEnum::Cyprus);
```

will return `\Answear\BoxNowBundle\DTO\PickupPointDTO[]` only for Cyprus (available Greece, Cyprus, Croatia).

Final notes
------------

Expand Down
3 changes: 3 additions & 0 deletions grumphp.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
grumphp:
process_timeout: 120
environment:
variables:
PHP_CS_FIXER_IGNORE_ENV: "1"
ascii:
failed: ~
succeeded: ~
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ConfigProvider
{
private const API_URL = 'https://api-stage.boxnow.gr';
private const API_URL = 'https://locationapi-stage.boxnow.gr';

public function __construct(
public readonly string $clientId,
Expand Down
1 change: 1 addition & 0 deletions src/DTO/PickupPointDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public function __construct(
public string $id,
public string $type,
public string $state,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is some state like "boxnow-ready"

public string $name,
public string $address,
public ?string $title = null,
Expand Down
12 changes: 12 additions & 0 deletions src/Enum/RegionEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Answear\BoxNowBundle\Enum;

enum RegionEnum: string
{
case Croatia = 'hr-HR';
case Cyprus = 'el-CY';
case Greece = 'el-GR';
}
38 changes: 38 additions & 0 deletions src/Request/GetPickupPointsByRegionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Answear\BoxNowBundle\Request;

use Answear\BoxNowBundle\Enum\RegionEnum;

readonly class GetPickupPointsByRegionRequest implements RequestInterface
{
/* please use locationapi url */
private const string ENDPOINT = '/v1/apms_REGION_PLACEHOLDER.json';
private const string HTTP_METHOD = 'GET';

public function __construct(private RegionEnum $region)
{
}

public function getEndpoint(): string
{
return str_replace('REGION_PLACEHOLDER', $this->region->value, self::ENDPOINT);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not sprintf? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a placeholder is better to show what exactly I'm doing

}

public function getMethod(): string
{
return self::HTTP_METHOD;
}

public function getUrlQuery(): ?string
{
return null;
}

public function getHeaders(): array
{
return [];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no authorization required for this, but I'll confirm that

}
}
1 change: 1 addition & 0 deletions src/Response/GetPickupPointsAmpsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function handleData(array $data): void
$this->pickupPoints[] = new PickupPointDTO(
$pickupPoint['id'],
$pickupPoint['state'],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this response there is no type, only state.
Previously as "type" we set "state" so i left it as is

$pickupPoint['state'],
$pickupPoint['name'],
$pickupPoint['addressLine1'],
$pickupPoint['title'] ?? null,
Expand Down
3 changes: 2 additions & 1 deletion src/Response/GetPickupPointsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public function handleData(array $data): void
$this->pickupPoints[] = new PickupPointDTO(
$pickupPoint['id'],
$pickupPoint['type'],
$pickupPoint['name'],
$pickupPoint['state'] ?? '',
trim($pickupPoint['name']),
$pickupPoint['addressLine1'],
$pickupPoint['title'] ?? null,
$pickupPoint['image'] ?? null,
Expand Down
16 changes: 16 additions & 0 deletions src/Service/PickupPointService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Answear\BoxNowBundle\Client\Client;
use Answear\BoxNowBundle\DTO\PickupPointDTO;
use Answear\BoxNowBundle\Enum\RegionEnum;
use Answear\BoxNowBundle\Request\GetPickupPointsAmpsRequest;
use Answear\BoxNowBundle\Request\GetPickupPointsByRegionRequest;
use Answear\BoxNowBundle\Request\GetPickupPointsRequest;
use Answear\BoxNowBundle\Response\GetPickupPointsAmpsResponse;
use Answear\BoxNowBundle\Response\GetPickupPointsResponse;
Expand Down Expand Up @@ -45,4 +47,18 @@ public function getAllWithRegion(string $token, string $region): array

return $pickupPointsResponse->getPickupPoints();
}

/**
* @return PickupPointDTO[]
*/
public function getAllByRegion(RegionEnum $region): array
{
$response = $this->client->request(new GetPickupPointsByRegionRequest($region));

$pickupPointsResponse = GetPickupPointsResponse::fromArray(
$this->serializer->decodeResponse($response)
);

return $pickupPointsResponse->getPickupPoints();
}
}
108 changes: 108 additions & 0 deletions tests/Integration/Service/PickupPointByRegionServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Answear\BoxNowBundle\Tests\Integration\Service;

use Answear\BoxNowBundle\Enum\RegionEnum;
use Answear\BoxNowBundle\Service\PickupPointService;
use Answear\BoxNowBundle\Tests\Util\FileTestUtil;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Webmozart\Assert\InvalidArgumentException;

class PickupPointByRegionServiceTest extends ServiceTestCase
{
#[Test]
public function successfulListAllPickupPoints(): void
{
$this->setClient(withLogger: true);
$service = $this->getService();

$this->mockGuzzleResponse(
new Response(
status: SymfonyResponse::HTTP_OK,
body: FileTestUtil::getFileContents(__DIR__ . '/../../data/pickup-points/success-by-region.json')
)
);

$pickupPoints = $service->getAllByRegion(RegionEnum::Croatia);
$actualData = [];

foreach ($pickupPoints as $pickupPoint) {
$actualData[] = [
'id' => $pickupPoint->id,
'type' => $pickupPoint->type,
'name' => $pickupPoint->name,
'address' => $pickupPoint->address,
'title' => $pickupPoint->title,
'image' => $pickupPoint->image,
'latitude' => $pickupPoint->latitude,
'longitude' => $pickupPoint->longitude,
'postalCode' => $pickupPoint->postalCode,
'country' => $pickupPoint->country,
'note' => $pickupPoint->note,
'additionalAddress' => $pickupPoint->additionalAddress,
'expectedDeliveryTime' => $pickupPoint->expectedDeliveryTime,
'region' => $pickupPoint->region,
];

self::assertSame(
$pickupPoint->address . ', ' . $pickupPoint->additionalAddress,
$pickupPoint->getFullAddress(', ')
);
}

$this->assertJsonStringEqualsJsonString(
json_encode($actualData, JSON_THROW_ON_ERROR),
FileTestUtil::getFileContents(__DIR__ . '/../../data/pickup-points/assert-valid-data-by-region.json'),
);
}

#[Test]
public function invalidPickupPointIdFieldValue(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Field id expected to be a string. Got: ');

$this->setClient();
$service = $this->getService();

$this->mockGuzzleResponse(
new Response(
status: SymfonyResponse::HTTP_OK,
body: FileTestUtil::getFileContents(__DIR__ . '/../../data/pickup-points/invalid-id-field-value.json')
)
);

$service->getAllByRegion(RegionEnum::Cyprus);
}

protected function getLoggerStream(): array
{
return [
'[BOXNOW] Request - /v1/apms_hr-HR.json' => [
'endpoint' => '/v1/apms_hr-HR.json',
'uri' => [
'path' => '/v1/apms_hr-HR.json',
'query' => [],
],
'body' => [],
],
'[BOXNOW] Response - /v1/apms_hr-HR.json' => [
'endpoint' => '/v1/apms_hr-HR.json',
'uri' => [
'path' => '/v1/apms_hr-HR.json',
'query' => [],
],
'response' => '--- HUGE CONTENT SKIPPED ---',
],
];
}

private function getService(): PickupPointService
{
return new PickupPointService($this->getClient(), $this->getSerializer());
}
}
114 changes: 114 additions & 0 deletions tests/data/pickup-points/assert-valid-data-by-region.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
[
{
"additionalAddress": "Λυκόβρυση",
"address": "Λεωφόρος Σοφοκλή Βενιζέλου 49",
"country": "GR",
"expectedDeliveryTime": "2025-06-27T19:00:00.000Z",
"id": "28",
"image": "https://storage.googleapis.com/boxnow-production-images/images/manager/30_11_2022/69366d64-218f-4575-8f2f-6cf571e5095f",
"latitude": 38.071742,
"longitude": 23.785242,
"name": "ΑΒ Λυκόβρυσης",
"note": "Επί της Λεωφόρου Σοφοκλή Βενιζέλου με κατεύθυνση το Ηράκλειο. Στο κατάστημα του ΑΒ Βασιλόπουλου δεξιά από την είσοδο.",
"postalCode": "14123",
"region": "el-GR",
"title": "Λ. ΣOΦOKΛH BENIZEΛOY 49, 14123",
"type": "apm"
},
{
"additionalAddress": "Γαλάτσι",
"address": "Αιγοσθενών 78",
"country": "GR",
"expectedDeliveryTime": "2025-06-27T19:00:00.000Z",
"id": "5143",
"image": "https://storage.googleapis.com/boxnow-production-images/images/operations-manager/07_06_2024/4a62d29d-8510-4b37-955e-fc776c4cbd97",
"latitude": 38.02378602124016,
"longitude": 23.756451726304455,
"name": "e-Mob - Γαλάτσι",
"note": "Επί της οδού Αιγοσθένων, στο κατάστημα e-Mob.",
"postalCode": "11146",
"region": "el-GR",
"title": "AIΓOΣΘENΩN 78, 11146",
"type": "apm"
},
{
"additionalAddress": "Λαμία",
"address": "Πύλου 24",
"country": "GR",
"expectedDeliveryTime": "2025-06-27T19:00:00.000Z",
"id": "7417",
"image": "https://storage.googleapis.com/boxnow-production-images/images/operations-manager/27_03_2024/4ccf4a19-1b85-49e9-bf7e-46d2f38b6ad4",
"latitude": 38.8932357729316,
"longitude": 22.41868934381607,
"name": "Γαλαξίας - 1ο ΧΛΜ Λαμίας Καρπενησίου",
"note": "Επί της οδού Πύλου 24, στο Super Market ΓΑΛΑΞΙΑΣ.",
"postalCode": "35100",
"region": "el-GR",
"title": "ΠYΛOY 24, 35100",
"type": "apm"
},
{
"additionalAddress": "Μεταμόρφωση",
"address": "Αγ Νεκταρίου 69",
"country": "GR",
"expectedDeliveryTime": "2025-06-27T19:00:00.000Z",
"id": "7427",
"image": "https://storage.googleapis.com/boxnow-production-images/images/operations-manager/15_03_2024/82088a63-e40f-4a5d-8bea-beba68a8ea16",
"latitude": 38.05352105272671,
"longitude": 23.759888047413963,
"name": "Pet Vip Arena Market - Μεταμόρφωση",
"note": "Επί της οδού Αγ. Νεκταρίου, στο κατάστημα Arena Market.",
"postalCode": "14451",
"region": "el-GR",
"title": "AΓ NEKTAPIOY, 14451",
"type": "apm"
},
{
"additionalAddress": " Άρτα",
"address": "Κόμβος A5 Ιονίας Οδού & EO Άρτας-Τρικάλων",
"country": "GR",
"expectedDeliveryTime": "2025-06-27T19:00:00.000Z",
"id": "7428",
"image": "https://storage.googleapis.com/boxnow-production-images/images/operations-manager/15_03_2024/dfe5bbe7-6333-4cde-b17a-b96a169690ef",
"latitude": 39.15787014093051,
"longitude": 21.012647900737115,
"name": "ΑΒ Κόμβος Ιονίας Οδού - Άρτα",
"note": "Στη συμβολη Α5 Ιονίας Οδού & ΕΟ Άρτας Τρικάλων, στο Super Market ΑΒ Βασιλόπουλος.",
"postalCode": "47100",
"region": "el-GR",
"title": "KOMBOΣ IONIAΣ OΔOY, 47100",
"type": "apm"
},
{
"additionalAddress": "Εύοσμος",
"address": "25ης Μαρτίου 84-86",
"country": "GR",
"expectedDeliveryTime": "2025-06-27T19:00:00.000Z",
"id": "7436",
"image": "https://storage.googleapis.com/boxnow-production-images/images/operations-manager/11_06_2024/23cacf70-98e6-4f00-860d-024c87cd7aad",
"latitude": 40.6725665098987,
"longitude": 22.91599971354653,
"name": "Το παντοπωλείο της Σοφίας - Εύοσμος",
"note": "Επί της οδού 25ης Μαρτίου, στο Παντοπωλείο της Σοφίας.",
"postalCode": "56224",
"region": "el-GR",
"title": "25HΣ MAPTIOY 84-86, 56224",
"type": "apm"
},
{
"additionalAddress": "Σταυρούπολη",
"address": "Κ. Κωνσταντινίδη & Αγ. Κωνσταντίνου 15",
"country": "GR",
"expectedDeliveryTime": "2025-06-27T19:00:00.000Z",
"id": "7439",
"image": "https://storage.googleapis.com/boxnow-production-images/images/operations-manager/22_11_2024/90d1d461-5ba5-4102-b9fa-d80326e6b9e8",
"latitude": 40.66777703688724,
"longitude": 22.927759187900392,
"name": "Woodncraft - Σταυρούπολη",
"note": "Στη συμβολή των οδών Κωσταντινίδη και Αγ. Κωσταντίνου, στο κατάστημα WoodNCraft.",
"postalCode": "56431",
"region": "el-GR",
"title": "AΓ KΩNΣTANTINOY 15, 56431",
"type": "apm"
}
]
Loading