Skip to content

Instantly share code, notes, and snippets.

@aguimaraes
Created May 22, 2017 12:54
Show Gist options
  • Save aguimaraes/fed02c0853c2d4cdbcc51f773eb82265 to your computer and use it in GitHub Desktop.
Save aguimaraes/fed02c0853c2d4cdbcc51f773eb82265 to your computer and use it in GitHub Desktop.
<?php
abstract class Payment_Service_Command_Authorize implements Orderprocessing_Service_Interface_Command
{
/**
* @param Orderprocessing_Service_Interface_CommandParameter $parameter
*
* @see Orderprocessing_Service_Interface_Command::__invoke()
*/
public function __invoke(Orderprocessing_Service_Interface_CommandParameter $parameter)
{
$start = microtime(true);
$workPackage = $parameter->getWorkPackage();
/* @var $context PaymentExt_Service_Context */
$context = $parameter->getContext('PaymentExt_Service_Context');
$processedItems = $parameter->getProcessedItems();
$orderCollection = $workPackage->getOrderCollection();
foreach ($processedItems->getAllOrdersWithoutFailedItems($orderCollection) as $order) {
$paymentMethod = $context->getPaymentMethod($order);
if ($paymentMethod->canAuthorize()) {
$paymentResult = $paymentMethod->authorize($order);
} else {
$paymentResult = new PaymentExt_Service_Result_Successful();
}
$context->setPaymentResult($order, $paymentResult);
if ($paymentResult instanceof Bob_Api_Result_Successful) {
$processedItems->setOrderSuccessful($order);
} else {
$processedItems->setOrderFailed($order);
}
}
$this->publishPaymentAttemptEventAsynchronously($orderCollection);
HF()->statsd()->timing('fraud-detection.payment.timing', microtime(true) - $start);
}
/**
* @param Transfer_Sales_OrderCollection $orderCollection
*/
private function publishPaymentAttemptEventAsynchronously(\Transfer_Sales_OrderCollection $orderCollection)
{
HF()->log()->error('Starting asynchronous shit.');
\Icicle\Coroutine\create(function () use ($orderCollection) {
HF()->log()->error('Created (and started) the co routine');
$fork = \Icicle\Concurrent\Forking\Fork::spawn(function () use ($orderCollection) {
HF()->log()->error('Created a fork');
$start = microtime();
try {
$publisher = new PaymentExt_Service_Event_Publisher(
new Bi_Service_Adapter_RabbitMQProducer('OrdersExchange'),
$orderCollection
);
$publisher->publishInBulk();
} catch (\Exception $e) {
HF()->log()->exception($e);
}
HF()->statsd()->timing('fraud-detection.payment-async.timing', microtime(true) - $start);
});
yield $fork->join();
});
HF()->log()->error('Run the loop.');
\Icicle\Loop\run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment