Skip to content

Commit

Permalink
Reformat ServicePoint functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
villermen committed Nov 18, 2023
1 parent f09d674 commit 0535cd9
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 192 deletions.
115 changes: 57 additions & 58 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function createMultiParcel(
?ShippingMethod $shippingMethod = null,
?string $errors = null,
int $quantity = 1
) : array {
): array {
$parcelData = $this->getParcelData(
null,
$shippingAddress,
Expand Down Expand Up @@ -522,50 +522,46 @@ public function getReturnPortalUrl(Parcel|int $parcel): ?string
}
}



/**
* Summary of searchServicePoints
*
* @see https://api.sendcloud.dev/docs/sendcloud-public-api/service-points%2Foperations%2Flist-service-points
*
* @param string $country A country ISO 2 code (Example : 'NL')
* @param ?string $address Address of the destination address. Can accept postal code instead of the street and the house number. (Example : 'Stadhuisplein 10')
* @param ?string $carrier A comma-separated list of carrier codes (stringified) (Example : 'postnl,dpd')
* @param ?string $city City of the destination address. (Example : 'Eindhoven')
* @param ?string $house_number House number of the destination address. (Example : '10')
* @param ?string $latitude Used as a reference point to calculate the distance of the service point to the provided location.
* @param ?string $longitude Used as a reference point to calculate the distance of the service point to the provided location.
* @param ?string $ne_latitude Latitude of the northeast corner of the bounding box.
* @param ?string $ne_longitude Longitude of the northeast corner of the bounding box.
* @param ?string $postal_code Postal code of the destination address. Using postal_code will return you service points located around that particular postal code. (Example : '5611 EM')
* @param ?string $pudo_id DPD-specific query parameter. (<= 7 characters)
* @param ?int $radius Radius (in meter) of a bounding circle. Can be used instead of the ne_latitude, ne_longitude, sw_latitude, and sw_longitude parameters to define a bounding box. By default, it’s 100 meters. Minimum value: 100 meters. Maximum value: 50 000 meters.
* @param ?string $shop_type Filters results by their shop type.
* @param ?string $sw_latitude Latitude of the southwest corner of the bounding box.
* @param ?string $sw_longitude Longitude of the southwest corner of the bounding box.
* @param ?float $weight Weight (in kg.) of the parcel to be shipped to the service points. Certain carriers impose limits for certain service points that cannot accept parcels above a certain weight limit.
*
* @return array<ServicePoint>
* @param string|null $address Address of the destination address. Can accept postal code instead of the street and the house number. (Example : 'Stadhuisplein 10')
* @param string|null $carrier A comma-separated list of carrier codes (stringified) (Example : 'postnl,dpd')
* @param string|null $city City of the destination address. (Example : 'Eindhoven')
* @param string|null $houseNumber House number of the destination address. (Example : '10')
* @param string|null $latitude Used as a reference point to calculate the distance of the service point to the provided location.
* @param string|null $longitude Used as a reference point to calculate the distance of the service point to the provided location.
* @param string|null $neLatitude Latitude of the northeast corner of the bounding box.
* @param string|null $neLongitude Longitude of the northeast corner of the bounding box.
* @param string|null $postalCode Postal code of the destination address. Using postal_code will return you service points located around that particular postal code. (Example : '5611 EM')
* @param string|null $pudoId DPD-specific query parameter. (<= 7 characters)
* @param int|null $radius Radius (in meter) of a bounding circle. Can be used instead of the ne_latitude, ne_longitude, sw_latitude, and sw_longitude parameters to define a bounding box. By default, it’s 100 meters. Minimum value: 100 meters. Maximum value: 50 000 meters.
* @param string|null $shopType Filters results by their shop type.
* @param string|null $swLatitude Latitude of the southwest corner of the bounding box.
* @param string|null $swLongitude Longitude of the southwest corner of the bounding box.
* @param float|null $weight Weight (in kg.) of the parcel to be shipped to the service points. Certain carriers impose limits for certain service points that cannot accept parcels above a certain weight limit.
* @return ServicePoint[]
*/
public function searchServicePoints(
string $country,
?string $address = null,
?string $carrier = null,
?string $city = null,
?string $house_number = null,
?string $houseNumber = null,
?string $latitude = null,
?string $longitude = null,
?string $ne_latitude = null,
?string $ne_longitude = null,
?string $postal_code = null,
?string $pudo_id = null,
?string $neLatitude = null,
?string $neLongitude = null,
?string $postalCode = null,
?string $pudoId = null,
?int $radius = null,
?string $shop_type = null,
?string $sw_latitude = null,
?string $sw_longitude = null,
?string $shopType = null,
?string $swLatitude = null,
?string $swLongitude = null,
?float $weight = null
) : array
{
): array {
try {
// Construct query array
$query = [];
Expand All @@ -580,43 +576,43 @@ public function searchServicePoints(
if (isset($city)) {
$query['city'] = $city;
}
if (isset($house_number)) {
$query['house_number'] = $house_number;
if (isset($houseNumber)) {
$query['house_number'] = $houseNumber;
}
if (isset($latitude)) {
$query['latitude'] = $latitude;
}
if (isset($longitude)) {
$query['longitude'] = $longitude;
}
if (isset($ne_latitude)) {
$query['ne_latitude'] = $ne_latitude;
if (isset($neLatitude)) {
$query['ne_latitude'] = $neLatitude;
}
if (isset($ne_longitude)) {
$query['ne_longitude'] = $ne_longitude;
if (isset($neLongitude)) {
$query['ne_longitude'] = $neLongitude;
}
if (isset($postal_code)) {
$query['postal_code'] = $postal_code;
if (isset($postalCode)) {
$query['postal_code'] = $postalCode;
}
if (isset($pudo_id)) {
$query['pudo_id'] = $pudo_id;
if (isset($pudoId)) {
$query['pudo_id'] = $pudoId;
}
if (isset($radius)) {
$query['radius'] = $radius;
}
if (isset($shop_type)) {
$query['shop_type'] = $shop_type;
if (isset($shopType)) {
$query['shop_type'] = $shopType;
}
if (isset($sw_latitude)) {
$query['sw_latitude'] = $sw_latitude;
if (isset($swLatitude)) {
$query['sw_latitude'] = $swLatitude;
}
if (isset($sw_longitude)) {
$query['sw_longitude'] = $sw_longitude;
if (isset($swLongitude)) {
$query['sw_longitude'] = $swLongitude;
}
if (isset($weight)) {
$query['weight'] = $weight;
}

// Send request
$response = $this->guzzleClient->get('service-point', [
'query' => $query,
Expand All @@ -625,28 +621,30 @@ public function searchServicePoints(
// Decode and create ServicePoint objects
$json = json_decode((string)$response->getBody(), true);

$service_points = [];
foreach($json as $obj) {
$service_points[] = ServicePoint::fromData($obj);
$servicePoints = [];
foreach ($json as $obj) {
$servicePoints[] = ServicePoint::fromData($obj);
}

return $service_points;
return $servicePoints;
} catch (TransferException $exception) {
throw $this->parseGuzzleException($exception, 'Could not retrieve service point.');
}
}

/**
* Return corresponding service point for the given id.
* Returns service point by ID.
*
* @see https://api.sendcloud.dev/docs/sendcloud-public-api/service-points%2Foperations%2Fget-a-service-point
*
* @param int $service_point_id
* @return \JouwWeb\Sendcloud\Model\ServicePoint
* @return ServicePoint
* @throws SendcloudRequestException
*/
public function getServicePoint(int $service_point_id) : ServicePoint
public function getServicePoint(ServicePoint|int $servicePoint): ServicePoint
{
$servicePointId = $servicePoint instanceof ServicePoint ? $servicePoint->getId() : $servicePoint;

try {
$response = $this->guzzleClient->get('service-point/' . $service_point_id);
$response = $this->guzzleClient->get('service-point/' . $servicePointId);
return ServicePoint::fromData(json_decode((string)$response->getBody(), true));
} catch (TransferException $exception) {
throw $this->parseGuzzleException($exception, 'Could not retrieve service point.');
Expand Down Expand Up @@ -851,6 +849,7 @@ protected function parseGuzzleException(
return new SendcloudRequestException($message, $code, $exception, $responseCode, $responseMessage);
}

// TODO: Remove parseParcelArgument() now we use native unions.
protected function parseParcelArgument(Parcel|int $parcel): int
{
if (is_int($parcel)) {
Expand Down
Loading

0 comments on commit 0535cd9

Please sign in to comment.