From c56602a129f4067a2540af7d08f77a92d54c8d4f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ph=C3=A9na=20Proxima?= <adam@phenaproxima.net>
Date: Thu, 16 Sep 2021 15:44:18 -0400
Subject: [PATCH] Make it an event

---
 automatic_updates.services.yml        | 13 ++----
 src/AutomaticUpdatesEvents.php        |  9 +++++
 src/ComposerStager/Committer.php      | 57 ---------------------------
 src/Event/UpdateRefreshSubscriber.php | 48 ++++++++++++++++++++++
 src/Updater.php                       |  1 +
 5 files changed, 62 insertions(+), 66 deletions(-)
 delete mode 100644 src/ComposerStager/Committer.php
 create mode 100644 src/Event/UpdateRefreshSubscriber.php

diff --git a/automatic_updates.services.yml b/automatic_updates.services.yml
index 98731c2562..e1e01d8d63 100644
--- a/automatic_updates.services.yml
+++ b/automatic_updates.services.yml
@@ -5,8 +5,6 @@ services:
   automatic_updates.updater:
     class: Drupal\automatic_updates\Updater
     arguments: ['@state', '@string_translation','@package_manager.beginner', '@package_manager.stager', '@package_manager.cleaner', '@package_manager.committer', '@event_dispatcher', '@automatic_updates.path_locator']
-
-  # Package Manager service decorators.
   automatic_updates.cleaner:
     class: Drupal\automatic_updates\ComposerStager\Cleaner
     decorates: package_manager.cleaner
@@ -16,15 +14,12 @@ services:
       - '%site.path%'
       - '@automatic_updates.path_locator'
     properties: { _serviceId: package_manager.cleaner }
-  automatic_updates.committer:
-    class: Drupal\automatic_updates\ComposerStager\Committer
-    decorates: package_manager.committer
-    public: false
+  automatic_updates.update_refresh_subscriber:
+    class: Drupal\automatic_updates\Event\UpdateRefreshSubscriber
     arguments:
-      - '@automatic_updates.committer.inner'
       - '@update.manager'
-    properties: { _serviceId: package_manager.committer }
-
+    tags:
+      - { name: event_subscriber, priority: 1000 }
   automatic_updates.excluded_paths_subscriber:
     class: Drupal\automatic_updates\Event\ExcludedPathsSubscriber
     arguments: ['%app.root%', '%site.path%', '@file_system', '@stream_wrapper_manager']
diff --git a/src/AutomaticUpdatesEvents.php b/src/AutomaticUpdatesEvents.php
index 7f04539a9c..dedef4e0cd 100644
--- a/src/AutomaticUpdatesEvents.php
+++ b/src/AutomaticUpdatesEvents.php
@@ -57,4 +57,13 @@ final class AutomaticUpdatesEvents {
    */
   const PRE_COMMIT = 'automatic_updates.pre_commit';
 
+  /**
+   * Name of the event fired when a staged update has been committed.
+   *
+   * @Event
+   *
+   * @var string
+   */
+  const POST_COMMIT = 'automatic_updates.post_commit';
+
 }
diff --git a/src/ComposerStager/Committer.php b/src/ComposerStager/Committer.php
deleted file mode 100644
index d8e73f928a..0000000000
--- a/src/ComposerStager/Committer.php
+++ /dev/null
@@ -1,57 +0,0 @@
-<?php
-
-namespace Drupal\automatic_updates\ComposerStager;
-
-use Drupal\update\UpdateManagerInterface;
-use PhpTuf\ComposerStager\Domain\CommitterInterface;
-use PhpTuf\ComposerStager\Domain\Output\ProcessOutputCallbackInterface;
-
-/**
- * Defines a committer service that clears stored update data.
- */
-class Committer implements CommitterInterface {
-
-  /**
-   * The decorated committer service.
-   *
-   * @var \PhpTuf\ComposerStager\Domain\CommitterInterface
-   */
-  protected $decorated;
-
-  /**
-   * The update manager service.
-   *
-   * @var \Drupal\update\UpdateManagerInterface
-   */
-  protected $updateManager;
-
-  /**
-   * Constructs a Committer object.
-   *
-   * @param \PhpTuf\ComposerStager\Domain\CommitterInterface $decorated
-   *   The decorated committer service.
-   * @param \Drupal\update\UpdateManagerInterface $update_manager
-   *   The update manager service.
-   */
-  public function __construct(CommitterInterface $decorated, UpdateManagerInterface $update_manager) {
-    $this->decorated = $decorated;
-    $this->updateManager = $update_manager;
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public function commit(string $stagingDir, string $activeDir, ?array $exclusions = [], ?ProcessOutputCallbackInterface $callback = NULL, ?int $timeout = 120): void {
-    $this->decorated->commit($stagingDir, $activeDir, $exclusions, $callback, $timeout);
-    $this->updateManager->refreshUpdateData();
-    update_storage_clear();
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public function directoryExists(string $stagingDir): bool {
-    return $this->decorated->directoryExists($stagingDir);
-  }
-
-}
diff --git a/src/Event/UpdateRefreshSubscriber.php b/src/Event/UpdateRefreshSubscriber.php
new file mode 100644
index 0000000000..715807c689
--- /dev/null
+++ b/src/Event/UpdateRefreshSubscriber.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace Drupal\automatic_updates\Event;
+
+use Drupal\automatic_updates\AutomaticUpdatesEvents;
+use Drupal\update\UpdateManagerInterface;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+/**
+ * Clears stale update data once a staged update has been committed.
+ */
+class UpdateRefreshSubscriber implements EventSubscriberInterface {
+
+  /**
+   * The update manager service.
+   *
+   * @var \Drupal\update\UpdateManagerInterface
+   */
+  protected $updateManager;
+
+  /**
+   * Constructs an UpdateRefreshSubscriber object.
+   *
+   * @param \Drupal\update\UpdateManagerInterface $update_manager
+   *   The update manager service.
+   */
+  public function __construct(UpdateManagerInterface $update_manager) {
+    $this->updateManager = $update_manager;
+  }
+
+  /**
+   * Clears stale update data.
+   */
+  public function clearData(): void {
+    $this->updateManager->refreshUpdateData();
+    update_storage_clear();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function getSubscribedEvents() {
+    return [
+      AutomaticUpdatesEvents::POST_COMMIT => 'clearData',
+    ];
+  }
+
+}
diff --git a/src/Updater.php b/src/Updater.php
index 0624bf8173..fcd251a5e0 100644
--- a/src/Updater.php
+++ b/src/Updater.php
@@ -234,6 +234,7 @@ class Updater {
     /** @var \Drupal\automatic_updates\Event\PreCommitEvent $event */
     $event = $this->dispatchUpdateEvent(new PreCommitEvent(), AutomaticUpdatesEvents::PRE_COMMIT);
     $this->committer->commit($this->pathLocator->getStageDirectory(), $this->pathLocator->getActiveDirectory(), $this->getExclusions($event));
+    $this->dispatchUpdateEvent(new UpdateEvent(), AutomaticUpdatesEvents::POST_COMMIT);
   }
 
   /**
-- 
GitLab