Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-novotny committed Nov 17, 2019
1 parent e490753 commit eb8c45d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 72 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ var_dump($data);

## System requirements

* [PHP 7.1](http://php.net/releases/7_1_0.php)
* [PHP 7.1+](http://php.net/releases/7_1_0.php)
* [ext-curl](http://php.net/curl)
* [ext-json](http://php.net/json)

Expand Down
4 changes: 2 additions & 2 deletions src/Model/Values/AbstractPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

abstract class AbstractPackage implements ArrayAccess
{
use CommonData;

/**
* Package data
*
Expand All @@ -24,8 +26,6 @@ public function __construct(array $data = [])
$this->data = $data;
}

use CommonData;

/**
* Determine if an item exists at an offset
*
Expand Down
3 changes: 2 additions & 1 deletion src/Model/Values/Branch.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,8 @@ private function setBranchId(): void
private function resolveBranchId(): string
{
// get key used in branch_id when calling add request
if ($this->shipper === Shipper::CP
if (
$this->shipper === Shipper::CP
|| $this->shipper === Shipper::SP
|| ($this->shipper === Shipper::ULOZENKA && $this->service === ServiceType::ULOZENKA_CP_NP)
) {
Expand Down
96 changes: 28 additions & 68 deletions src/Services/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@ public function trackPackagesLastStatus(string $shipper, array $carrierIds): arr

$response = $this->requester->call(API::V1, $shipper, Request::TRACK_STATUS, $data, false);

if (empty($response[0])) {
throw new BadRequestException($response);
}

unset($response['status']);

if (count($response) !== count($carrierIds)) {
Expand Down Expand Up @@ -342,11 +338,7 @@ public function getServices(string $shipper): array
{
$response = $this->requester->call(API::V1, $shipper, Request::SERVICES);

if (isset($response['service_types']) === false) {
return [];
}

return $response['service_types'];
return $response['service_types'] ?? [];
}

/**
Expand All @@ -366,13 +358,7 @@ public function getManipulationUnits(string $shipper): array
return [];
}

$units = [];

foreach ($response['units'] as $item) {
$units[$item['code']] = $item['name'];
}

return $units;
return $this->normalizeResponseItems($response['units'], 'code', 'name');
}

/**
Expand All @@ -398,11 +384,7 @@ public function getBranches(

$response = $this->requester->call(API::V1, $shipper, $request . '/' . $service . '/' . $country);

if ($response['branches'] === null) {
return [];
}

return $response['branches'];
return $response['branches'] ?? [];
}

/**
Expand Down Expand Up @@ -447,11 +429,7 @@ public function getBranchesForLocation(

$response = $this->requester->call(API::V1, $shipper, Request::BRANCH_LOCATOR, $data);

if ($response['branches'] === null) {
return [];
}

return $response['branches'];
return $response['branches'] ?? [];
}

/**
Expand All @@ -467,17 +445,7 @@ public function getCodCountries(string $shipper): array
{
$response = $this->requester->call(API::V1, $shipper, Request::CASH_ON_DELIVERY_COUNTRIES);

if ($response['service_types'] === null) {
return [];
}

$services = [];

foreach ($response['service_types'] as $item) {
$services[$item['service_type']] = $item['cod_countries'];
}

return $services;
return $this->normalizeResponseItems($response['service_types'] ?? [], 'service_type', 'cod_countries');
}

/**
Expand All @@ -493,17 +461,7 @@ public function getCountries(string $shipper): array
{
$response = $this->requester->call(API::V1, $shipper, Request::COUNTRIES);

if ($response['service_types'] === null) {
return [];
}

$services = [];

foreach ($response['service_types'] as $item) {
$services[$item['service_type']] = $item['countries'];
}

return $services;
return $this->normalizeResponseItems($response['service_types'] ?? [], 'service_type', 'countries');
}

/**
Expand All @@ -529,14 +487,10 @@ public function getPostCodes(string $shipper, string $service, string $country =

$response = $this->requester->call(API::V1, $shipper, Request::ZIP_CODES . '/' . $urlPath);

if ($response['zip_codes'] === null) {
return [];
}

$country = $response['country'] ?? $country;
$formattedPostCodes = [];

foreach ($response['zip_codes'] as $postCode) {
foreach ($response['zip_codes'] ?? [] as $postCode) {
$formattedPostCodes[] = [
'postcode' => $postCode['zip'] ?? ($postCode['zip_start'] ?? null),
'postcode_end' => $postCode['zip_end'] ?? null,
Expand Down Expand Up @@ -577,17 +531,7 @@ public function getAdrUnits(string $shipper): array
{
$response = $this->requester->call(API::V1, $shipper, Request::ADR_UNITS);

if ($response['units'] === null) {
return [];
}

$units = [];

foreach ($response['units'] as $item) {
$units[$item['code']] = $item['name'];
}

return $units;
return $this->normalizeResponseItems($response['units'] ?? [], 'code', 'name');
}

/**
Expand Down Expand Up @@ -664,10 +608,6 @@ public function getProofOfDeliveries(string $shipper, array $carrierIds): array

$response = $this->requester->call(API::V1, $shipper, Request::PROOF_OF_DELIVERY, $data, false);

if (empty($response[0])) {
throw new BadRequestException($response);
}

unset($response['status']);

if (count($response) !== count($carrierIds)) {
Expand All @@ -687,6 +627,26 @@ public function getProofOfDeliveries(string $shipper, array $carrierIds): array
return $formatedLinks;
}

/**
* Normalize response items
*
* @param array $items
* @param string $keyName
* @param string $valueName
*
* @return array
*/
private function normalizeResponseItems(array $items, string $keyName, string $valueName): array
{
$services = [];

foreach ($items as $item) {
$services[$item[$keyName]] = $item[$valueName];
}

return $services;
}

/**
* Encapsulate ids
*
Expand Down

0 comments on commit eb8c45d

Please sign in to comment.