OrderRequest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Rest\Request;
  6. use Temando\Shipping\Rest\Adapter\OrderApiInterface;
  7. use Temando\Shipping\Rest\Request\Type\OrderRequestTypeInterface;
  8. /**
  9. * Temando API Order Operation Parameters
  10. *
  11. * @package Temando\Shipping\Rest
  12. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  13. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  14. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  15. * @link http://www.temando.com/
  16. */
  17. class OrderRequest
  18. {
  19. /**
  20. * @var OrderRequestTypeInterface
  21. */
  22. private $order;
  23. /**
  24. * @var string
  25. */
  26. private $action;
  27. /**
  28. * OrderRequest constructor.
  29. *
  30. * @param OrderRequestTypeInterface $order
  31. * @param string $action
  32. */
  33. public function __construct(
  34. OrderRequestTypeInterface $order,
  35. $action = OrderApiInterface::ACTION_CREATE
  36. ) {
  37. $this->order = $order;
  38. $this->action = $action;
  39. }
  40. /**
  41. * @return string[]
  42. */
  43. public function getPathParams()
  44. {
  45. // create
  46. if (!$this->order->getId()) {
  47. return [];
  48. }
  49. // update
  50. return [
  51. $this->order->getId(),
  52. ];
  53. }
  54. /**
  55. * @return string[]
  56. */
  57. public function getRequestParams()
  58. {
  59. switch ($this->action) {
  60. case OrderApiInterface::ACTION_ALLOCATE_PICKUP:
  61. case OrderApiInterface::ACTION_ALLOCATE_SHIPMENT:
  62. // create with shipment / pickup fulfillment
  63. return [
  64. 'action' => $this->action,
  65. 'experience' => $this->order->getSelectedExperienceCode()
  66. ];
  67. case OrderApiInterface::ACTION_CREATE:
  68. // regular create
  69. return ['experience' => $this->order->getSelectedExperienceCode()];
  70. default:
  71. // update
  72. return [];
  73. }
  74. }
  75. /**
  76. * @return string
  77. */
  78. public function getRequestBody()
  79. {
  80. return json_encode($this->order, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  81. }
  82. }