Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. class QCModifiedSubscriber implements EventSubscriberInterface
  2. {
  3. public function __construct(EntityManagerInterface $em, CorrectionReponseService $correctionReponse)
  4. {
  5. $this->em = $em;
  6. $this->correctionReponse = $correctionReponse;
  7. }
  8.  
  9. public static function getSubscribedEvents()
  10. {
  11. // Liste des évènements écoutés et méthodes à appeler
  12. return array(
  13. 'qc.modified' => 'calculStats'
  14. );
  15. }
  16.  
  17. public function calculStats(Event $event)
  18. {
  19. $data = $event->getData();
  20. $QC = $this->em->getRepository(QC::class)->find($data['qcId']);
  21. $this->correctionReponse->correctionReponsesOfQC($QC);
  22. }
  23.  
  24. services:
  25. PACESColleBundleEventListenerQCModifiedSubscriber:
  26. tags:
  27. - { name: 'kernel.event_subscriber'}
  28.  
  29. protected function execute(InputInterface $input, OutputInterface $output)
  30. {
  31. $em = $this->getContainer()->get('doctrine')->getManager();
  32. $dispatcher = new EventDispatcher();
  33.  
  34. while (true) {
  35. $processId = rand(0, 999999999);
  36. # attempts to claim a single event, returns it if successful. may return multiple events.
  37. // note: $processId is some unique id to this process, helps prevent race conditions (see below)
  38. $events = $em->getRepository(Event::class)->claimEvent($processId);
  39.  
  40. # no events to process, so break out of loop.
  41. if (count($events) === 0) {
  42. break;
  43. }
  44.  
  45. # iterate over each event to be processed, typically just 1.
  46. foreach ($events as $eventEntity) {
  47. $output->write("Processing id: {$eventEntity->getId()}" . PHP_EOL);
  48.  
  49. # create the event...
  50. $event = new SymfonyComponentEventDispatcherEvent($eventEntity);
  51.  
  52. try {
  53. # dispatch the event!
  54. $dispatcher->dispatch($eventEntity->getName(), $event);
  55. # if we made it here we were successful, mark as processed
  56. $eventEntity->setProcessed(1);
  57.  
  58. } catch (Exception $e) {
  59. $eventEntity->setError((string)$e);
  60. }
  61.  
  62. $em->persist($eventEntity);
  63. $em->flush();
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement