Skip to content

Commit 0b42254

Browse files
committed
Added findDeliveryPoint method to get only one point
1 parent bf4d2f6 commit 0b42254

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/MondialRelay/ApiClient.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,17 @@ public function findDeliveryPoints(array $request)
2121
try{
2222
$request = $this->decorateRequest($request);
2323
$result = $this->client->WSI3_PointRelais_Recherche($request);
24-
$this->checkResponse($result);
24+
$this->checkResponse('WSI3_PointRelais_Recherche',$result);
2525
$delivery_points = [];
2626
if (!property_exists($result->WSI3_PointRelais_RechercheResult->PointsRelais, 'PointRelais_Details')){
2727
return $delivery_points;
2828
}
2929
$label_position = 1;
30+
if (is_object($result->WSI3_PointRelais_RechercheResult->PointsRelais->PointRelais_Details)){
31+
return $this->createPoint($result->WSI3_PointRelais_RechercheResult->PointsRelais->PointRelais_Details);
32+
}
3033
foreach($result->WSI3_PointRelais_RechercheResult->PointsRelais->PointRelais_Details as $destination_point){
31-
$delivery_points[] = new Point(
32-
$destination_point->Num,
33-
trim($destination_point->LgAdr1),
34-
str_replace(",",".",$destination_point->Latitude),
35-
str_replace(",",".",$destination_point->Longitude),
36-
$destination_point->CP
37-
);
34+
$delivery_points[] = $this->createPoint($destination_point);
3835
$label_position++;
3936
}
4037
return $delivery_points;
@@ -44,6 +41,19 @@ public function findDeliveryPoints(array $request)
4441

4542
}
4643

44+
public function findDeliveryPoint($id, $country)
45+
{
46+
try {
47+
return $this->findDeliveryPoints(array(
48+
'NumPointRelais' => $id,
49+
'Pays' => $country
50+
));
51+
52+
} catch ( \SoapFault $e ) {
53+
throw new \Exception();
54+
}
55+
}
56+
4757
private function decorateRequest($request)
4858
{
4959
$key = $this->websiteId;
@@ -56,17 +66,27 @@ private function decorateRequest($request)
5666
return $request;
5767
}
5868

59-
private function checkResponse($result)
69+
private function checkResponse($method, $result)
6070
{
61-
if ($result->WSI3_PointRelais_RechercheResult->STAT != 0) {
71+
$method = $method . "Result";
72+
if ($result->{$method}->STAT != 0) {
6273
$request = $this->decorateRequest([
63-
'STAT_ID' => $result->WSI3_PointRelais_RechercheResult->STAT,
74+
'STAT_ID' => $result->{$method}->STAT,
6475
'Langue' => 'ES',
6576
]);
6677
$error_response = $this->client->WSI2_STAT_Label($request);
6778
throw new \InvalidArgumentException($error_response->WSI2_STAT_LabelResult);
6879
}
6980
}
7081

82+
private function createPoint($response) {
83+
return new Point(
84+
$response->Num,
85+
trim($response->LgAdr1),
86+
str_replace(",",".",$response->Latitude),
87+
str_replace(",",".",$response->Longitude),
88+
$response->CP
89+
);
90+
}
7191

7292
}

tests/MondialRelay/ApiClientTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,13 @@ public function itShouldReturnAnArrayOfPointsIfParametersMatch()
6767
$this->assertInstanceOf(Point::class, $point);
6868
}
6969
}
70+
71+
/**
72+
* @test
73+
*/
74+
public function itShouldReturnAValidPoint()
75+
{
76+
$point = $this->client->findDeliveryPoint('077712','ES');
77+
$this->assertInstanceOf(Point::class, $point);
78+
}
7079
}

0 commit comments

Comments
 (0)