Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active January 4, 2018 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/71162ce725576990a7437e48eed676fd to your computer and use it in GitHub Desktop.
Save webdevilopers/71162ce725576990a7437e48eed676fd to your computer and use it in GitHub Desktop.
Run ReactPHP Event Loop with database query and return Promise
<?php
namespace Acme\FooApplication\OfferLetter;
use React\EventLoop\Timer\Timer;
use React\Promise\Deferred;
use Acme\Foo\Domain\Model\Offer\OfferId;
use React\EventLoop\Factory;
use Acme\Foo\Infrastructure\Projection\Mongo\OfferOverviewFinder;
class OfferLetterLookup
{
/** @var OfferOverviewFinder */
private $offerOverviewFinder;
/**
* OfferLetterLookup constructor.
* @param OfferOverviewFinder $offerOverviewFinder
*/
public function __construct(OfferOverviewFinder $offerOverviewFinder)
{
$this->offerOverviewFinder = $offerOverviewFinder;
}
public function lookup(OfferId $offerId, Deferred $deferred)
{
$i = 0;
$loop = Factory::create();
$loop->addPeriodicTimer(2, function(Timer $timer) use (&$i, $deferred, $offerId) {
$deferred->notify($i++);
$offer = $this->offerOverviewFinder->ofOfferId($offerId);
if (isset($offer['offerLetterId'])) {
$deferred->resolve($offer['offerLetterId']);
$timer->cancel();
}
if ($i >= 10) {
$timer->cancel();
$deferred->reject();
}
});
$loop->run();
return $deferred;
}
}
<?php
namespace Acme\Intranet\Infrastructure\Symfony\IntranetBundle\Controller;
class OfferController extends Controller
{
public function lookupOfferLetterAction(string $offerId)
{
$data = ['offerLetterId' => null];
$query = LookupOfferLetterQuery::with(OfferId::fromString($offerId));
$offerList = $this->get('prooph_service_bus.intranet_query_bus')->dispatch($query);
$offerList
->then(
function($offerLetterId) use (&$data) {
$data['offerLetterId'] = (string)$offerLetterId;
}
);
return new JsonResponse($data);
}
}
{% block content %}
<script type="text/javascript">
$(document).ready(function() {
{% if details.offerLetterId is null %}
function isUUID(string){
return RegExp("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$").test(string);
}
$.ajax({
type: "POST",
dataType: "json",
url: '{{ path('sps_intranet_offer_lookup_offer_letter', {'offerId': details.offerId }) }}',
timeout: 60000,
success: function(data) {
$('#lookup-offer-letter').hide();
if (data.hasOwnProperty('offerLetterId')) {
if (isUUID(data['offerLetterId'])) {
$('#advanced-offer-options').show();
return true;
}
}
alert('{{ 'lookup_offer_letter_error'|trans }}');
},
error: function(jqXHR, textStatus) {
$('#lookup-offer-letter').hide();
if (textStatus === 'timeout') {
alert('{{ 'lookup_offer_letter_error'|trans }}');
}
}
});
{% endif %}
</script>
{% endblock %}
@seregazhuk
Copy link

And by the way, you should use array by reference in closure:

function($offerLetterId) use ($data) {
    $data['offerLetterId'] = (string)$offerLetterId;
}

This code doesn't change an array outside the closure, only the copy inside of it.

@webdevilopers
Copy link
Author

Thank you very much @seregazhuk, @rpkamp. I added your changes!

@webdevilopers
Copy link
Author

When running the EventLoop locally on Apache the process will block all other HTTP processes.
Is this an expected behaviour? Do I need sockets? Is "multi threading" possible with Docker instead?

Forgive my ignorance on this topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment