Skip to content

Commit

Permalink
Refactoring of models
Browse files Browse the repository at this point in the history
  • Loading branch information
baldurrensch committed Feb 4, 2017
1 parent c760c91 commit 0378e2c
Show file tree
Hide file tree
Showing 10 changed files with 520 additions and 315 deletions.
18 changes: 7 additions & 11 deletions Client.php
Expand Up @@ -4,6 +4,8 @@

use LAShowroom\TaxJarBundle\Model\Order;
use LAShowroom\TaxJarBundle\Model\Response\TaxResponse;
use LAShowroom\TaxJarBundle\Model\Tax\TaxRequest;
use LAShowroom\TaxJarBundle\Model\Transaction\OrderTransaction;
use Psr\Cache\CacheItemPoolInterface;
use TaxJar\Client as TaxJarClient;

Expand All @@ -28,15 +30,13 @@ public function __construct(TaxJarClient $apiClient)
}

/**
* @param Order $order
* @param TaxRequest $order
*
* @return TaxResponse
*/
public function getTaxesForOrder(Order $order)
public function getTaxesForOrder(TaxRequest $order)
{
$request = $order->toArray();
unset($request['transaction_id']);
unset($request['transaction_date']);

if (null === $this->cacheItemPool) {
return new TaxResponse($this->apiClient->taxForOrder($request));
Expand All @@ -55,17 +55,13 @@ public function getTaxesForOrder(Order $order)
}

/**
* @param Order $order
* @param OrderTransaction $orderTransaction
*
* @return TaxResponse
*/
public function createOrderTransaction(Order $order)
public function createOrderTransaction(OrderTransaction $orderTransaction)
{
$request = $order->toArray();

$response = new TaxResponse($this->apiClient->createOrder($request));

return $response;
return new TaxResponse($this->apiClient->createOrder($orderTransaction->toArray()));
}

/**
Expand Down
116 changes: 116 additions & 0 deletions Model/BaseRequest.php
@@ -0,0 +1,116 @@
<?php

namespace LAShowroom\TaxJarBundle\Model;

use Webmozart\Assert\Assert;

class BaseRequest
{
/**
* @var Address
*/
protected $fromAddress;

/**
* @var Address
*/
protected $toAddress;

/**
* @var float
*/
protected $amount;

/**
* @var float
*/
protected $shipping;

/**
* @return Address
*/
public function getFromAddress()
{
return $this->fromAddress;
}

/**
* @param Address $fromAddress
*/
public function setFromAddress(Address $fromAddress)
{
$this->fromAddress = $fromAddress;
}

/**
* @return Address
*/
public function getToAddress()
{
return $this->toAddress;
}

/**
* @param Address $toAddress
*/
public function setToAddress(Address $toAddress)
{
$this->toAddress = $toAddress;
}

/**
* @return float
*/
public function getAmount()
{
return $this->amount;
}

/**
* @param float $amount
*/
public function setAmount($amount)
{
Assert::float($amount);

$this->amount = $amount;
}

/**
* @return float
*/
public function getShipping()
{
return $this->shipping;
}

/**
* @param float $shipping
*/
public function setShipping($shipping)
{
Assert::float($shipping);

$this->shipping = $shipping;
}

protected function toArray()
{
$result = [];

if (!empty($this->fromAddress)) {
$result = array_merge($result, $this->fromAddress->toArray('from_'));
}

if (!empty($this->toAddress)) {
$result = array_merge($result, $this->toAddress->toArray('to_'));
}

$result = array_merge($result, [
'amount' => $this->getAmount(),
'shipping' => $this->getShipping(),
]);

return $result;
}
}
2 changes: 1 addition & 1 deletion Model/LineItem.php
Expand Up @@ -38,7 +38,7 @@ class LineItem
* @param float $unitPrice
* @param float $discount
*/
public function __construct($id, $quantity, $productTaxCode, $unitPrice, $discount)
public function __construct($id, $quantity, $productTaxCode, $unitPrice, $discount = 0.0)
{
Assert::integer($quantity);
Assert::float($unitPrice);
Expand Down

0 comments on commit 0378e2c

Please sign in to comment.