MediaWiki master
MiddlewareChain.php
Go to the documentation of this file.
1<?php
3
4use Wikimedia\ObjectFactory\ObjectFactory;
5
13 private array $middleware = [];
14
15 private ObjectFactory $objectFactory;
16 private array $middlewareSpecs;
17 private bool $loadedFromSpecs = false;
18
19 private bool $isProcessing = false;
20
21 public function __construct( ObjectFactory $objectFactory, array $specs ) {
22 $this->objectFactory = $objectFactory;
23 $this->middlewareSpecs = $specs;
24 }
25
26 public function process( NotificationsBatch $batch ): NotificationsBatch {
27 if ( $this->isProcessing ) {
28 // If you need to send an additional notification from middleware,
29 // use NotificationBatch::add() instead of calling NotificationService from middleware
30 throw new MiddlewareException(
31 "Middleware cannot re-trigger the notification processing while it is already in progress. "
32 );
33 }
34 $this->isProcessing = true;
35
36 if ( !$this->loadedFromSpecs ) {
37 foreach ( $this->middlewareSpecs as $spec ) {
38 $this->middleware[] = $this->objectFactory->createObject( $spec, [
39 'assertClass' => NotificationMiddlewareInterface::class
40 ] );
41 }
42 $this->loadedFromSpecs = true;
43 }
44
45 $this->callNext( $batch, 0 );
46 $this->isProcessing = false;
47 return $batch;
48 }
49
50 private function callNext( NotificationsBatch $batch, int $index ): void {
51 if ( $index < count( $this->middleware ) ) {
52 $this->middleware[$index]->handle(
53 $batch,
54 function () use ( $batch, $index ) {
55 $this->callNext( $batch, $index + 1 );
56 }
57 );
58 }
59 }
60}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
__construct(ObjectFactory $objectFactory, array $specs)
An object representing notification with list of recipients.