Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-novotny committed May 30, 2020
1 parent 6dafe1e commit 890fb66
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 27 deletions.
50 changes: 24 additions & 26 deletions src/Services/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ public function addPackages(string $shipper, array $packages, string $version =
unset($response['labels_url']);
unset($response['status']);

if (count($response) !== count($packages)) {
throw new BadRequestException($response);
}
$this->validateIndexes($response, $packages);

return $response;
}
Expand Down Expand Up @@ -134,25 +132,14 @@ public function trackPackages(string $shipper, array $carrierIds): array
unset($response['status']);

// fixes that API return only last package statuses for GLS shipper
if (
$shipper === Shipper::GLS
&& count($carrierIds) > 1
&& count($response) === 1
&& isset($response[count($carrierIds) - 1])
) {
if ($shipper === Shipper::GLS && count($carrierIds) !== count($response)) {
for ($i = 0; $i < count($carrierIds) - 1; $i++) {
$response[$i] = [];
$response[$i] = $response[$i] ?? [];
}
sort($response);
}

if (isset($response[0]) === false) {
throw new BadRequestException($response);
}

if (count($response) !== count($carrierIds)) {
throw new BadRequestException($response);
}
$this->validateIndexes($response, $carrierIds);

return $response;
}
Expand Down Expand Up @@ -192,9 +179,7 @@ public function trackPackagesLastStatus(string $shipper, array $carrierIds): arr

unset($response['status']);

if (count($response) !== count($carrierIds)) {
throw new BadRequestException($response);
}
$this->validateIndexes($response, $carrierIds);

$formatedStatuses = [];

Expand Down Expand Up @@ -641,9 +626,7 @@ public function orderB2AShipment(string $shipper, array $packages): array

unset($response['status']);

if (count($response) !== count($packages)) {
throw new BadRequestException($response);
}
$this->validateIndexes($response, $packages);

return $response;
}
Expand Down Expand Up @@ -683,9 +666,7 @@ public function getProofOfDeliveries(string $shipper, array $carrierIds): array

unset($response['status']);

if (count($response) !== count($carrierIds)) {
throw new BadRequestException($response);
}
$this->validateIndexes($response, $carrierIds);

$formatedLinks = [];

Expand Down Expand Up @@ -713,6 +694,23 @@ private function validateStatus(array $responseItem, array $response): void
}
}

/**
* Validate indexes
*
* @param array<mixed,mixed> $response
* @param array<mixed,mixed> $request
*
* @return void
*
* @throws \Inspirum\Balikobot\Exceptions\BadRequestException
*/
private function validateIndexes(array $response, array $request): void
{
if (array_keys($response) !== range(0, count($request) - 1)) {
throw new BadRequestException($response);
}
}

/**
* Normalize response items
*
Expand Down
40 changes: 39 additions & 1 deletion tests/Unit/Client/Requests/TrackRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function testThrowsExceptionOnErrorWithMultiplePackages()
$client->trackPackages('cp', [1, 2, 4]);
}

public function testRequestShouldHaveStatusWithMultiplePackages()
public function testRequestShouldNotHaveStatusWithMultiplePackages()
{
$requester = $this->newRequesterWithMockedRequestMethod(200, [
0 => [
Expand Down Expand Up @@ -183,6 +183,44 @@ public function testRequestShouldHaveStatusWithMultiplePackages()
$this->assertNotEmpty($status);
}

public function testThrowsExceptionOnBadPackageIndexes()
{
$this->expectException(BadRequestException::class);

$requester = $this->newRequesterWithMockedRequestMethod(200, [
0 => [
[
'date' => '2018-07-02 00:00:00',
'status_id' => 2,
'name' => 'Dodání zásilky. 10003 Depo Praha 701',
],
[
'date' => '2018-07-02 00:00:00',
'status_id' => 1,
'name' => 'Doručování zásilky. 10003 Depo Praha 701',
],
],
2 => [
[
'date' => '2018-07-02 00:00:00',
'status_id' => 2,
'name' => 'Dodání zásilky. 10005 Depo Praha 701',
],
],
3 => [
[
'date' => '2018-07-02 00:00:00',
'status_id' => 1,
'name' => 'Doručování zásilky. 10003 Depo Praha 701',
],
],
]);

$client = new Client($requester);

$client->trackPackages('cp', [3, 4, 5]);
}

public function testThrowsExceptionOnBadStatusCodeWithMultiplePackages()
{
$this->expectException(BadRequestException::class);
Expand Down

0 comments on commit 890fb66

Please sign in to comment.