From 4fd5ec3eb6061d62ee54e9b7b300b4b550460f66 Mon Sep 17 00:00:00 2001
From: tedbow <tedbow@240860.no-reply.drupal.org>
Date: Tue, 7 Sep 2021 17:44:58 +0000
Subject: [PATCH] Issue #3231153 by tedbow: Simplify event dispatching in the
 updater

---
 src/Updater.php | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)

diff --git a/src/Updater.php b/src/Updater.php
index 5c8fbce678..57588de8b2 100644
--- a/src/Updater.php
+++ b/src/Updater.php
@@ -25,16 +25,6 @@ class Updater {
 
   use StringTranslationTrait;
 
-  /**
-   * The event classes to dispatch for various update events.
-   *
-   * @var string[]
-   */
-  protected const EVENT_CLASSES = [
-    AutomaticUpdatesEvents::PRE_START => PreStartEvent::class,
-    AutomaticUpdatesEvents::PRE_COMMIT => PreCommitEvent::class,
-  ];
-
   /**
    * The state key in which to store the status of the update.
    *
@@ -178,7 +168,8 @@ class Updater {
     }
     $packages[] = 'drupal/core:' . $project_versions['drupal'];
     $stage_key = $this->createActiveStage($packages);
-    $event = $this->dispatchUpdateEvent(AutomaticUpdatesEvents::PRE_START);
+    /** @var \Drupal\automatic_updates\Event\PreStartEvent $event */
+    $event = $this->dispatchUpdateEvent(new PreStartEvent(), AutomaticUpdatesEvents::PRE_START);
     $this->beginner->begin(static::getActiveDirectory(), static::getStageDirectory(), $this->getExclusions($event));
     return $stage_key;
   }
@@ -224,7 +215,7 @@ class Updater {
    */
   public function commit(): void {
     /** @var \Drupal\automatic_updates\Event\PreCommitEvent $event */
-    $event = $this->dispatchUpdateEvent(AutomaticUpdatesEvents::PRE_COMMIT);
+    $event = $this->dispatchUpdateEvent(new PreCommitEvent(), AutomaticUpdatesEvents::PRE_COMMIT);
     // @todo Pass excluded paths into the committer once
     // https://github.com/php-tuf/composer-stager/pull/14 is in a tagged release
     // of Composer Stager.
@@ -278,6 +269,8 @@ class Updater {
   /**
    * Dispatches an update event.
    *
+   * @param \Drupal\automatic_updates\Event\UpdateEvent $event
+   *   The update event.
    * @param string $event_name
    *   The name of the event to dispatch.
    *
@@ -287,9 +280,7 @@ class Updater {
    * @throws \Drupal\automatic_updates\Exception\UpdateException
    *   If any of the event subscribers adds a validation error.
    */
-  public function dispatchUpdateEvent(string $event_name): UpdateEvent {
-    $class = static::EVENT_CLASSES[$event_name] ?? UpdateEvent::class;
-    $event = new $class();
+  public function dispatchUpdateEvent(UpdateEvent $event, string $event_name): UpdateEvent {
     $this->eventDispatcher->dispatch($event, $event_name);
     if ($checker_results = $event->getResults(SystemManager::REQUIREMENT_ERROR)) {
       throw new UpdateException($checker_results,
-- 
GitLab