mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-26 01:31:20 +02:00
33 lines
849 B
PHP
33 lines
849 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace DesignPatterns\Behavioral\ChainOfResponsibilities;
|
|
|
|
use Psr\Http\Message\RequestInterface;
|
|
|
|
abstract class Handler
|
|
{
|
|
public function __construct(private ?Handler $successor = null)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* This approach by using a template method pattern ensures you that
|
|
* each subclass will not forget to call the successor
|
|
*/
|
|
final public function handle(RequestInterface $request): ?string
|
|
{
|
|
$processed = $this->processing($request);
|
|
|
|
if ($processed === null && $this->successor !== null) {
|
|
// the request has not been processed by this handler => see the next
|
|
$processed = $this->successor->handle($request);
|
|
}
|
|
|
|
return $processed;
|
|
}
|
|
|
|
abstract protected function processing(RequestInterface $request): ?string;
|
|
}
|