Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Update and Delete operations to Orders #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/Components/Operations/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Afosto\ShopCtrl\Components\Operations;

use Afosto\ShopCtrl\Components\App;
use Afosto\ShopCtrl\Helpers\Exceptions\ApiException;
use GuzzleHttp\Exception\ClientException;
use Psr\Http\Message\ResponseInterface;

trait Delete
{

/**
* @param ResponseInterface $response
*/
abstract protected function validateResponse(ResponseInterface $response);

/**
* @return string
*/
abstract protected function getMethod();

/**
* @param null $id
*
* @throws ApiException
*/
public function delete($id = null)
{
try {
$response = App::getInstance()->getClient()->delete($this->deleteUri($id));
} catch (ClientException $previous) {
$e = new ApiException((string)$previous->getResponse()->getBody());
$e->exception = $previous;
throw $e;
}

return $response;
}

/**
* @param $id
*
* @return string
*/
protected function deleteUri($id)
{
return 'v1/' . $this->getMethod() . '/' . $id;
}

}
53 changes: 53 additions & 0 deletions src/Components/Operations/Update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Afosto\ShopCtrl\Components\Operations;

use Afosto\ShopCtrl\Components\App;
use Afosto\ShopCtrl\Helpers\Exceptions\ApiException;
use GuzzleHttp\Exception\ClientException;
use Psr\Http\Message\ResponseInterface;

trait Update
{

/**
* @param ResponseInterface $response
*/
abstract protected function validateResponse(ResponseInterface $response);

/**
* @return string
*/
abstract protected function getMethod();

/**
* @param null $id
* @param null $uri
*
* @throws ApiException
*/
public function update()
{
try {
$response = App::getInstance()
->getClient()
->put($this->updateUri(), ['json' => $this->getModel()]);
} catch (ClientException $previous) {
$e = new ApiException((string)$previous->getResponse()->getBody());
$e->exception = $previous;
throw $e;
}
return $response;
}

/**
* @param $id
*
* @return string
*/
protected function updateUri()
{
return 'v1/' . $this->getMethod();
}

}
8 changes: 7 additions & 1 deletion src/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Afosto\ShopCtrl\Components\Model;
use Afosto\ShopCtrl\Components\Operations\Create;
use Afosto\ShopCtrl\Components\Operations\Delete;
use Afosto\ShopCtrl\Components\Operations\Find;
use Afosto\ShopCtrl\Components\Operations\FindAll;
use Afosto\ShopCtrl\Components\App;
use Afosto\ShopCtrl\Components\Operations\Update;

/**
* @property boolean $recalculateTotalsOnSave Gets or sets a value indicating whether we should recalculate the totals.
Expand Down Expand Up @@ -63,7 +65,7 @@ class Order extends Model
{
const REVIEW_DISABLE_INVITE = -3;

use Find, FindAll, Create;
use Find, FindAll, Create, Update, Delete;

public function getMap()
{
Expand Down Expand Up @@ -193,4 +195,8 @@ protected function createUri()
return $this->findAllUri();
}

protected function updateUri()
{
return 'v1/Orders';
}
}