Code refactor and modernization

Created on 13 April 2023, over 1 year ago
Updated 21 April 2023, over 1 year ago

Problem/Motivation

The purpose of this issue is to refactor the code for the 3.0.x branch to move as much of the functionality into services utilizing dependency injection as possible. As I dig into the code as a whole I'll add a bullet list of items being addressed here.

The hope is that this refactor will not only set the module up for long term stability but also pave the way for many of the enhancement issues to be tackled in a cleaner manner.

Dependency injection candidates:

src/Plugin/QueueWorker/SubscriptionBuilderBase.php

\Drupal::database()

database:
    class: Drupal\Core\Database\Connection
    factory: Drupal\Core\Database\Database::getConnection
    arguments: [default]

\Drupal::configFactory()

config.factory:
    class: Drupal\Core\Config\ConfigFactory
    tags:
      - { name: event_subscriber }
      - { name: service_collector, tag: 'config.factory.override', call: addOverride }
    arguments: ['@config.storage', '@event_dispatcher', '@config.typed']

\Drupal::logger()

logger.factory:
    class: Drupal\Core\Logger\LoggerChannelFactory
    parent: container.trait
    tags:
      - { name: service_collector, tag: logger, call: addLogger }

\Drupal::service('queue')

 queue:
    class: Drupal\Core\Queue\QueueFactory
    arguments: ['@settings']
    calls:
      - [setContainer, ['@service_container']]

src/Plugin/QueueWorker/ImmediateEmailBase.php

\Drupal::database()

database:
    class: Drupal\Core\Database\Connection
    factory: Drupal\Core\Database\Database::getConnection
    arguments: [default]

\Drupal::config('system.theme')

(::config utilizes ::configFactory so maybe just use that instead)

config.factory:
    class: Drupal\Core\Config\ConfigFactory
    tags:
      - { name: event_subscriber }
      - { name: service_collector, tag: 'config.factory.override', call: addOverride }
    arguments: ['@config.storage', '@event_dispatcher', '@config.typed']

\Drupal::service('theme.initialization')

theme.initialization:
    class: Drupal\Core\Theme\ThemeInitialization
    arguments: ['%app.root%', '@theme_handler', '@cache.bootstrap', '@module_handler']

\Drupal::entityTypeManager()

entity_type.manager:
    class: Drupal\Core\Entity\EntityTypeManager
    arguments: ['@container.namespaces', '@module_handler', '@cache.discovery', '@string_translation', '@class_resolver', '@entity.last_installed_schema.repository']
    parent: container.trait
    tags:
      - { name: plugin_manager_cache_clear }

src/Plugin/QueueWorker/ImmediateAnnouncementBase.php

\Drupal::database()

database:
    class: Drupal\Core\Database\Connection
    factory: Drupal\Core\Database\Database::getConnection
    arguments: [default]

\Drupal::config('system.theme')

(::config utilizes ::configFactory so maybe just use that instead)

config.factory:
    class: Drupal\Core\Config\ConfigFactory
    tags:
      - { name: event_subscriber }
      - { name: service_collector, tag: 'config.factory.override', call: addOverride }
    arguments: ['@config.storage', '@event_dispatcher', '@config.typed']

\Drupal::service('theme.initialization')

theme.initialization:
    class: Drupal\Core\Theme\ThemeInitialization
    arguments: ['%app.root%', '@theme_handler', '@cache.bootstrap', '@module_handler']

\Drupal::entityTypeManager()

entity_type.manager:
    class: Drupal\Core\Entity\EntityTypeManager
    arguments: ['@container.namespaces', '@module_handler', '@cache.discovery', '@string_translation', '@class_resolver', '@entity.last_installed_schema.repository']
    parent: container.trait
    tags:
      - { name: plugin_manager_cache_clear }

src/Plugin/QueueWorker/DailyEmailBase.php

\Drupal::database()

database:
    class: Drupal\Core\Database\Connection
    factory: Drupal\Core\Database\Database::getConnection
    arguments: [default]

\Drupal::config('system.site') and \Drupal::config('system.theme')

(::config utilizes ::configFactory so maybe just use that instead)

config.factory:
    class: Drupal\Core\Config\ConfigFactory
    tags:
      - { name: event_subscriber }
      - { name: service_collector, tag: 'config.factory.override', call: addOverride }
    arguments: ['@config.storage', '@event_dispatcher', '@config.typed']

\Drupal::service('theme.initialization')

theme.initialization:
    class: Drupal\Core\Theme\ThemeInitialization
    arguments: ['%app.root%', '@theme_handler', '@cache.bootstrap', '@module_handler']

\Drupal::entityTypeManager()

entity_type.manager:
    class: Drupal\Core\Entity\EntityTypeManager
    arguments: ['@container.namespaces', '@module_handler', '@cache.discovery', '@string_translation', '@class_resolver', '@entity.last_installed_schema.repository']
    parent: container.trait
    tags:
      - { name: plugin_manager_cache_clear }

src/Plugin/QueueWorker/AnnouncementBuilderBase.php

\Drupal::configFactory()

config.factory:
    class: Drupal\Core\Config\ConfigFactory
    tags:
      - { name: event_subscriber }
      - { name: service_collector, tag: 'config.factory.override', call: addOverride }
    arguments: ['@config.storage', '@event_dispatcher', '@config.typed']

\Drupal::logger()

logger.factory:
    class: Drupal\Core\Logger\LoggerChannelFactory
    parent: container.trait
    tags:
      - { name: service_collector, tag: logger, call: addLogger }

\Drupal::database()

database:
    class: Drupal\Core\Database\Connection
    factory: Drupal\Core\Database\Database::getConnection
    arguments: [default]

\Drupal::service('queue')

 queue:
    class: Drupal\Core\Queue\QueueFactory
    arguments: ['@settings']
    calls:
      - [setContainer, ['@service_container']]

src/Plugin/Block/GroupSubscriptionBlock.php

\Drupal::currentUser()

current_user:
    class: Drupal\Core\Session\AccountProxy
    arguments: ['@event_dispatcher']

\Drupal::formBuilder()

form_builder:
    class: Drupal\Core\Form\FormBuilder
    arguments: ['@form_validator', '@form_submitter', '@form_cache', '@module_handler', '@event_dispatcher', '@request_stack', '@class_resolver', '@element_info', '@theme.manager', '@?csrf_token']
  

src/Form/UserSettings.php

\Drupal::routeMatch()

\Drupal::database()

\Drupal::service('group.membership_loader')

\Drupal::configFactory()

\Drupal::logger()

src/Form/GroupSubscriptionBlockForm.php

\Drupal::service('path.current')

\Drupal::currentUser()

\Drupal::database()

\Drupal::configFactory()

\Drupal::logger()

📌 Task
Status

Fixed

Version

3.0

Component

Code

Created by

🇨🇦Canada JayDarnell Guelph, Ontario

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024