mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-09-25 22:09:23 +02:00
43 lines
1.0 KiB
PHP
43 lines
1.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace DesignPatterns\Behavioral\ChainOfResponsibilities;
|
|
|
|
use Psr\Http\Message\RequestInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
abstract class Handler
|
|
{
|
|
/**
|
|
* @var Handler|null
|
|
*/
|
|
private $successor = null;
|
|
|
|
public function __construct(Handler $handler = null)
|
|
{
|
|
$this->successor = $handler;
|
|
}
|
|
|
|
/**
|
|
* This approach by using a template method pattern ensures you that
|
|
* each subclass will not forget to call the successor
|
|
*
|
|
* @param RequestInterface $request
|
|
*
|
|
* @return string|null
|
|
*/
|
|
final public function handle(RequestInterface $request)
|
|
{
|
|
$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);
|
|
}
|