feat(webservice): Started on api endpoint
Using specific management and the WebserviceSpecificManagementInterface contract to tap into the webservice api.
This commit is contained in:
80
classes/WebserviceSpecificManagementOrderreference.php
Normal file
80
classes/WebserviceSpecificManagementOrderreference.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
// You can do api/order_reference?id_order=12 or api/order_reference?id_cart=12
|
||||
|
||||
class WebserviceSpecificManagementOrderreference implements WebserviceSpecificManagementInterface
|
||||
{
|
||||
private $objectOutput;
|
||||
private $wsObject;
|
||||
private ?array $result = null;
|
||||
|
||||
public function setObjectOutput(WebserviceOutputBuilder $objectOutput)
|
||||
{
|
||||
$this->objectOutput = $objectOutput;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getObjectOutput()
|
||||
{
|
||||
return $this->objectOutput;
|
||||
}
|
||||
|
||||
public function setWsObject(WebserviceRequest $wsObject)
|
||||
{
|
||||
$this->wsObject = $wsObject;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getWsObject()
|
||||
{
|
||||
return $this->wsObject;
|
||||
}
|
||||
|
||||
public function manage()
|
||||
{
|
||||
$fragments = $this->wsObject ? $this->wsObject->urlFragments : [];
|
||||
|
||||
if (isset($fragments['id_order'])) {
|
||||
$this->result = $this->fetchByOrderId((int) $fragments['id_order']);
|
||||
} elseif (isset($fragments['id_cart'])) {
|
||||
$this->result = $this->fetchByCartId((int) $fragments['id_cart']);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function fetchByCartId(int $cartId): ?array
|
||||
{
|
||||
$row = Db::getInstance()->getRow(
|
||||
'SELECT id, id_cart, order_reference FROM ' . _DB_PREFIX_ . 'order_reference WHERE id_cart = ' . (int) $cartId
|
||||
);
|
||||
|
||||
return $row ?: null;
|
||||
}
|
||||
|
||||
private function fetchByOrderId(int $orderId): ?array
|
||||
{
|
||||
$idCart = Db::getInstance()->getValue(
|
||||
'SELECT id_cart FROM ' . _DB_PREFIX_ . 'orders WHERE id_order = ' . (int) $orderId
|
||||
);
|
||||
|
||||
if ($idCart) {
|
||||
return $this->fetchByCartId((int) $idCart);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getContent()
|
||||
{
|
||||
$xml = new SimpleXMLElement('<order_references/>');
|
||||
|
||||
if ($this->result !== null) {
|
||||
$node = $xml->addChild('order_reference');
|
||||
$node->addChild('id', (string) $this->result['id']);
|
||||
$node->addChild('id_cart', (string) $this->result['id_cart']);
|
||||
$node->addChild('reference', htmlspecialchars($this->result['order_reference']));
|
||||
}
|
||||
|
||||
return $xml->asXML();
|
||||
}
|
||||
}
|
||||
18
classes/config/services.yml
Normal file
18
classes/config/services.yml
Normal file
@@ -0,0 +1,18 @@
|
||||
services:
|
||||
_defaults:
|
||||
autowire: true
|
||||
autoconfigure: true
|
||||
|
||||
# Repository - this makes it available via dependency injection
|
||||
Module\WsOrderreference\Repository\OrderReferenceRepository:
|
||||
public: true
|
||||
factory: ["@doctrine.orm.default_entity_manager", getRepository]
|
||||
arguments:
|
||||
- Module\WsOrderreference\Entity\OrderReference
|
||||
|
||||
# Database installer
|
||||
Module\WsOrderreference\Database\ReferenceInstaller:
|
||||
public: true
|
||||
arguments:
|
||||
- "@doctrine.dbal.default_connection"
|
||||
- "%database_prefix%"
|
||||
@@ -15,7 +15,8 @@
|
||||
"Module\\WsOrderreference\\": "src/"
|
||||
},
|
||||
"classmap": [
|
||||
"ws_orderreference.php"
|
||||
"ws_orderreference.php",
|
||||
"classes/"
|
||||
],
|
||||
"exclude-from-classmap": []
|
||||
},
|
||||
|
||||
@@ -3,6 +3,8 @@ services:
|
||||
autowire: true
|
||||
autoconfigure: true
|
||||
|
||||
Module\WsOrderreference\Controller\:
|
||||
resource: "../src/Controller/*"
|
||||
# Repository - this makes it available via dependency injection
|
||||
Module\WsOrderreference\Repository\OrderReferenceRepository:
|
||||
public: true
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
namespace Module\WsOrderreference\Controller;
|
||||
|
||||
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
|
||||
class OrderReferenceController extends FrameworkBundleAdminController
|
||||
{
|
||||
public function getReferenceAction(int $id_order)
|
||||
{
|
||||
// Dummy data for testing
|
||||
|
||||
return new JsonResponse([
|
||||
'orderId' => $id_order,
|
||||
'customReference' => 'DUMMY-REF-12345',
|
||||
'status' => 'This is a test response'
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ trait Install
|
||||
&& parent::install()
|
||||
&& $this->registerHook('displayShoppingCart')
|
||||
&& $this->registerHook('displayPDFDeliverySlip')
|
||||
&& $this->registerHook('addWebserviceResources')
|
||||
&& $this->registerHook('actionFrontControllerSetMedia');
|
||||
}
|
||||
|
||||
|
||||
@@ -22,4 +22,19 @@ trait actionHooks
|
||||
['media' => 'all', 'priority' => 150]
|
||||
);
|
||||
}
|
||||
// Register in web service using specific management to bypass objectmodel handling and return custom data.
|
||||
// I used specific_management because otherwise I'd have to make an objectmodel which conflicts with the doctrine entity I made.
|
||||
// Instead of having to manage two places where the order reference is handled, I can just use the specific management to return the data from the db directly.
|
||||
|
||||
// See classes/webservice/WebserviceSpecificManagementInterface.php for the interface contract.
|
||||
|
||||
public function hookAddWebserviceResources($params)
|
||||
{
|
||||
return [
|
||||
'order_reference' => [
|
||||
'description' => 'Order references from the ws_orderreference module',
|
||||
'specific_management' => true,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,27 @@ class OrderReferenceRepository extends EntityRepository
|
||||
return $this->findOneBy(['idCart' => $cartId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find order reference entity by order ID (via cart lookup)
|
||||
*
|
||||
* @param int $orderId
|
||||
* @return OrderReference|null
|
||||
*/
|
||||
public function findByOrderId(int $orderId): ?OrderReference
|
||||
{
|
||||
$conn = $this->getEntityManager()->getConnection();
|
||||
$sql = 'SELECT id_cart FROM ' . _DB_PREFIX_ . 'orders WHERE id_order = :orderId';
|
||||
$stmt = $conn->prepare($sql);
|
||||
$resultSet = $stmt->executeQuery(['orderId' => $orderId]);
|
||||
$idCart = $resultSet->fetchOne();
|
||||
|
||||
if ($idCart) {
|
||||
return $this->findByCartId((int) $idCart);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Create a function that accepts an order Id and uses the cart ID to check if there is a custom order reference.
|
||||
// Requires an order ID as a parameter.
|
||||
// Returns the order reference if found, otherwise returns null.
|
||||
@@ -39,7 +60,7 @@ class OrderReferenceRepository extends EntityRepository
|
||||
public function customRefByOrderId(int $orderId): ?string
|
||||
{
|
||||
$conn = $this->getEntityManager()->getConnection();
|
||||
$sql = 'SELECT id_cart FROM ps_orders WHERE id_order = :orderId';
|
||||
$sql = 'SELECT id_cart FROM ' . _DB_PREFIX_ . 'orders WHERE id_order = :orderId';
|
||||
$stmt = $conn->prepare($sql);
|
||||
$resultSet = $stmt->executeQuery(['orderId' => $orderId]);
|
||||
$idCart = $resultSet->fetchOne();
|
||||
|
||||
@@ -13,6 +13,8 @@ if (file_exists(__DIR__ . '/vendor/autoload.php')) {
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/classes/WebserviceSpecificManagementOrderreference.php';
|
||||
|
||||
class Ws_OrderReference extends Module
|
||||
{
|
||||
/** @var OrderReferenceRepository */
|
||||
|
||||
Reference in New Issue
Block a user