Add order webhook processor

This commit is contained in:
Thibaud Fabre
2016-04-29 02:26:28 +02:00
parent 276b060c58
commit 957d588d6f
5 changed files with 150 additions and 3 deletions

View File

@@ -26,6 +26,11 @@ class WebhookEvent
const RECORD_SUBDEFS_CREATED = 'record.subdefs.created';
const RECORD_SUBDEF_TYPE = 'record.subdef';
const ORDER_TYPE = 'order';
const ORDER_CREATED = 'order.created';
const ORDER_DELIVERED = 'order.delivered';
const ORDER_DENIED = 'order.denied';
/**
* @ORM\Column(type="integer")
* @ORM\Id

View File

@@ -13,19 +13,39 @@ namespace Alchemy\Phrasea\Order\ValidationNotifier;
use Alchemy\Phrasea\Model\Entities\Order;
use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Model\Entities\WebhookEvent;
use Alchemy\Phrasea\Model\Manipulator\WebhookEventManipulator;
use Alchemy\Phrasea\Order\OrderDelivery;
use Alchemy\Phrasea\Order\ValidationNotifier;
class WebhookNotifier implements ValidationNotifier
{
/**
* @var WebhookEventManipulator
*/
private $webhookManipulator;
/**
* @param WebhookEventManipulator $webhookEventManipulator
*/
public function __construct(WebhookEventManipulator $webhookEventManipulator)
{
$this->webhookManipulator = $webhookEventManipulator;
}
/**
* @param Order $order
* @param User $recipient
*/
public function notifyCreation(Order $order, User $recipient)
{
// TODO: Implement notifyCreation() method.
$eventData = [
'order_id' => $order->getId(),
'user_id' => $recipient->getId(),
];
$this->webhookManipulator->create(WebhookEvent::ORDER_CREATED, WebhookEvent::ORDER_TYPE, $eventData);
}
/**
@@ -33,7 +53,13 @@ class WebhookNotifier implements ValidationNotifier
*/
public function notifyDelivery(OrderDelivery $delivery)
{
// TODO: Implement notifyDelivery() method.
$eventData = [
'order_id' => $delivery->getOrder()->getId(),
'admin_id' => $delivery->getAdmin()->getId(),
'quantity' => $delivery->getQuantity()
];
$this->webhookManipulator->create(WebhookEvent::ORDER_DELIVERED, WebhookEvent::ORDER_TYPE, $eventData);
}
/**
@@ -41,6 +67,12 @@ class WebhookNotifier implements ValidationNotifier
*/
public function notifyDenial(OrderDelivery $delivery)
{
// TODO: Implement notifyDenial() method.
$eventData = [
'order_id' => $delivery->getOrder()->getId(),
'admin_id' => $delivery->getAdmin()->getId(),
'quantity' => $delivery->getQuantity()
];
$this->webhookManipulator->create(WebhookEvent::ORDER_DENIED, WebhookEvent::ORDER_TYPE, $eventData);
}
}

View File

@@ -7,6 +7,7 @@ use Alchemy\Phrasea\Model\Entities\WebhookEvent;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Webhook\Processor\CallableProcessorFactory;
use Alchemy\Phrasea\Webhook\Processor\FeedEntryProcessorFactory;
use Alchemy\Phrasea\Webhook\Processor\OrderNotificationProcessorFactory;
use Alchemy\Phrasea\Webhook\Processor\ProcessorFactory;
use Alchemy\Phrasea\Webhook\Processor\UserRegistrationProcessorFactory;
@@ -25,6 +26,7 @@ class EventProcessorFactory
{
$this->registerFactory(WebhookEvent::FEED_ENTRY_TYPE, new FeedEntryProcessorFactory($app));
$this->registerFactory(WebhookEvent::USER_REGISTRATION_TYPE, new UserRegistrationProcessorFactory($app));
$this->registerFactory(WebhookEvent::ORDER_TYPE, new OrderNotificationProcessorFactory($app));
}
/**

View File

@@ -0,0 +1,79 @@
<?php
namespace Alchemy\Phrasea\Webhook\Processor;
use Alchemy\Phrasea\Model\Entities\Order;
use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Model\Entities\WebhookEvent;
use Alchemy\Phrasea\Model\Repositories\OrderRepository;
use Alchemy\Phrasea\Model\Repositories\UserRepository;
class OrderNotificationProcessor implements ProcessorInterface
{
/**
* @var OrderRepository
*/
private $orderRepository;
/**
* @var UserRepository
*/
private $userRepository;
public function __construct(OrderRepository $orderRepository, UserRepository $userRepository)
{
$this->orderRepository = $orderRepository;
$this->userRepository = $userRepository;
}
public function process(WebhookEvent $event)
{
if ($event->getName() == WebhookEvent::ORDER_CREATED) {
return $this->processCreateOrder($event);
}
return $this->processDeliveryOrder($event);
}
protected function processCreateOrder(WebhookEvent $event)
{
$data = $event->getData();
/** @var User $user */
$user = $this->userRepository->find($data['user_id']);
/** @var Order $order */
$order = $this->orderRepository->find($data['order_id']);
return $this->getOrderData($event, $user, $order);
}
protected function processDeliveryOrder(WebhookEvent $event)
{
$data = $event->getData();
/** @var Order $order */
$order = $this->orderRepository->find($data['order_id']);
$user = $order->getUser();
return $this->getOrderData($event, $user, $order);
}
/**
* @param WebhookEvent $event
* @param User $user
* @param Order $order
* @return array
*/
protected function getOrderData(WebhookEvent $event, User $user, Order $order)
{
return [
'event' => $event->getName(),
'user' => [
'id' => $user->getId(),
'email' => $user->getEmail(),
'login' => $user->getLogin()
],
'order' => $order->getId()
];
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Alchemy\Phrasea\Webhook\Processor;
use Alchemy\Phrasea\Application;
class OrderNotificationProcessorFactory implements ProcessorFactory
{
/**
* @var Application
*/
private $application;
public function __construct(Application $application)
{
$this->application = $application;
}
/**
* @return ProcessorInterface
*/
public function createProcessor()
{
return new OrderNotificationProcessor(
$this->application['repo.orders'],
$this->application['repo.users']
);
}
}