From a349b6b2aee1cb49a3cbddd41afce90f733d3983 Mon Sep 17 00:00:00 2001
From: xjm <xjm@65776.no-reply.drupal.org>
Date: Wed, 27 Mar 2019 18:54:56 -0500
Subject: [PATCH] Issue #3043797 by tim.plunkett, johndevman, phenaproxima:
 Create an interface for InlineBlockUsage

---
 .../SetInlineBlockDependency.php              |  9 ++-
 .../src/InlineBlockEntityOperations.php       |  6 +-
 .../layout_builder/src/InlineBlockUsage.php   | 37 ++----------
 .../src/InlineBlockUsageInterface.php         | 60 +++++++++++++++++++
 .../FunctionalJavascript/InlineBlockTest.php  |  2 +-
 5 files changed, 74 insertions(+), 40 deletions(-)
 create mode 100644 core/modules/layout_builder/src/InlineBlockUsageInterface.php

diff --git a/core/modules/layout_builder/src/EventSubscriber/SetInlineBlockDependency.php b/core/modules/layout_builder/src/EventSubscriber/SetInlineBlockDependency.php
index 9f9ba7c2d6da..23e965be8654 100644
--- a/core/modules/layout_builder/src/EventSubscriber/SetInlineBlockDependency.php
+++ b/core/modules/layout_builder/src/EventSubscriber/SetInlineBlockDependency.php
@@ -8,7 +8,7 @@
 use Drupal\Core\Database\Connection;
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
-use Drupal\layout_builder\InlineBlockUsage;
+use Drupal\layout_builder\InlineBlockUsageInterface;
 use Drupal\layout_builder\LayoutEntityHelperTrait;
 use Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -52,7 +52,7 @@ class SetInlineBlockDependency implements EventSubscriberInterface {
   /**
    * The inline block usage service.
    *
-   * @var \Drupal\layout_builder\InlineBlockUsage
+   * @var \Drupal\layout_builder\InlineBlockUsageInterface
    */
   protected $usage;
 
@@ -63,12 +63,12 @@ class SetInlineBlockDependency implements EventSubscriberInterface {
    *   The entity type manager.
    * @param \Drupal\Core\Database\Connection $database
    *   The database connection.
-   * @param \Drupal\layout_builder\InlineBlockUsage $usage
+   * @param \Drupal\layout_builder\InlineBlockUsageInterface $usage
    *   The inline block usage service.
    * @param \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface $section_storage_manager
    *   The section storage manager.
    */
-  public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $database, InlineBlockUsage $usage, SectionStorageManagerInterface $section_storage_manager) {
+  public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $database, InlineBlockUsageInterface $usage, SectionStorageManagerInterface $section_storage_manager) {
     $this->entityTypeManager = $entity_type_manager;
     $this->database = $database;
     $this->usage = $usage;
@@ -128,7 +128,6 @@ protected function getInlineBlockDependency(BlockContentInterface $block_content
       // dependency. It may be used by another module besides layout builder.
       return NULL;
     }
-    /** @var \Drupal\layout_builder\InlineBlockUsage $usage */
     $layout_entity_storage = $this->entityTypeManager->getStorage($layout_entity_info->layout_entity_type);
     $layout_entity = $layout_entity_storage->load($layout_entity_info->layout_entity_id);
     if ($this->isLayoutCompatibleEntity($layout_entity)) {
diff --git a/core/modules/layout_builder/src/InlineBlockEntityOperations.php b/core/modules/layout_builder/src/InlineBlockEntityOperations.php
index 55eab3b75e7d..27c350b62751 100644
--- a/core/modules/layout_builder/src/InlineBlockEntityOperations.php
+++ b/core/modules/layout_builder/src/InlineBlockEntityOperations.php
@@ -23,7 +23,7 @@ class InlineBlockEntityOperations implements ContainerInjectionInterface {
   /**
    * Inline block usage tracking service.
    *
-   * @var \Drupal\layout_builder\InlineBlockUsage
+   * @var \Drupal\layout_builder\InlineBlockUsageInterface
    */
   protected $usage;
 
@@ -52,7 +52,7 @@ class InlineBlockEntityOperations implements ContainerInjectionInterface {
    *
    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
    *   The entity type manager service.
-   * @param \Drupal\layout_builder\InlineBlockUsage $usage
+   * @param \Drupal\layout_builder\InlineBlockUsageInterface $usage
    *   Inline block usage tracking service.
    * @param \Drupal\Core\Database\Connection $database
    *   The database connection.
@@ -65,7 +65,7 @@ class InlineBlockEntityOperations implements ContainerInjectionInterface {
    *   - The $database parameter is unused and should be removed.
    *   Deprecate in https://www.drupal.org/node/3031492.
    */
-  public function __construct(EntityTypeManagerInterface $entityTypeManager, InlineBlockUsage $usage, Connection $database, SectionStorageManagerInterface $section_storage_manager = NULL) {
+  public function __construct(EntityTypeManagerInterface $entityTypeManager, InlineBlockUsageInterface $usage, Connection $database, SectionStorageManagerInterface $section_storage_manager = NULL) {
     $this->entityTypeManager = $entityTypeManager;
     $this->blockContentStorage = $entityTypeManager->getStorage('block_content');
     $this->usage = $usage;
diff --git a/core/modules/layout_builder/src/InlineBlockUsage.php b/core/modules/layout_builder/src/InlineBlockUsage.php
index 063986212312..ab94d4c535bb 100644
--- a/core/modules/layout_builder/src/InlineBlockUsage.php
+++ b/core/modules/layout_builder/src/InlineBlockUsage.php
@@ -7,10 +7,8 @@
 
 /**
  * Service class to track inline block usage.
- *
- * @internal
  */
-class InlineBlockUsage {
+class InlineBlockUsage implements InlineBlockUsageInterface {
 
   /**
    * The database connection.
@@ -30,12 +28,7 @@ public function __construct(Connection $database) {
   }
 
   /**
-   * Adds a usage record.
-   *
-   * @param int $block_content_id
-   *   The block content id.
-   * @param \Drupal\Core\Entity\EntityInterface $entity
-   *   The layout entity.
+   * {@inheritdoc}
    */
   public function addUsage($block_content_id, EntityInterface $entity) {
     $this->database->merge('inline_block_usage')
@@ -47,13 +40,7 @@ public function addUsage($block_content_id, EntityInterface $entity) {
   }
 
   /**
-   * Gets unused inline block IDs.
-   *
-   * @param int $limit
-   *   The maximum number of block content entity IDs to return.
-   *
-   * @return int[]
-   *   The entity IDs.
+   * {@inheritdoc}
    */
   public function getUnused($limit = 100) {
     $query = $this->database->select('inline_block_usage', 't');
@@ -64,10 +51,7 @@ public function getUnused($limit = 100) {
   }
 
   /**
-   * Remove usage record by layout entity.
-   *
-   * @param \Drupal\Core\Entity\EntityInterface $entity
-   *   The layout entity.
+   * {@inheritdoc}
    */
   public function removeByLayoutEntity(EntityInterface $entity) {
     $query = $this->database->update('inline_block_usage')
@@ -81,10 +65,7 @@ public function removeByLayoutEntity(EntityInterface $entity) {
   }
 
   /**
-   * Delete the inline blocks' the usage records.
-   *
-   * @param int[] $block_content_ids
-   *   The block content entity IDs.
+   * {@inheritdoc}
    */
   public function deleteUsage(array $block_content_ids) {
     if (!empty($block_content_ids)) {
@@ -94,13 +75,7 @@ public function deleteUsage(array $block_content_ids) {
   }
 
   /**
-   * Gets usage record for inline block by ID.
-   *
-   * @param int $block_content_id
-   *   The block content entity ID.
-   *
-   * @return object
-   *   The usage record with properties layout_entity_id and layout_entity_type.
+   * {@inheritdoc}
    */
   public function getUsage($block_content_id) {
     $query = $this->database->select('inline_block_usage');
diff --git a/core/modules/layout_builder/src/InlineBlockUsageInterface.php b/core/modules/layout_builder/src/InlineBlockUsageInterface.php
new file mode 100644
index 000000000000..f16019166cf9
--- /dev/null
+++ b/core/modules/layout_builder/src/InlineBlockUsageInterface.php
@@ -0,0 +1,60 @@
+<?php
+
+namespace Drupal\layout_builder;
+
+use Drupal\Core\Entity\EntityInterface;
+
+/**
+ * Defines an interface for tracking inline block usage.
+ */
+interface InlineBlockUsageInterface {
+
+  /**
+   * Adds a usage record.
+   *
+   * @param int $block_content_id
+   *   The block content ID.
+   * @param \Drupal\Core\Entity\EntityInterface $entity
+   *   The layout entity.
+   */
+  public function addUsage($block_content_id, EntityInterface $entity);
+
+  /**
+   * Gets unused inline block IDs.
+   *
+   * @param int $limit
+   *   The maximum number of block content entity IDs to return.
+   *
+   * @return int[]
+   *   The entity IDs.
+   */
+  public function getUnused($limit = 100);
+
+  /**
+   * Remove usage record by layout entity.
+   *
+   * @param \Drupal\Core\Entity\EntityInterface $entity
+   *   The layout entity.
+   */
+  public function removeByLayoutEntity(EntityInterface $entity);
+
+  /**
+   * Delete the inline blocks' the usage records.
+   *
+   * @param int[] $block_content_ids
+   *   The block content entity IDs.
+   */
+  public function deleteUsage(array $block_content_ids);
+
+  /**
+   * Gets usage record for inline block by ID.
+   *
+   * @param int $block_content_id
+   *   The block content entity ID.
+   *
+   * @return object
+   *   The usage record with properties layout_entity_id and layout_entity_type.
+   */
+  public function getUsage($block_content_id);
+
+}
diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php
index b7c64dcfbed1..f1c2b67fb272 100644
--- a/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php
+++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php
@@ -264,7 +264,7 @@ public function testInlineBlocksRevisioning() {
   public function testDeletion() {
     /** @var \Drupal\Core\Cron $cron */
     $cron = \Drupal::service('cron');
-    /** @var \Drupal\layout_builder\InlineBlockUsage $usage */
+    /** @var \Drupal\layout_builder\InlineBlockUsageInterface $usage */
     $usage = \Drupal::service('inline_block.usage');
     $this->drupalLogin($this->drupalCreateUser([
       'administer content types',
-- 
GitLab