Skip to content

Commit

Permalink
Merge pull request #433 from wizaplace/feature/wiz-478-add-shipping-s…
Browse files Browse the repository at this point in the history
…ervice-1.44

Feature/wiz 478 add shipping service 1.44
  • Loading branch information
wizacedric authored Jan 9, 2019
2 parents 99774a9 + bdfb2b5 commit db7f6c8
Show file tree
Hide file tree
Showing 10 changed files with 646 additions and 0 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## 1.44.8

### New features

- Added `\Wizaplace\SDK\Shipping\ShippingService::getAll`
- Added `\Wizaplace\SDK\Shipping\ShippingService::getById`
- Added `\Wizaplace\SDK\Shipping\ShippingService::put`
- Added `\Wizaplace\SDK\Shipping\ShippingRate`
- Added `\Wizaplace\SDK\Shipping\ShippingStatus`
- Added `\Wizaplace\SDK\Pim\Product\ProductService::addVideo`
- Added `\Wizaplace\SDK\Pim\Product\ProductService::deleteVideo`

## 1.44.7

### New features
Expand Down
97 changes: 97 additions & 0 deletions src/Shipping/Shipping.php
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;
}
}
42 changes: 42 additions & 0 deletions src/Shipping/ShippingRate.php
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;
}
}
113 changes: 113 additions & 0 deletions src/Shipping/ShippingService.php
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;
}
}
}
20 changes: 20 additions & 0 deletions src/Shipping/ShippingStatus.php
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';
}
103 changes: 103 additions & 0 deletions tests/Shipping/ShippingServiceTest.php
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);
}
}
Loading

0 comments on commit db7f6c8

Please sign in to comment.