-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #433 from wizaplace/feature/wiz-478-add-shipping-s…
…ervice-1.44 Feature/wiz 478 add shipping service 1.44
- Loading branch information
Showing
10 changed files
with
646 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) Wizacha | ||
* @license Proprietary | ||
*/ | ||
declare(strict_types = 1); | ||
|
||
namespace Wizaplace\SDK\Shipping; | ||
|
||
final class Shipping | ||
{ | ||
/** @var int */ | ||
private $id; | ||
|
||
/** @var string */ | ||
private $name; | ||
|
||
/** @var string */ | ||
private $deliveryTime; | ||
|
||
/** @var boolean */ | ||
private $isEnabled; | ||
|
||
/** @var ShippingRate[]|null */ | ||
private $rates; | ||
|
||
/** @var string|null */ | ||
private $description; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @param array $data | ||
*/ | ||
public function __construct(array $data) | ||
{ | ||
$this->id = $data['shipping_id']; | ||
$this->name = $data['shipping']; | ||
$this->deliveryTime = $data['delivery_time']; | ||
$this->isEnabled = $data['status'] === "A"; | ||
|
||
if (isset($data['rates'])) { | ||
$this->rates = array_map(function ($rate) { | ||
return new ShippingRate($rate); | ||
}, $data['rates']); | ||
} | ||
$this->description = $data['description'] ?? null; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @return string example: "24h" | ||
*/ | ||
public function getDeliveryTime(): string | ||
{ | ||
return $this->deliveryTime; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isEnabled(): bool | ||
{ | ||
return $this->isEnabled; | ||
} | ||
|
||
/** | ||
* @return ShippingRate[]|null | ||
*/ | ||
public function getRates(): ?array | ||
{ | ||
return $this->rates; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getDescription(): ?string | ||
{ | ||
return $this->description; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) Wizacha | ||
* @license Proprietary | ||
*/ | ||
declare(strict_types = 1); | ||
|
||
namespace Wizaplace\SDK\Shipping; | ||
|
||
final class ShippingRate | ||
{ | ||
/** @var int */ | ||
private $amount; | ||
|
||
/** @var float|null */ | ||
private $value; | ||
|
||
/** | ||
* @param array $data | ||
*/ | ||
public function __construct(array $data) | ||
{ | ||
$this->amount = $data['amount']; | ||
$this->value = $data['value']; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getAmount(): int | ||
{ | ||
return $this->amount; | ||
} | ||
|
||
/** | ||
* @return float|null | ||
*/ | ||
public function getValue(): ?float | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) Wizacha | ||
* @license Proprietary | ||
*/ | ||
|
||
namespace Wizaplace\SDK\Shipping; | ||
|
||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\RequestOptions; | ||
use function theodorejb\polycast\to_int; | ||
use Wizaplace\SDK\AbstractService; | ||
use Wizaplace\SDK\Exception\NotFound; | ||
|
||
class ShippingService extends AbstractService | ||
{ | ||
private const ENDPOINT = "shippings"; | ||
|
||
/** | ||
* Get the list of shippings | ||
* | ||
* @return Shipping[] | ||
* @throws \Wizaplace\SDK\Authentication\AuthenticationRequired | ||
*/ | ||
public function getAll(): array | ||
{ | ||
$this->client->mustBeAuthenticated(); | ||
|
||
try { | ||
$response = $this->client->get(self::ENDPOINT); | ||
|
||
return array_map(function ($shipping) { | ||
return new Shipping($shipping); | ||
}, $response); | ||
} catch (ClientException $e) { | ||
throw $e; | ||
} | ||
} | ||
|
||
/** | ||
* Get a shipping | ||
* | ||
* @param int $id | ||
* | ||
* @return Shipping | ||
* @throws NotFound | ||
* @throws \Wizaplace\SDK\Authentication\AuthenticationRequired | ||
*/ | ||
public function getById(int $id): Shipping | ||
{ | ||
$this->client->mustBeAuthenticated(); | ||
|
||
try { | ||
$response = $this->client->get(self::ENDPOINT."/".$id); | ||
|
||
return new Shipping($response); | ||
} catch (ClientException $e) { | ||
if ($e->getResponse()->getStatusCode() === 404) { | ||
throw new NotFound("The shipping #{$id} doesn't exist", $e); | ||
} | ||
|
||
throw $e; | ||
} | ||
} | ||
|
||
/** | ||
* @param int $id | ||
* @param ShippingStatus $status | ||
* @param ShippingRate[] $rates | ||
* | ||
* @return int Shipping's id | ||
* @throws NotFound | ||
* @throws \Wizaplace\SDK\Authentication\AuthenticationRequired | ||
*/ | ||
public function put(int $id, ShippingStatus $status, array $rates): int | ||
{ | ||
$this->client->mustBeAuthenticated(); | ||
|
||
if (empty($rates)) { | ||
throw new \BadMethodCallException("You need to set a least one rate"); | ||
} | ||
|
||
if (count($rates) > 2) { | ||
throw new \BadMethodCallException("You can have only 2 rates max"); | ||
} | ||
|
||
try { | ||
$response = $this->client->put(self::ENDPOINT."/".$id, [ | ||
RequestOptions::JSON => [ | ||
"status" => $status->getValue(), | ||
"rates" => [ | ||
[ | ||
"amount" => $rates[0]->getAmount(), | ||
"value" => $rates[0]->getValue(), | ||
], | ||
[ | ||
"amount" => isset($rates[1]) ? $rates[1]->getAmount() : $rates[0]->getAmount(), | ||
"value" => isset($rates[1]) ? $rates[1]->getValue() : $rates[0]->getValue(), | ||
], | ||
], | ||
], | ||
]); | ||
|
||
return to_int($response['shipping_id']); | ||
} catch (ClientException $e) { | ||
if ($e->getResponse()->getStatusCode() === 404) { | ||
throw new NotFound("The shipping #{$id} doesn't exist", $e); | ||
} | ||
|
||
throw $e; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) Wizacha | ||
* @license Proprietary | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Wizaplace\SDK\Shipping; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
/** | ||
* @method static ShippingStatus ENABLED() | ||
* @method static ShippingStatus DISABLED() | ||
*/ | ||
final class ShippingStatus extends Enum | ||
{ | ||
private const ENABLED = 'A'; | ||
private const DISABLED = 'D'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) Wizacha | ||
* @license Proprietary | ||
*/ | ||
declare(strict_types = 1); | ||
|
||
namespace Wizaplace\SDK\Tests\Shipping; | ||
|
||
use Wizaplace\SDK\Exception\NotFound; | ||
use Wizaplace\SDK\Shipping\Shipping; | ||
use Wizaplace\SDK\Shipping\ShippingRate; | ||
use Wizaplace\SDK\Shipping\ShippingService; | ||
use Wizaplace\SDK\Shipping\ShippingStatus; | ||
use Wizaplace\SDK\Tests\ApiTestCase; | ||
|
||
final class ShippingServiceTest extends ApiTestCase | ||
{ | ||
public function testGetAllShippings() | ||
{ | ||
$shippingService = $this->buildShippingService(); | ||
|
||
foreach ($shippingService->getAll() as $shipping) { | ||
$this->assertInstanceOf(Shipping::class, $shipping); | ||
$this->assertNotNull($shipping->getId()); | ||
$this->assertNotNull($shipping->getName()); | ||
$this->assertNotNull($shipping->getDeliveryTime()); | ||
$this->assertNotNull($shipping->isEnabled()); | ||
$this->assertNull($shipping->getRates()); | ||
$this->assertNull($shipping->getDescription()); | ||
} | ||
} | ||
|
||
public function testGetAShipping() | ||
{ | ||
$shippingService = $this->buildShippingService(); | ||
|
||
$shipping = $shippingService->getById(1); | ||
|
||
$this->assertInstanceOf(Shipping::class, $shipping); | ||
$this->assertNotNull($shipping->getId()); | ||
$this->assertNotNull($shipping->getName()); | ||
$this->assertNotNull($shipping->getDeliveryTime()); | ||
$this->assertNotNull($shipping->isEnabled()); | ||
$this->assertNotNull($shipping->getRates()); | ||
foreach ($shipping->getRates() as $rate) { | ||
$this->assertInstanceOf(ShippingRate::class, $rate); | ||
$this->assertNotNull($rate->getAmount()); | ||
$this->assertNotNull($rate->getValue()); | ||
} | ||
$this->assertNotNull($shipping->getDescription()); | ||
} | ||
|
||
public function testGetANotFoundShipping() | ||
{ | ||
$shippingService = $this->buildShippingService(); | ||
|
||
$this->expectException(NotFound::class); | ||
$shippingService->getById(123456789); | ||
} | ||
|
||
public function testPutAShipping() | ||
{ | ||
$shippingService = $this->buildShippingService(); | ||
|
||
$id = $shippingService->put(1, ShippingStatus::DISABLED(), [ | ||
new ShippingRate([ | ||
'amount' => 0, | ||
'value' => 10, | ||
]), | ||
new ShippingRate([ | ||
'amount' => 1, | ||
'value' => 10, | ||
]), | ||
]); | ||
$this->assertSame(1, $id); | ||
|
||
|
||
$shipping = $shippingService->getById(1); | ||
|
||
$this->assertInstanceOf(Shipping::class, $shipping); | ||
$this->assertNotNull($shipping->getId()); | ||
$this->assertNotNull($shipping->getName()); | ||
$this->assertNotNull($shipping->getDeliveryTime()); | ||
$this->assertFalse($shipping->isEnabled()); | ||
$this->assertNotNull($shipping->getRates()); | ||
$this->assertInstanceOf(ShippingRate::class, $shipping->getRates()[0]); | ||
$this->assertInstanceOf(ShippingRate::class, $shipping->getRates()[1]); | ||
$this->assertSame(0, $shipping->getRates()[0]->getAmount()); | ||
$this->assertSame(1, $shipping->getRates()[1]->getAmount()); | ||
$this->assertEquals(10, $shipping->getRates()[0]->getValue()); | ||
$this->assertEquals(10, $shipping->getRates()[1]->getValue()); | ||
$this->assertNotNull($shipping->getDescription()); | ||
} | ||
|
||
private function buildShippingService($userEmail = '[email protected]', $userPassword = 'password'): ShippingService | ||
{ | ||
$apiClient = $this->buildApiClient(); | ||
$apiClient->authenticate($userEmail, $userPassword); | ||
|
||
return new ShippingService($apiClient); | ||
} | ||
} |
Oops, something went wrong.