Skip to content

Commit

Permalink
Add endpoint for updating order status (#47)
Browse files Browse the repository at this point in the history
* Create UpdateOrderRequestBody

* Create UpdateOrderRequestBodyNormalizer

* Add updateOrder to DealsResource

* Register UpdateOrderRequestBodyNormalizer
  • Loading branch information
Andriichello authored Nov 28, 2022
1 parent f49d66f commit a03bfa2
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
44 changes: 44 additions & 0 deletions php/src/Snagshout/Promote/Model/UpdateOrderRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* Copyright 2016-2022, Snagshout <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This file is part of the Merchant package
*/

namespace Snagshout\Promote\Model;

class UpdateOrderRequestBody
{
/**
* `status` of the order.
*
* @var string
*/
protected $status;
/**
* Get `status` of the order.
*
* @return string|null
*/
public function getStatus() : ?string
{
return $this->status;
}
/**
* Set `status` of the order.
*
* @param string|null $status
*
* @return self
*/
public function setStatus(?string $status) : self
{
$this->status = $status;

return $this;
}
}
1 change: 1 addition & 0 deletions php/src/Snagshout/Promote/Normalizer/NormalizerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static function create()
$normalizers[] = new SyncEmailForOrdersRequestBodyNormalizer();
$normalizers[] = new CreateSurveyReviewRequestBodyNormalizer();
$normalizers[] = new UpdateReviewNameBodyNormalizer();
$normalizers[] = new UpdateOrderRequestBodyNormalizer();

return $normalizers;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* Copyright 2016-2022, Snagshout <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This file is part of the Merchant package
*/

namespace Snagshout\Promote\Normalizer;

use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer;

class UpdateOrderRequestBodyNormalizer extends SerializerAwareNormalizer implements DenormalizerInterface, NormalizerInterface
{
public function supportsDenormalization($data, $type, $format = null)
{
if ($type !== 'Snagshout\\Promote\\Model\\UpdateOrderRequestBody') {
return false;
}

return true;
}
public function supportsNormalization($data, $format = null)
{
if ($data instanceof \Snagshout\Promote\Model\UpdateOrderRequestBody) {
return true;
}

return false;
}
public function denormalize($data, $class, $format = null, array $context = [])
{
$object = new \Snagshout\Promote\Model\UpdateOrderRequestBody();
if (property_exists($data, 'status')) {
$object->setStatus($data->{'status'});
}

return $object;
}
public function normalize($object, $format = null, array $context = [])
{
$data = new \stdClass();
if (null !== $object->getStatus()) {
$data->{'status'} = $object->getStatus();
}

return $data;
}
}
41 changes: 41 additions & 0 deletions php/src/Snagshout/Promote/Resource/DealsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,47 @@ public function createOrder($campaign, \Snagshout\Promote\Model\CreateOrderReque

return $response;
}
/**
*
*
* @param int $order ID of an order to fetch
* @param \Snagshout\Promote\Model\UpdateOrderRequestBody $body
* @param array $parameters List of parameters
* @param string $fetch Fetch mode (object or response)
*
* @return \Psr\Http\Message\ResponseInterface|\Snagshout\Promote\Model\Payload|\Snagshout\Promote\Model\Error
*/
public function updateOrder($order, \Snagshout\Promote\Model\UpdateOrderRequestBody $body, $parameters = [], $fetch = self::FETCH_OBJECT)
{
$queryParam = new QueryParam();
$url = '/api/v1/deals/orders/{order}';
$url = str_replace('{order}', urlencode($order), $url);
$url = $url . ('?' . $queryParam->buildQueryString($parameters));
$headers = array_merge(['Host' => 'localhost'], $queryParam->buildHeaders($parameters));
$body = $this->serializer->serialize($body, 'json');
$request = $this->messageFactory->createRequest('PATCH', $url, $headers, $body);
$promise = $this->httpClient->sendAsyncRequest($request);
if (self::FETCH_PROMISE === $fetch) {
return $promise;
}
$response = $promise->wait();
if (self::FETCH_OBJECT == $fetch) {
if ('200' == $response->getStatusCode()) {
return $this->serializer->deserialize((string) $response->getBody(), 'Snagshout\\Promote\\Model\\Payload', 'json');
}
if ('404' == $response->getStatusCode()) {
return $this->serializer->deserialize((string) $response->getBody(), 'Snagshout\\Promote\\Model\\Error', 'json');
}
if ('409' == $response->getStatusCode()) {
return $this->serializer->deserialize((string) $response->getBody(), 'Snagshout\\Promote\\Model\\Error', 'json');
}
if ('422' == $response->getStatusCode()) {
return $this->serializer->deserialize((string) $response->getBody(), 'Snagshout\\Promote\\Model\\Error', 'json');
}
}

return $response;
}
/**
*
*
Expand Down

0 comments on commit a03bfa2

Please sign in to comment.