You are here

class Trigger in Build Hooks 3.x

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

Defines a service for triggering deployments.

Hierarchy

Expanded class hierarchy of Trigger

2 files declare their use of Trigger
CircleBuildHookTest.php in modules/build_hooks_circleci/tests/src/Kernel/CircleBuildHookTest.php
EnvironmentTestTrait.php in tests/src/Traits/EnvironmentTestTrait.php
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 24

Namespace

Drupal\build_hooks
View source
class Trigger implements TriggerInterface {
  use MessengerTrait;
  use StringTranslationTrait;

  /**
   * 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;

  /**
   * The deploy logger service.
   *
   * @var \Drupal\build_hooks\DeployLogger
   */
  protected $deployLogger;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * The cache tag invalidator service.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheTagInvalidator;

  /**
   * The event dispatcher service.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * Constructs a new Trigger object.
   */
  public function __construct(ConfigFactoryInterface $configFactory, ClientInterface $httpClient, AccountProxyInterface $currentUser, TranslationManager $stringTranslation, MessengerInterface $messenger, LoggerChannelFactoryInterface $logger, DeployLogger $deployLogger, EntityTypeManager $entityTypeManager, CacheTagsInvalidatorInterface $cacheTagInvalidator, EventDispatcherInterface $event_dispatcher) {
    $this->configFactory = $configFactory;
    $this->httpClient = $httpClient;
    $this->currentUser = $currentUser;
    $this->stringTranslation = $stringTranslation;
    $this->messenger = $messenger;
    $this->logger = $logger;
    $this->deployLogger = $deployLogger;
    $this->entityTypeManager = $entityTypeManager;
    $this->cacheTagInvalidator = $cacheTagInvalidator;
    $this->eventDispatcher = $event_dispatcher;
  }

  /**
   * Triggers all environments that are marked to fire on cron.
   */
  public function deployFrontendCronEnvironments() {
    $this
      ->deployEnvironmentsByDeploymentStrategy(TriggerInterface::DEPLOYMENT_STRATEGY_CRON);
  }

  /**
   * Triggers all environments that are marked to fire on entity update.
   */
  public function deployFrontendEntityUpdateEnvironments() {
    $this
      ->deployEnvironmentsByDeploymentStrategy(TriggerInterface::DEPLOYMENT_STRATEGY_ENTITYSAVE);
  }

  /**
   * Triggers all environments found by a specific deployment strategy.
   *
   * @param string $strategy
   *   The type of deployment strategy.
   */
  private function deployEnvironmentsByDeploymentStrategy(string $strategy) {
    try {

      /** @var \Drupal\build_hooks\Entity\FrontendEnvironmentInterface $environment */
      $environments = $this->entityTypeManager
        ->getStorage('frontend_environment')
        ->loadByProperties([
        'deployment_strategy' => $strategy,
      ]);
      foreach ($environments as $environment) {
        $this
          ->triggerBuildHookForEnvironment($environment);
      }
    } catch (\Exception $e) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('Could not trigger deployments with strategy @strategy. Error message: @error', [
        '@strategy' => $strategy,
        '@error' => $e
          ->getMessage(),
      ]));
    }
  }

  /**
   * Checks if we should show the environments in the upper menu.
   *
   * @return bool
   *   Boolean value.
   */
  public function showMenu() {
    if (!$this
      ->isValidUser()) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * Checks if the user has the permission to trigger deployments.
   *
   * @return bool
   *   Boolean value.
   */
  private function isValidUser() {
    return $this->currentUser
      ->hasPermission('trigger deployments');
  }

  /**
   * Trigger a deployment for a frontend environment.
   *
   * @param \Drupal\build_hooks\Entity\FrontendEnvironmentInterface $frontendEnvironment
   *   The frontend environment entity to trigger the deployment for.
   */
  public function triggerBuildHookForEnvironment(FrontendEnvironmentInterface $frontendEnvironment) {

    /** @var \Drupal\build_hooks\Plugin\FrontendEnvironmentInterface $plugin */
    $plugin = $frontendEnvironment
      ->getPlugin();
    $buildHookDetails = $plugin
      ->getBuildHookDetails();
    try {
      $result = $this
        ->triggerBuildHook($buildHookDetails, $frontendEnvironment);
      if (!$result
        ->shouldBuild()) {
        $reason = $result
          ->getReason();
        $this->messenger
          ->addWarning($reason);
        $this->logger
          ->get('build_hooks')
          ->notice(sprintf('Build was cancelled: %s', (string) $reason));
        return;
      }
      if ($plugin
        ->deploymentWasTriggered($result
        ->getResponse())) {

        // If the call was successful, set the latest deployment time
        // for this environment.
        $this->deployLogger
          ->setLastDeployTimeForEnvironment($frontendEnvironment);
        $this
          ->messenger()
          ->addMessage($this
          ->t('Deployment triggered for environment @env .', [
          '@env' => $frontendEnvironment
            ->label(),
        ]));
        $this
          ->invalidateToolbarCacheTag();
      }
      else {
        $this
          ->messenger()
          ->addWarning($result
          ->getResponse()
          ->getReasonPhrase());
      }
    } catch (GuzzleException $e) {
      $error = [
        'Failed to execute build hook for environment @env . Error message: <pre> @message </pre>',
        [
          '@message' => $e
            ->getMessage(),
          '@env' => $frontendEnvironment
            ->label(),
        ],
      ];
      $this
        ->messenger()
        ->addError($this
        ->t('Failed to execute build hook for environment @env . Error message: <pre> @message </pre>', $error[1]));
      $this->logger
        ->get('build_hooks')
        ->error($error[0], $error[1]);
    }
  }

  /**
   * Triggers a build hook by the details.
   *
   * @param \Drupal\build_hooks\BuildHookDetails $buildHookDetails
   *   An object that holds the information about the call.
   * @param \Drupal\build_hooks\Entity\FrontendEnvironmentInterface $frontendEnvironment
   *   The plugin which performs the trigger.
   *
   * @return \Drupal\build_hooks\Event\BuildTrigger
   *   Trigger event.
   *
   * @throws \GuzzleHttp\Exception\GuzzleException
   */
  private function triggerBuildHook(BuildHookDetails $buildHookDetails, FrontendEnvironmentInterface $frontendEnvironment) {

    /** @var \Drupal\build_hooks\DeploymentStorageHandlerInterface $deployment_storage */
    $deployment_storage = $this->entityTypeManager
      ->getStorage('build_hooks_deployment');
    $deployment = $deployment_storage
      ->getOrCreateNextDeploymentForEnvironment($frontendEnvironment);
    $triggerEvent = new BuildTrigger($buildHookDetails, $frontendEnvironment, $deployment);
    $frontendEnvironment
      ->getPlugin()
      ->preDeploymentTrigger($triggerEvent);
    $this->eventDispatcher
      ->dispatch(BuildTrigger::class, $triggerEvent);
    if (!$triggerEvent
      ->shouldBuild()) {
      return $triggerEvent;
    }
    $response = $this->httpClient
      ->request($buildHookDetails
      ->getMethod(), $buildHookDetails
      ->getUrl(), $buildHookDetails
      ->getOptions());
    $event = new ResponseEvent($response, $frontendEnvironment
      ->getPlugin());
    $this->eventDispatcher
      ->dispatch(ResponseEvent::EVENT_NAME, $event);
    return $triggerEvent
      ->setResponse($response);
  }

  /**
   * Utility function to retrieve the cache tag to apply to the toolbar.
   *
   * @return string
   *   The toolbar cache tag.
   */
  public function getToolbarCacheTag() {
    return TriggerInterface::BUILD_HOOKS_TOOLBAR_CACHE_TAG;
  }

  /**
   * Invalidates the toolbar cache tag.
   */
  public function invalidateToolbarCacheTag() {
    $this->cacheTagInvalidator
      ->invalidateTags([
      $this
        ->getToolbarCacheTag(),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
Trigger::$cacheTagInvalidator protected property The cache tag invalidator service.
Trigger::$configFactory protected property The config.factory service.
Trigger::$currentUser protected property The current_user service.
Trigger::$deployLogger protected property The deploy logger service.
Trigger::$entityTypeManager protected property The entity type manager.
Trigger::$eventDispatcher protected property The event dispatcher service.
Trigger::$httpClient protected property The http_client service.
Trigger::$logger protected property The logger.factory service.
Trigger::$messenger protected property The messenger service. Overrides MessengerTrait::$messenger
Trigger::$stringTranslation protected property The string_translation service. Overrides StringTranslationTrait::$stringTranslation
Trigger::deployEnvironmentsByDeploymentStrategy private function Triggers all environments found by a specific deployment strategy.
Trigger::deployFrontendCronEnvironments public function Triggers all environments that are marked to fire on cron. Overrides TriggerInterface::deployFrontendCronEnvironments
Trigger::deployFrontendEntityUpdateEnvironments public function Triggers all environments that are marked to fire on entity update.
Trigger::getToolbarCacheTag public function Utility function to retrieve the cache tag to apply to the toolbar. Overrides TriggerInterface::getToolbarCacheTag
Trigger::invalidateToolbarCacheTag public function Invalidates the toolbar cache tag. Overrides TriggerInterface::invalidateToolbarCacheTag
Trigger::isValidUser private function Checks if the user has the permission to trigger deployments.
Trigger::showMenu public function Checks if we should show the environments in the upper menu. Overrides TriggerInterface::showMenu
Trigger::triggerBuildHook private function Triggers a build hook by the details.
Trigger::triggerBuildHookForEnvironment public function Trigger a deployment for a frontend environment. Overrides TriggerInterface::triggerBuildHookForEnvironment
Trigger::__construct public function Constructs a new Trigger object.
TriggerInterface::BUILD_HOOKS_TOOLBAR_CACHE_TAG constant
TriggerInterface::DEPLOYMENT_STRATEGY_CRON constant
TriggerInterface::DEPLOYMENT_STRATEGY_ENTITYSAVE constant
TriggerInterface::DEPLOYMENT_STRATEGY_MANUAL constant