Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class QCModifiedSubscriber implements EventSubscriberInterface
- {
- public function __construct(EntityManagerInterface $em, CorrectionReponseService $correctionReponse)
- {
- $this->em = $em;
- $this->correctionReponse = $correctionReponse;
- }
- public static function getSubscribedEvents()
- {
- // Liste des évènements écoutés et méthodes à appeler
- return array(
- 'qc.modified' => 'calculStats'
- );
- }
- public function calculStats(Event $event)
- {
- $data = $event->getData();
- $QC = $this->em->getRepository(QC::class)->find($data['qcId']);
- $this->correctionReponse->correctionReponsesOfQC($QC);
- }
- services:
- PACESColleBundleEventListenerQCModifiedSubscriber:
- tags:
- - { name: 'kernel.event_subscriber'}
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $em = $this->getContainer()->get('doctrine')->getManager();
- $dispatcher = new EventDispatcher();
- while (true) {
- $processId = rand(0, 999999999);
- # attempts to claim a single event, returns it if successful. may return multiple events.
- // note: $processId is some unique id to this process, helps prevent race conditions (see below)
- $events = $em->getRepository(Event::class)->claimEvent($processId);
- # no events to process, so break out of loop.
- if (count($events) === 0) {
- break;
- }
- # iterate over each event to be processed, typically just 1.
- foreach ($events as $eventEntity) {
- $output->write("Processing id: {$eventEntity->getId()}" . PHP_EOL);
- # create the event...
- $event = new SymfonyComponentEventDispatcherEvent($eventEntity);
- try {
- # dispatch the event!
- $dispatcher->dispatch($eventEntity->getName(), $event);
- # if we made it here we were successful, mark as processed
- $eventEntity->setProcessed(1);
- } catch (Exception $e) {
- $eventEntity->setError((string)$e);
- }
- $em->persist($eventEntity);
- $em->flush();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement