Allow for easily sending multiple order items

This allows for the sending of multiple items in a single order, by
adding a new `OrderItem` class and moving all of the order item specific
fields from the `Order` to an individual item.

These can then be assigned to an order using the `withOrderItems()`
method as an array of order items.

Fixes #19
This commit is contained in:
Oliver Davies 2020-06-08 19:33:49 +01:00
parent 0ed99797f5
commit ae766763b2
9 changed files with 86 additions and 53 deletions

View file

@ -8,6 +8,7 @@ use Opdavies\Glassboxx\Enum\InteractionType;
use Opdavies\Glassboxx\Traits\UsesAuthTokenTrait;
use Opdavies\Glassboxx\Traits\UsesCreatedAtTrait;
use Opdavies\Glassboxx\ValueObject\OrderInterface;
use Opdavies\Glassboxx\ValueObject\OrderItem;
use RuntimeException;
class OrderRequest extends AbstractRequest implements OrderRequestInterface
@ -18,6 +19,9 @@ class OrderRequest extends AbstractRequest implements OrderRequestInterface
/** @var OrderInterface|null */
private $order;
/** @var OrderItem[] */
private $orderItems = [];
public function forOrder(OrderInterface $order): AbstractRequest
{
$this->order = $order;
@ -25,6 +29,13 @@ class OrderRequest extends AbstractRequest implements OrderRequestInterface
return $this;
}
public function withOrderItems(array $orderItems): AbstractRequest
{
$this->orderItems = $orderItems;
return $this;
}
public function execute(): string
{
if (!$this->config) {
@ -39,9 +50,10 @@ class OrderRequest extends AbstractRequest implements OrderRequestInterface
throw new RuntimeException('There is no customer');
}
$body = [
'items' => [
[
$body = [];
foreach ($this->orderItems as $orderItem) {
$body['items'][] = [
'created_at' => $this->getCreatedAtDate(),
'currency_code' => $this->order->getCurrencyCode(),
'customer_email' => $this->order->getCustomer()->getEmailAddress(),
@ -51,12 +63,11 @@ class OrderRequest extends AbstractRequest implements OrderRequestInterface
'duration_for_loan' => 0,
'hostname' => $this->config->getVendorId(),
'original_order_number' => $this->order->getOrderNumber(),
'price_incl_tax' => $this->order->getPrice(),
'sku' => $this->order->getSku(),
'price_incl_tax' => $orderItem->getPrice(),
'sku' => $orderItem->getSku(),
'type_of_interaction' => InteractionType::PURCHASE,
],
],
];
}
$response = $this->client->request(
'POST',

View file

@ -12,5 +12,7 @@ interface OrderRequestInterface
public function forOrder(OrderInterface $order): AbstractRequest;
public function withOrderItems(array $orderItems): AbstractRequest;
public function execute(): string;
}

View file

@ -9,30 +9,20 @@ final class Order implements OrderInterface
/** @var CustomerInterface */
private $customer;
/** @var string */
private $sku;
/** @var string */
private $orderNumber;
/** @var string */
private $currencyCode;
/** @var float */
private $price;
public function __construct(
CustomerInterface $customer,
string $sku,
string $orderNumber,
string $currencyCode,
float $price
string $currencyCode
) {
$this->customer = $customer;
$this->sku = $sku;
$this->orderNumber = $orderNumber;
$this->currencyCode = $currencyCode;
$this->price = $price;
}
public function getCurrencyCode(): string
@ -49,14 +39,4 @@ final class Order implements OrderInterface
{
return $this->orderNumber;
}
public function getPrice(): float
{
return $this->price;
}
public function getSku(): string
{
return $this->sku;
}
}

View file

@ -6,10 +6,8 @@ interface OrderInterface
{
public function __construct(
CustomerInterface $customer,
string $sku,
string $orderNumber,
string $currencyCode,
float $price
string $currencyCode
);
public function getCurrencyCode(): string;
@ -17,8 +15,4 @@ interface OrderInterface
public function getCustomer(): CustomerInterface;
public function getOrderNumber(): string;
public function getPrice(): float;
public function getSku(): string;
}

View file

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Opdavies\Glassboxx\ValueObject;
class OrderItem implements OrderItemInterface
{
/** @var string */
private $sku;
/** @var float */
private $price;
public function __construct(float $price, string $sku)
{
$this->price = $price;
$this->sku = $sku;
}
public function getPrice(): float
{
return $this->price;
}
public function getSku(): string
{
return $this->sku;
}
}

View file

@ -0,0 +1,10 @@
<?php
namespace Opdavies\Glassboxx\ValueObject;
interface OrderItemInterface
{
public function getPrice(): float;
public function getSku(): string;
}

View file

@ -7,6 +7,7 @@ namespace Opdavies\Glassboxx\Tests\Glassboxx\Request;
use DateTime;
use Opdavies\Glassboxx\Request\OrderRequest;
use Opdavies\Glassboxx\Tests\Glassboxx\TestCase;
use Opdavies\Glassboxx\ValueObject\OrderItemInterface;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Contracts\HttpClient\ResponseInterface;
@ -56,6 +57,7 @@ final class OrderRequestTest extends TestCase
$request = (new OrderRequest($client))
->forOrder($this->getMockOrder())
->withOrderItems([$this->getMockOrderItem()])
->withAuthToken($authTokenRequest->getToken())
->withConfig($this->config)
->setCreatedDate('2020-06-04 12:00:00');
@ -63,4 +65,14 @@ final class OrderRequestTest extends TestCase
// A successful response returns the original body.
$this->assertSame(json_encode($body), $request->execute());
}
private function getMockOrderItem(): OrderItemInterface
{
$orderItem = $this->getMockBuilder(OrderItemInterface::class)
->getMock();
$orderItem->method('getPrice')->willReturn(7.99);
$orderItem->method('getSku')->willReturn('this-is-the-first-sku');
return $orderItem;
}
}

View file

@ -60,8 +60,6 @@ class TestCase extends \PHPUnit\Framework\TestCase
$order->method('getCurrencyCode')->willReturn('GBP');
$order->method('getCustomer')->willReturn($this->getMockCustomer());
$order->method('getOrderNumber')->willReturn('abc123');
$order->method('getPrice')->willReturn(7.99);
$order->method('getSku')->willReturn('this-is-the-first-sku');
return $order;
}

View file

@ -19,16 +19,12 @@ class OrderTest extends TestCase
$order = new Order(
$customer,
'this-is-the-sku',
'123',
'GBP',
10.00
'GBP'
);
$this->assertSame('GBP', $order->getCurrencyCode());
$this->assertSame($customer, $order->getCustomer());
$this->assertSame('123', $order->getOrderNumber());
$this->assertSame(10.0, $order->getPrice());
$this->assertSame('this-is-the-sku', $order->getSku());
}
}