[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_scheduler/src/Helper/ -> ExecRuleHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_scheduler
   6   *
   7   * @copyright   (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Scheduler\Administrator\Helper;
  12  
  13  use Cron\CronExpression;
  14  use Joomla\CMS\Date\Date;
  15  use Joomla\CMS\Factory;
  16  use Joomla\Component\Scheduler\Administrator\Task\Task;
  17  use Joomla\Database\DatabaseDriver;
  18  use Joomla\Utilities\ArrayHelper;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Helper class for supporting task execution rules.
  26   *
  27   * @since  4.1.0
  28   * @todo   This helper should probably be merged into the {@see Task} class.
  29   */
  30  class ExecRuleHelper
  31  {
  32      /**
  33       * The execution rule type
  34       *
  35       * @var string
  36       * @since  4.1.0
  37       */
  38      private $type;
  39  
  40      /**
  41       * @var array
  42       * @since  4.1.0
  43       */
  44      private $task;
  45  
  46      /**
  47       * @var object
  48       * @since  4.1.0
  49       */
  50      private $rule;
  51  
  52      /**
  53       * @param   array|object  $task  A task entry
  54       *
  55       * @since  4.1.0
  56       */
  57      public function __construct($task)
  58      {
  59          $this->task = \is_array($task) ? $task : ArrayHelper::fromObject($task);
  60          $rule       = $this->getFromTask('cron_rules');
  61          $this->rule = \is_string($rule)
  62              ? (object) json_decode($rule)
  63              : (\is_array($rule) ? (object) $rule : $rule);
  64          $this->type = $this->rule->type;
  65      }
  66  
  67      /**
  68       * Get a property from the task array
  69       *
  70       * @param   string  $property  The property to get
  71       * @param   mixed   $default   The default value returned if property does not exist
  72       *
  73       * @return mixed
  74       *
  75       * @since  4.1.0
  76       */
  77      private function getFromTask(string $property, $default = null)
  78      {
  79          $property = ArrayHelper::getValue($this->task, $property);
  80  
  81          return $property ?? $default;
  82      }
  83  
  84      /**
  85       * @param   boolean  $string    If true, an SQL formatted string is returned.
  86       * @param   boolean  $basisNow  If true, the current date-time is used as the basis for projecting the next
  87       *                              execution.
  88       *
  89       * @return ?Date|string
  90       *
  91       * @since  4.1.0
  92       * @throws \Exception
  93       */
  94      public function nextExec(bool $string = true, bool $basisNow = false)
  95      {
  96          // Exception handling here
  97          switch ($this->type) {
  98              case 'interval':
  99                  $lastExec = Factory::getDate($basisNow ? 'now' : $this->getFromTask('last_execution'), 'UTC');
 100                  $interval = new \DateInterval($this->rule->exp);
 101                  $nextExec = $lastExec->add($interval);
 102                  $nextExec = $string ? $nextExec->toSql() : $nextExec;
 103                  break;
 104              case 'cron-expression':
 105                  // @todo: testing
 106                  $cExp     = new CronExpression((string) $this->rule->exp);
 107                  $nextExec = $cExp->getNextRunDate('now', 0, false, 'UTC');
 108                  $nextExec = $string ? $this->dateTimeToSql($nextExec) : $nextExec;
 109                  break;
 110              default:
 111                  // 'manual' execution is handled here.
 112                  $nextExec = null;
 113          }
 114  
 115          return $nextExec;
 116      }
 117  
 118      /**
 119       * Returns a sql-formatted string for a DateTime object.
 120       * Only needed for DateTime objects returned by CronExpression, JDate supports this as class method.
 121       *
 122       * @param   \DateTime  $dateTime  A DateTime object to format
 123       *
 124       * @return string
 125       *
 126       * @since  4.1.0
 127       */
 128      private function dateTimeToSql(\DateTime $dateTime): string
 129      {
 130          static $db;
 131          $db = $db ?? Factory::getContainer()->get(DatabaseDriver::class);
 132  
 133          return $dateTime->format($db->getDateFormat());
 134      }
 135  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer