Skip to main content
2 of 5
added 244 characters in body
Doc Brown
  • 214k
  • 34
  • 394
  • 603

Separate the construction process in your chain from the actual constructor call by providing a corresponding factory class for each of your handler classes. Each factory constructor stays parameterless, but inside each factory you create the modules with the required services injected.

By using using a strict naming scheme like FirstHandlerFactory for FirstHandler, SecondHandlerFactory for SecondHandler etc, the resulting code will then look like

foreach ( $modulesQueue as $moduleName ) {
    $factoryName = "extensions\\".$moduleName."Factory";
    $startModule->setSuccessor((new $factoryName())->buildModule());
}

The buildModule function then may look like this

 class FirstHandlerFactory
 { 
     function buildModule()
     {
        return new FirstHandler(/* provide the individual services here */);
     }
 }

In your unit tests, you will be able to create your handler objects just with mock services injected, as required for the specific test, without using the factories.

Doc Brown
  • 214k
  • 34
  • 394
  • 603