Commit 8098495b authored by Ján Forgáč's avatar Ján Forgáč

OrderMethods refactoring, abstract Ancestor added

parent d9e4ef36
......@@ -7,28 +7,26 @@
namespace ToptransApiWrapper;
use ToptransApiWrapper\Constants\OrderApiArrayKeys;
use ToptransApiWrapper\Entities\Order;
use ToptransApiWrapper\Responses;
class OrderListMethod
class OrderListMethod extends OrderMethod
{
use OrderMethodTrait;
const REQUEST_PATH = '/order/list';
const ALLOWED_PARAMETERS = [];
/** @var Order */
private $order;
public function __construct(Order $order)
{
$this->order = $order;
}
/**
* @param Request $request
* @return Responses\OrderListResponse
* @throws Exceptions\BadResponseException
* @throws Exceptions\ResponseStatusException
*/
public function sendRequest(Request $request): Responses\OrderListResponse
{
$response = $request->sentRequest(OrderConverter::orderToArray($this->order, self::ALLOWED_PARAMETERS), self::REQUEST_PATH);
$response = parent::sendRequest($request);
return new Responses\OrderListResponse($response);
}
......
<?php
/**
* This file is part of toptrans-api-wrapper.
* Copyright © 2020 Ján Forgáč <forgac@artfocus.cz>
*/
namespace ToptransApiWrapper;
use ToptransApiWrapper\Entities\Order;
abstract class OrderMethod
{
/** @var Order */
protected $order;
public function __construct(Order $order)
{
$this->order = $order;
}
/**
* @param Request $request
* @throws Exceptions\BadResponseException
* @throws Exceptions\ResponseStatusException
* @return array
*/
public function sendRequest(Request $request)
{
try {
return $request->sendRequest(OrderConverter::orderToArray($this->order, $this->getAllowedParameters()), $this->getRequestPath());
} catch (\ToptransApiWrapper\Exceptions\InvalidArgumentException $e) {
return [
'errors' => [
$e->getMessage(),
],
];
}
}
abstract protected function getRequestPath(): string;
abstract protected function getAllowedParameters(): array;
}
<?php
/**
* This file is part of toptrans-api-wrapper.
* Copyright © 2020 Ján Forgáč <forgac@artfocus.cz>
*/
namespace ToptransApiWrapper;
use ToptransApiWrapper\Entities\Order;
trait OrderMethodTrait
{
protected function getRequestPath(): string
{
return self::REQUEST_PATH;
}
protected function getAllowedParameters(): array
{
return self::ALLOWED_PARAMETERS;
}
}
......@@ -7,12 +7,13 @@
namespace ToptransApiWrapper;
use ToptransApiWrapper\Entities\Order;
use ToptransApiWrapper\Responses;
class OrderPriceMethod
class OrderPriceMethod extends OrderMethod
{
use OrderMethodTrait;
const REQUEST_PATH = '/order/price';
const ALLOWED_PARAMETERS = [
......@@ -37,17 +38,9 @@ class OrderPriceMethod
'm3',
];
/** @var Order */
private $order;
public function __construct(Order $order)
{
$this->order = $order;
}
public function sendRequest(Request $request): Responses\OrderPriceResponse
{
$response = $request->sentRequest(OrderConverter::orderToArray($this->order, self::ALLOWED_PARAMETERS), self::REQUEST_PATH);
$response = parent::sendRequest($request);
return new Responses\OrderPriceResponse($response);
}
......
......@@ -8,27 +8,20 @@
namespace ToptransApiWrapper;
use ToptransApiWrapper\Constants\OrderApiArrayKeys;
use ToptransApiWrapper\Entities\Order;
use ToptransApiWrapper\Responses;
class OrderSaveMethod
class OrderSaveMethod extends OrderMethod
{
use OrderMethodTrait;
const REQUEST_PATH = '/order/save';
const ALLOWED_PARAMETERS = OrderApiArrayKeys::ALL_API_KEYS;
/** @var Order */
private $order;
public function __construct(Order $order)
{
$this->order = $order;
}
public function sendRequest(Request $request): Responses\OrderSaveResponse
{
$response = $request->sentRequest(OrderConverter::orderToArray($this->order, self::ALLOWED_PARAMETERS), self::REQUEST_PATH);
$response = parent::sendRequest($request);
return new Responses\OrderSaveResponse($response);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment