You are here

class SmsDeliveryReportsProcessor in SMS Framework 8

Same name and namespace in other branches
  1. 2.x src/EventSubscriber/SmsDeliveryReportsProcessor.php \Drupal\sms\EventSubscriber\SmsDeliveryReportsProcessor
  2. 2.1.x src/EventSubscriber/SmsDeliveryReportsProcessor.php \Drupal\sms\EventSubscriber\SmsDeliveryReportsProcessor

Handles delivery reports as they come in and updates storage.

Hierarchy

Expanded class hierarchy of SmsDeliveryReportsProcessor

1 string reference to 'SmsDeliveryReportsProcessor'
sms.services.yml in ./sms.services.yml
sms.services.yml
1 service uses SmsDeliveryReportsProcessor
sms.delivery_reports_processor in ./sms.services.yml
Drupal\sms\EventSubscriber\SmsDeliveryReportsProcessor

File

src/EventSubscriber/SmsDeliveryReportsProcessor.php, line 13

Namespace

Drupal\sms\EventSubscriber
View source
class SmsDeliveryReportsProcessor implements EventSubscriberInterface {

  /**
   * The entity storage for SMS delivery reports.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $reportStorage;

  /**
   * Creates a new SmsDeliveryReportsProcessor controller.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->reportStorage = $entity_type_manager
      ->getStorage('sms_report');
  }

  /**
   * Updates the delivery status on stored SMS delivery reports.
   *
   * @param \Drupal\sms\Event\SmsDeliveryReportEvent $event
   *   The event containing updated delivery reports status.
   */
  public function updateDeliveryReports(SmsDeliveryReportEvent $event) {
    foreach ($event
      ->getReports() as $report) {

      // Only messages that have message IDs can be tracked and updated.
      if ($report
        ->getMessageId()) {
        $existing = $this->reportStorage
          ->loadByProperties([
          'message_id' => $report
            ->getMessageId(),
        ]);
        if ($existing) {
          $existing = reset($existing);
          $existing
            ->setStatus($report
            ->getStatus())
            ->setStatusMessage($report
            ->getStatusMessage())
            ->setStatusTime($report
            ->getStatusTime())
            ->save();
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // Update delivery reports as they are received.
    $events[SmsEvents::DELIVERY_REPORT_POST_PROCESS][] = [
      'updateDeliveryReports',
      1024,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SmsDeliveryReportsProcessor::$reportStorage protected property The entity storage for SMS delivery reports.
SmsDeliveryReportsProcessor::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
SmsDeliveryReportsProcessor::updateDeliveryReports public function Updates the delivery status on stored SMS delivery reports.
SmsDeliveryReportsProcessor::__construct public function Creates a new SmsDeliveryReportsProcessor controller.