[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/task/requests/src/Extension/ -> Requests.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugins
   5   * @subpackage  Task.Requests
   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\Plugin\Task\Requests\Extension;
  12  
  13  use Exception;
  14  use Joomla\CMS\Plugin\CMSPlugin;
  15  use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
  16  use Joomla\Component\Scheduler\Administrator\Task\Status as TaskStatus;
  17  use Joomla\Component\Scheduler\Administrator\Traits\TaskPluginTrait;
  18  use Joomla\Event\DispatcherInterface;
  19  use Joomla\Event\SubscriberInterface;
  20  use Joomla\Filesystem\File;
  21  use Joomla\Filesystem\Path;
  22  use Joomla\Http\HttpFactory;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Task plugin with routines to make HTTP requests.
  30   * At the moment, offers a single routine for GET requests.
  31   *
  32   * @since  4.1.0
  33   */
  34  final class Requests extends CMSPlugin implements SubscriberInterface
  35  {
  36      use TaskPluginTrait;
  37  
  38      /**
  39       * @var string[]
  40       * @since 4.1.0
  41       */
  42      protected const TASKS_MAP = [
  43          'plg_task_requests_task_get' => [
  44              'langConstPrefix' => 'PLG_TASK_REQUESTS_TASK_GET_REQUEST',
  45              'form'            => 'get_requests',
  46              'method'          => 'makeGetRequest',
  47          ],
  48      ];
  49  
  50      /**
  51       * Returns an array of events this subscriber will listen to.
  52       *
  53       * @return string[]
  54       *
  55       * @since 4.1.0
  56       */
  57      public static function getSubscribedEvents(): array
  58      {
  59          return [
  60              'onTaskOptionsList'    => 'advertiseRoutines',
  61              'onExecuteTask'        => 'standardRoutineHandler',
  62              'onContentPrepareForm' => 'enhanceTaskItemForm',
  63          ];
  64      }
  65  
  66      /**
  67       * @var boolean
  68       * @since 4.1.0
  69       */
  70      protected $autoloadLanguage = true;
  71  
  72      /**
  73       * The http factory
  74       *
  75       * @var    HttpFactory
  76       * @since  4.2.0
  77       */
  78      private $httpFactory;
  79  
  80      /**
  81       * The root directory
  82       *
  83       * @var    string
  84       * @since  4.2.0
  85       */
  86      private $rootDirectory;
  87  
  88      /**
  89       * Constructor.
  90       *
  91       * @param   DispatcherInterface  $dispatcher     The dispatcher
  92       * @param   array                $config         An optional associative array of configuration settings
  93       * @param   HttpFactory          $httpFactory    The http factory
  94       * @param   string               $rootDirectory  The root directory to store the output file in
  95       *
  96       * @since   4.2.0
  97       */
  98      public function __construct(DispatcherInterface $dispatcher, array $config, HttpFactory $httpFactory, string $rootDirectory)
  99      {
 100          parent::__construct($dispatcher, $config);
 101  
 102          $this->httpFactory   = $httpFactory;
 103          $this->rootDirectory = $rootDirectory;
 104      }
 105  
 106      /**
 107       * Standard routine method for the get request routine.
 108       *
 109       * @param   ExecuteTaskEvent  $event  The onExecuteTask event
 110       *
 111       * @return integer  The exit code
 112       *
 113       * @since 4.1.0
 114       * @throws Exception
 115       */
 116      protected function makeGetRequest(ExecuteTaskEvent $event): int
 117      {
 118          $id     = $event->getTaskId();
 119          $params = $event->getArgument('params');
 120  
 121          $url      = $params->url;
 122          $timeout  = $params->timeout;
 123          $auth     = (string) $params->auth ?? 0;
 124          $authType = (string) $params->authType ?? '';
 125          $authKey  = (string) $params->authKey ?? '';
 126          $headers  = [];
 127  
 128          if ($auth && $authType && $authKey) {
 129              $headers = [$authType => $authKey];
 130          }
 131  
 132          try {
 133              $response = $this->httpFactory->getHttp([])->get($url, $headers, $timeout);
 134          } catch (Exception $e) {
 135              $this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_TIMEOUT'));
 136  
 137              return TaskStatus::TIMEOUT;
 138          }
 139  
 140          $responseCode = $response->code;
 141          $responseBody = $response->body;
 142  
 143          // @todo this handling must be rethought and made safe. stands as a good demo right now.
 144          $responseFilename = Path::clean($this->rootDirectory . "/task_{$id}_response.html");
 145  
 146          try {
 147              File::write($responseFilename, $responseBody);
 148              $this->snapshot['output_file'] = $responseFilename;
 149              $responseStatus = 'SAVED';
 150          } catch (Exception $e) {
 151              $this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_UNWRITEABLE_OUTPUT'), 'error');
 152              $responseStatus = 'NOT_SAVED';
 153          }
 154  
 155          $this->snapshot['output']      = <<< EOF
 156  ======= Task Output Body =======
 157  > URL: $url
 158  > Response Code: $responseCode
 159  > Response: $responseStatus
 160  EOF;
 161  
 162          $this->logTask(sprintf($this->getApplication()->getLanguage()->_('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_RESPONSE'), $responseCode));
 163  
 164          if ($response->code !== 200) {
 165              return TaskStatus::KNOCKOUT;
 166          }
 167  
 168          return TaskStatus::OK;
 169      }
 170  }


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