예제 #1
0
 /**
  * Handle any event
  *
  * If this method does not fit to your need, you are advised to extend this
  * class and modify this method to suit your own needs
  *
  * @param string $resourceType
  * @param string $action
  * @param Event $event
  */
 protected function onEvent($resourceType, $action, Event $event)
 {
     $idList = null;
     $data = null;
     $key = $resourceType . ':' . $action;
     $sendOnDefaultChan = true;
     $additionalChanId = [];
     if ($event instanceof ResourceEvent) {
         $data = $event->getArguments();
         $idList = $event->getResourceIdList();
         $additionalChanId = $event->getChanIdList();
         $sendOnDefaultChan = $event->shouldKeepDefaultChan();
     } else {
         if ($event instanceof GenericEvent) {
             $data = $event->getArguments();
             // Attempt to do a best guess at the resource identifier
             if (isset($event['id'])) {
                 $idList = [$event['id']];
             } else {
                 $subject = $event->getSubject();
                 if (is_scalar($subject)) {
                     $idList = [$subject];
                 } else {
                     if (method_exists($subject, 'getId')) {
                         $idList = [$subject->getId()];
                     } else {
                         if (property_exists($subject, 'id')) {
                             $idList = [$subject->id];
                         }
                     }
                 }
             }
         }
     }
     if (isset($idList)) {
         // Proceed with pre-configured overrides
         if ($sendOnDefaultChan && isset($this->chanBlockingOverrides[$key])) {
             $additionalChanId = array_merge($additionalChanId, $this->chanBlockingOverrides[$key]);
             // FIXME override should not be blocking default chans
             // @TODO add a blocking boolean tag attribute (false by default)
             // $sendOnDefaultChan = false;
         }
         if ($additionalChanId) {
             foreach ($idList as $id) {
                 if ($sendOnDefaultChan) {
                     $additionalChanId[] = $this->service->getChanId($resourceType, $id);
                 }
                 $this->service->notifyChannel($additionalChanId, $resourceType, $id, $action, $data);
             }
         } else {
             if ($sendOnDefaultChan) {
                 foreach ($idList as $id) {
                     $this->service->notify($resourceType, $id, $action, $data);
                 }
             }
         }
         // We might end up sending nothing, as soon as any of the listeners
         // explicitly forbid to send on the default chan, and we have no
         // additional chans.
     }
 }