[REF] Minor cleanup around actionn schedule code.
authoreileen <emcnaughton@wikimedia.org>
Thu, 23 Apr 2020 02:58:22 +0000 (14:58 +1200)
committereileen <emcnaughton@wikimedia.org>
Sat, 25 Apr 2020 00:45:06 +0000 (12:45 +1200)
CRM_Core_BAO_ActionSchedule::processQueue is only called from one place. It either succeeds or throws
an exception so there is no value in returning anything & the parsing of what it does return assumes that it
could return false

Some slight tightenig around typing as buildRecipientContact is also only called from one place

CRM/Core/BAO/ActionSchedule.php
CRM/Utils/Time.php
Civi/Api4/ActionSchedule.php
api/v3/Job.php

index 66a61544b409cea7e907ed46844df8dd6d23e77e..6a1472390d543663d10d47a8bc092d290e156bc0 100644 (file)
@@ -28,6 +28,7 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
    *
    * @return array
    *   Array(scalar $id => Mapping $mapping).
+   *
    * @throws \CRM_Core_Exception
    */
   public static function getMappings($filters = NULL) {
@@ -322,14 +323,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)) {
@@ -348,25 +356,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;
   }
 
   /**
index 6ab662da767f07187231a15248396b4730491738..dd4b4e2cca6100321cfc29eeb81d8eda11617a31 100644 (file)
@@ -32,7 +32,7 @@ class CRM_Utils_Time {
    * @param string $returnFormat
    *   Format in which date is to be retrieved.
    *
-   * @return date
+   * @return string
    */
   public static function getTime($returnFormat = 'YmdHis') {
     return date($returnFormat, self::getTimeRaw());
@@ -56,7 +56,7 @@ class CRM_Utils_Time {
    * @param string $returnFormat
    *   Format in which date is to be retrieved.
    *
-   * @return date
+   * @return string
    */
   public static function setTime($newDateTime, $returnFormat = 'YmdHis') {
     self::$_delta = strtotime($newDateTime) - time();
index 7ddcb4ea6578ea0fc9bb94637b653135f2982164..86cf74fc9db7bf5bf73386f9e704a4fc7ca5a675 100644 (file)
@@ -14,8 +14,6 @@
  *
  * @package CRM
  * @copyright CiviCRM LLC https://civicrm.org/licensing
- * $Id$
- *
  */
 
 
index 252726d7878e8b0892a219237a57d24c1726324d..f34d7d48a2245f1f81713596c068f6a61fc74525 100644 (file)
@@ -182,14 +182,16 @@ function _civicrm_api3_job_geocode_spec(&$params) {
 }
 
 /**
- * Send the scheduled reminders for all contacts (either for activities or events).
+ * Send the scheduled reminders as configured.
  *
  * @param array $params
- *   (reference ) input parameters.
- *                        now - the time to use, in YmdHis format
- *                            - makes testing a bit simpler since we can simulate past/future time
+ *  - now - the time to use, in YmdHis format
+ *  - makes testing a bit simpler since we can simulate past/future time
  *
  * @return array
+ * @throws \API_Exception
+ * @throws \CRM_Core_Exception
+ * @throws \CiviCRM_API3_Exception
  */
 function civicrm_api3_job_send_reminder($params) {
   //note that $params['rowCount' can be overridden by one of the preferred syntaxes ($options['limit'] = x
@@ -199,18 +201,12 @@ function civicrm_api3_job_send_reminder($params) {
   $params['rowCount'] = 0;
   $lock = Civi::lockManager()->acquire('worker.core.ActionSchedule');
   if (!$lock->isAcquired()) {
-    return civicrm_api3_create_error('Could not acquire lock, another ActionSchedule process is running');
+    throw new API_Exception('Could not acquire lock, another ActionSchedule process is running');
   }
 
-  $result = CRM_Core_BAO_ActionSchedule::processQueue(CRM_Utils_Array::value('now', $params), $params);
+  CRM_Core_BAO_ActionSchedule::processQueue($params['now'] ?? NULL, $params);
   $lock->release();
-
-  if ($result['is_error'] == 0) {
-    return civicrm_api3_create_success();
-  }
-  else {
-    return civicrm_api3_create_error($result['messages']);
-  }
+  return civicrm_api3_create_success(1, $params, 'ActionSchedule', 'send_reminder');
 }
 
 /**