Merge pull request #17358 from seamuslee001/d8_prevnext_cache_test_fix
[civicrm-core.git] / CRM / Core / BAO / ActionSchedule.php
index 11d9b209c80afc09071862e8be08a7e8204aca0e..a53e0e623278acb7ba7a1eb4b8f4df4aa58b8267 100644 (file)
@@ -25,15 +25,18 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
   /**
    * @param array $filters
    *   Filter by property (e.g. 'id').
+   *
    * @return array
    *   Array(scalar $id => Mapping $mapping).
+   *
+   * @throws \CRM_Core_Exception
    */
   public static function getMappings($filters = NULL) {
     static $_action_mapping;
 
     if ($_action_mapping === NULL) {
-      $event = \Civi\Core\Container::singleton()->get('dispatcher')
-        ->dispatch(\Civi\ActionSchedule\Events::MAPPINGS,
+      $event = \Civi::dispatcher()
+        ->dispatch('civi.actionSchedule.getMappings',
           new \Civi\ActionSchedule\Event\MappingRegisterEvent());
       $_action_mapping = $event->getMappings();
     }
@@ -223,6 +226,7 @@ FROM civicrm_action_schedule cas
    * @param int $id
    *   ID of the Reminder to be deleted.
    *
+   * @throws CRM_Core_Exception
    */
   public static function del($id) {
     if ($id) {
@@ -233,7 +237,7 @@ FROM civicrm_action_schedule cas
         return;
       }
     }
-    CRM_Core_Error::fatal(ts('Invalid value passed to delete function.'));
+    throw new CRM_Core_Exception(ts('Invalid value passed to delete function.'));
   }
 
   /**
@@ -320,14 +324,21 @@ FROM civicrm_action_schedule cas
   }
 
   /**
-   * @param int $mappingID
-   * @param $now
+   * Build a list of the contacts to send to.
+   *
+   * @param string $mappingID
+   *   Value from the mapping_id field in the civicrm_action_schedule able. It might be a string like
+   *  'contribpage' for an older class like CRM_Contribute_ActionMapping_ByPage of for ones following
+   *   more recent patterns, an integer.
+   * @param string $now
    * @param array $params
    *
    * @throws API_Exception
+   * @throws \CRM_Core_Exception
    */
-  public static function buildRecipientContacts($mappingID, $now, $params = []) {
+  public static function buildRecipientContacts(string $mappingID, $now, $params = []) {
     $actionSchedule = new CRM_Core_DAO_ActionSchedule();
+
     $actionSchedule->mapping_id = $mappingID;
     $actionSchedule->is_active = 1;
     if (!empty($params)) {
@@ -346,25 +357,22 @@ FROM civicrm_action_schedule cas
   }
 
   /**
-   * @param null $now
+   * Main processing callback for sending out scheduled reminders.
+   *
+   * @param string $now
    * @param array $params
    *
-   * @return array
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
    */
   public static function processQueue($now = NULL, $params = []) {
     $now = $now ? CRM_Utils_Time::setTime($now) : CRM_Utils_Time::getTime();
 
     $mappings = CRM_Core_BAO_ActionSchedule::getMappings();
     foreach ($mappings as $mappingID => $mapping) {
-      CRM_Core_BAO_ActionSchedule::buildRecipientContacts($mappingID, $now, $params);
+      CRM_Core_BAO_ActionSchedule::buildRecipientContacts((string) $mappingID, $now, $params);
       CRM_Core_BAO_ActionSchedule::sendMailings($mappingID, $now);
     }
-
-    $result = [
-      'is_error' => 0,
-      'messages' => ts('Sent all scheduled reminders successfully'),
-    ];
-    return $result;
   }
 
   /**
@@ -507,9 +515,9 @@ FROM civicrm_action_schedule cas
       $select->where("e.id = reminder.entity_id OR reminder.entity_table = 'civicrm_contact'");
     }
 
-    \Civi\Core\Container::singleton()->get('dispatcher')
+    \Civi::dispatcher()
       ->dispatch(
-        \Civi\ActionSchedule\Events::MAILING_QUERY,
+        'civi.actionSchedule.prepareMailingQuery',
         new \Civi\ActionSchedule\Event\MailingQueryEvent($actionSchedule, $mapping, $select)
       );
 
@@ -644,7 +652,7 @@ FROM civicrm_action_schedule cas
    * @return \Civi\Token\TokenProcessor
    */
   protected static function createTokenProcessor($schedule, $mapping) {
-    $tp = new \Civi\Token\TokenProcessor(\Civi\Core\Container::singleton()->get('dispatcher'), [
+    $tp = new \Civi\Token\TokenProcessor(\Civi::dispatcher(), [
       'controller' => __CLASS__,
       'actionSchedule' => $schedule,
       'actionMapping' => $mapping,