You are here

class Trigger in Build Hooks 8

Same name and namespace in other branches
  1. 8.2 src/Trigger.php \Drupal\build_hooks\Trigger
  2. 3.x src/Trigger.php \Drupal\build_hooks\Trigger

Class Trigger.

Hierarchy

Expanded class hierarchy of Trigger

1 string reference to 'Trigger'
build_hooks.services.yml in ./build_hooks.services.yml
build_hooks.services.yml
1 service uses Trigger
build_hooks.trigger in ./build_hooks.services.yml
Drupal\build_hooks\Trigger

File

src/Trigger.php, line 16

Namespace

Drupal\build_hooks
View source
class Trigger implements TriggerInterface {

  /**
   * The config.factory service
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The http_client service
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * The current_user service
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The string_translation service
   *
   * @var \Drupal\Core\StringTranslation\TranslationManager
   */
  protected $stringTranslation;

  /**
   * The messenger service
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The logger.factory service
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $logger;

  /**
   * Constructs a new Trigger object.
   */
  public function __construct(ConfigFactoryInterface $configFactory, ClientInterface $httpClient, AccountProxyInterface $currentUser, TranslationManager $stringTranslation, MessengerInterface $messenger, LoggerChannelFactoryInterface $logger) {
    $this->configFactory = $configFactory;
    $this->httpClient = $httpClient;
    $this->currentUser = $currentUser;
    $this->stringTranslation = $stringTranslation;
    $this->messenger = $messenger;
    $this->logger = $logger;
  }
  public function execute() {
    if (!$this
      ->isValidUser()) {
      $message = $this->stringTranslation
        ->translate('Insufficient user acess', []);
      $this->logger
        ->get('build_hooks')
        ->error($message);
      return FALSE;
    }
    try {
      $hook = $this->configFactory
        ->get('build_hooks.settings')
        ->get('build_hook');
      $this->httpClient
        ->post($hook . '-lorem');
      $message = 'Build Hook Executed';
      $this
        ->processMessage($message);
      $this
        ->processLog($message);
    } catch (RequestException $exception) {
      $message = $this->stringTranslation
        ->translate('Build Hook execute error: "%error"', [
        '%error' => $exception
          ->getMessage(),
      ]);
      $this->messenger
        ->addError($message);
      $this->logger
        ->get('build_hooks')
        ->error($message);
      return FALSE;
    }
    return TRUE;
  }
  public function executeCron() {
    $execute = (bool) $this->configFactory
      ->get('build_hooks.settings')
      ->get('triggers.cron');
    if ($execute) {
      $this
        ->execute();
    }
  }
  public function executeNode($nodeType) {
    $execute = (bool) $this->configFactory
      ->get('build_hooks.settings')
      ->get('triggers.node.' . $nodeType);
    if ($execute) {
      $this
        ->execute();
    }
  }
  public function showMenu() {
    if (!$this
      ->isValidUser()) {
      return FALSE;
    }
    return (bool) $this->configFactory
      ->get('build_hooks.settings')
      ->get('triggers.menu');
  }
  private function isValidUser() {
    return $this->currentUser
      ->hasPermission('access admin');
  }
  private function processMessage($message, $args = []) {
    $show = (bool) $this->configFactory
      ->get('build_hooks.settings')
      ->get('messages.show');
    if (!$show) {
      return;
    }
    $this->messenger
      ->addMessage($this->stringTranslation
      ->translate($message, $args));
  }
  private function processLog($message, $args = []) {
    $log = (bool) $this->configFactory
      ->get('build_hooks.settings')
      ->get('messages.log');
    if (!$log) {
      return;
    }
    $this->logger
      ->get('build_hooks')
      ->info($this->stringTranslation
      ->translate($message, $args));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Trigger::$configFactory protected property The config.factory service
Trigger::$currentUser protected property The current_user service
Trigger::$httpClient protected property The http_client service
Trigger::$logger protected property The logger.factory service
Trigger::$messenger protected property The messenger service
Trigger::$stringTranslation protected property The string_translation service
Trigger::execute public function return void Overrides TriggerInterface::execute
Trigger::executeCron public function return void Overrides TriggerInterface::executeCron
Trigger::executeNode public function Overrides TriggerInterface::executeNode
Trigger::isValidUser private function
Trigger::processLog private function
Trigger::processMessage private function
Trigger::showMenu public function return void Overrides TriggerInterface::showMenu
Trigger::__construct public function Constructs a new Trigger object.