By Keven


2019-03-14 19:42:05 8 Comments

I'm trying to figure out the best way to structure a custom module that uses the Queue API in D8. I'm new to D8 and a bit rusty with OOP so forgive me if I'm breaking some recommended design pattern here...

I created a single custom module where multiple different queues need to process items differently, but each queue has common functionality that they may need to use between each other. So my question is, should I create an abstract base class that extends QueueWorkerBase, make processItem abstract, and then add common functionality to the base class?

Something like this:

In src/Plugin/QueueWorker/ProcessQueueBase.php

abstract class ProcessQueueBase extends QueueWorkerBase {

  /**
   * @inheritDoc
   */
  abstract public function processItem($data);

  protected function doSomethingCommon(){
    // Some common logic
  }
}

In src/Plugin/QueueWorker/ProcessQueueOne.php

class ProcessQueueOne extends ProcessQueueBase {

  /**
   * @inheritDoc
   */
  public function processItem($data){
    // Do stuff now

    parent::doSomethingCommon();
  }    
}

In src/Plugin/QueueWorker/ProcessQueueTwo.php

class ProcessQueueTwo extends ProcessQueueBase {

  /**
   * @inheritDoc
   */
  public function processItem($data){
    // Do stuff now

    parent::doSomethingCommon();
  }    
}

OR should I create separate classes that all extend QueueWorkerBase and then just create services for the common functionality.

Something like this:

In DoSomethingService.php:

class DoSomethingService {

  public function doSomethingCommon(){
    // Do something
  }

}

In src/Plugin/QueueWorker/ProcessQueueOne.php

class ProcessQueueOne extends QueueWorkerBase {

  /**
   * @inheritDoc
   */
  public function processItem($data){
    // Do stuff

    $do_something = \Drupal::service('mymodule.doSomething');
    $do_something->doSomethingCommon();
  }    
}

In src/Plugin/QueueWorker/ProcessQueueTwo.php

class ProcessQueueTwo extends QueueWorkerBase {

  /**
   * @inheritDoc
   */
  public function processItem($data){
    // Do stuff

    $do_something = \Drupal::service('mymodule.doSomething');
    $do_something ->doSomethingCommon();
  }    
}

1 comments

@Steven 2019-03-14 21:04:26

I would suggest an abstract base class, but instead of creating another function ::doSomethingCommon, partially implement ProcessQueueBase::processItem and call parent::processItem from the subclasses.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Cron Queue API - Exception in worker callback prevents queue item from being removed

  • 2018-03-08 18:00:59
  • CodeWatson
  • 164 View
  • 2 Score
  • 1 Answer
  • Tags:   cron queue

1 Answered Questions

[SOLVED] Access data from previously submitted values in buildForm in FormBase plugin

  • 2018-03-12 14:37:46
  • Matt
  • 289 View
  • 0 Score
  • 1 Answer
  • Tags:   8 plugins

3 Answered Questions

2 Answered Questions

4 Answered Questions

[SOLVED] How to get the list of field-types?

  • 2015-11-09 16:06:28
  • cFreed
  • 7683 View
  • 10 Score
  • 4 Answer
  • Tags:   entities 8

0 Answered Questions

Custom Fields not displayed

  • 2016-08-10 11:53:46
  • Jitha M Saroj
  • 720 View
  • 1 Score
  • 0 Answer
  • Tags:   8

0 Answered Questions

How to access the values of custom Widget form fields?

  • 2016-08-25 10:37:31
  • Jitha M Saroj
  • 706 View
  • 0 Score
  • 0 Answer
  • Tags:   8

0 Answered Questions

Drupal Queue is not processing with hook cron

  • 2014-08-13 12:35:00
  • Mamadou
  • 606 View
  • 1 Score
  • 0 Answer
  • Tags:   cron queue

1 Answered Questions

[SOLVED] Queue doesn't enter on worker function with Elysia cron

  • 2014-04-07 08:56:34
  • arrubiu
  • 726 View
  • 2 Score
  • 1 Answer
  • Tags:   cron queue

3 Answered Questions

[SOLVED] How to manage / consume multiple Queue API queues in a module?

  • 2011-06-02 23:16:44
  • geerlingguy
  • 1851 View
  • 0 Score
  • 3 Answer
  • Tags:   cron queue

Sponsored Content