signifly /
gls-parcel-shop
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Signifly\ParcelShop; |
||||
| 4 | |||||
| 5 | use Illuminate\Support\Collection; |
||||
| 6 | use Laminas\Soap\Client; |
||||
| 7 | use Signifly\ParcelShop\Contracts\ParcelShop as Contract; |
||||
| 8 | use Signifly\ParcelShop\Resources\ParcelShop; |
||||
| 9 | |||||
| 10 | class GLSParcelShop implements Contract |
||||
| 11 | { |
||||
| 12 | /** |
||||
| 13 | * The Soap client instance. |
||||
| 14 | * |
||||
| 15 | * @var \Laminas\Soap\Client |
||||
| 16 | */ |
||||
| 17 | protected $client; |
||||
| 18 | |||||
| 19 | /** |
||||
| 20 | * Create a new GLSParcelShop instance. |
||||
| 21 | * |
||||
| 22 | * @param \Laminas\Soap\Client $client |
||||
| 23 | */ |
||||
| 24 | public function __construct(Client $client) |
||||
| 25 | { |
||||
| 26 | $this->client = $client; |
||||
| 27 | } |
||||
| 28 | |||||
| 29 | /** |
||||
| 30 | * Instantiate using only an endpoint. |
||||
| 31 | * |
||||
| 32 | * @param string|null $endpoint |
||||
| 33 | * @return self |
||||
| 34 | */ |
||||
| 35 | public static function make(?string $endpoint = null): self |
||||
| 36 | { |
||||
| 37 | $client = new Client($endpoint ?? 'http://www.gls.dk/webservices_v4/wsShopFinder.asmx?WSDL', [ |
||||
| 38 | 'connection_timeout' => 60, |
||||
| 39 | 'keep_alive' => false, |
||||
| 40 | ]); |
||||
| 41 | |||||
| 42 | return new self($client); |
||||
| 43 | } |
||||
| 44 | |||||
| 45 | /** |
||||
| 46 | * Retrieve all parcel shops for a specific country. |
||||
| 47 | * |
||||
| 48 | * @param string $countryCode |
||||
| 49 | * @return \Illuminate\Support\Collection |
||||
| 50 | */ |
||||
| 51 | public function all(string $countryCode): Collection |
||||
| 52 | { |
||||
| 53 | $response = $this->client->GetAllParcelShops([ |
||||
| 54 | 'countryIso3166A2' => $countryCode, |
||||
| 55 | ]); |
||||
| 56 | |||||
| 57 | return $this->transformCollection( |
||||
| 58 | data_get($response, $this->getAllDataKey()) |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 59 | ); |
||||
| 60 | } |
||||
| 61 | |||||
| 62 | /** |
||||
| 63 | * Find a given parcel shop by its number. |
||||
| 64 | * |
||||
| 65 | * @param string|int $shopNumber |
||||
| 66 | * @return ParcelShop |
||||
| 67 | */ |
||||
| 68 | public function find($shopNumber): ParcelShop |
||||
| 69 | { |
||||
| 70 | $response = $this->client->GetOneParcelShop([ |
||||
| 71 | 'ParcelShopNumber' => $shopNumber, |
||||
| 72 | ]); |
||||
| 73 | |||||
| 74 | return new ParcelShop((array) $response->GetOneParcelShopResult); |
||||
| 75 | } |
||||
| 76 | |||||
| 77 | /** |
||||
| 78 | * Retrieve parcel shops near an address. |
||||
| 79 | * |
||||
| 80 | * @param string $streetName |
||||
| 81 | * @param string $zipCode |
||||
| 82 | * @param string $countryCode |
||||
| 83 | * @param int $amount |
||||
| 84 | * @return \Illuminate\Support\Collection |
||||
| 85 | */ |
||||
| 86 | public function nearest( |
||||
| 87 | string $streetName, |
||||
| 88 | string $zipCode, |
||||
| 89 | string $countryCode, |
||||
| 90 | int $amount = 5 |
||||
| 91 | ): Collection { |
||||
| 92 | $response = $this->client->SearchNearestParcelShops([ |
||||
| 93 | 'street' => $streetName, |
||||
| 94 | 'zipcode' => $zipCode, |
||||
| 95 | 'countryIso3166A2' => $countryCode, |
||||
| 96 | 'Amount' => $amount, |
||||
| 97 | ]); |
||||
| 98 | |||||
| 99 | return $this->transformCollection( |
||||
| 100 | data_get($response, $this->getNearestDataKey()) |
||||
|
0 ignored issues
–
show
It seems like
data_get($response, $this->getNearestDataKey()) can also be of type null; however, parameter $items of Signifly\ParcelShop\GLSP...::transformCollection() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 101 | ); |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | /** |
||||
| 105 | * Retrieve parcel shops within a given zip code and country code. |
||||
| 106 | * |
||||
| 107 | * @param string $zipCode |
||||
| 108 | * @param string $countryCode |
||||
| 109 | * @return \Illuminate\Support\Collection |
||||
| 110 | */ |
||||
| 111 | public function within(string $zipCode, string $countryCode): Collection |
||||
| 112 | { |
||||
| 113 | $response = $this->client->GetParcelShopsInZipcode([ |
||||
| 114 | 'zipcode' => $zipCode, |
||||
| 115 | 'countryIso3166A2' => $countryCode, |
||||
| 116 | ]); |
||||
| 117 | |||||
| 118 | $items = data_get($response, $this->getWithinDataKey()); |
||||
| 119 | |||||
| 120 | return $this->transformCollection( |
||||
| 121 | is_array($items) ? $items : [$items] |
||||
| 122 | ); |
||||
| 123 | } |
||||
| 124 | |||||
| 125 | protected function getAllDataKey(): string |
||||
| 126 | { |
||||
| 127 | return 'GetAllParcelShopsResult.PakkeshopData'; |
||||
| 128 | } |
||||
| 129 | |||||
| 130 | protected function getNearestDataKey(): string |
||||
| 131 | { |
||||
| 132 | return 'SearchNearestParcelShopsResult.parcelshops.PakkeshopData'; |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | protected function getWithinDataKey(): string |
||||
| 136 | { |
||||
| 137 | return 'GetParcelShopsInZipcodeResult.PakkeshopData'; |
||||
| 138 | } |
||||
| 139 | |||||
| 140 | protected function transformCollection(array $items): Collection |
||||
| 141 | { |
||||
| 142 | return collect($items) |
||||
| 143 | ->map(function ($item) { |
||||
| 144 | return new ParcelShop((array) $item); |
||||
| 145 | }); |
||||
| 146 | } |
||||
| 147 | } |
||||
| 148 |