forked from DesignPatternsPHP/DesignPatternsPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Handler.php
43 lines (35 loc) · 1.03 KB
/
Handler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
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) {
// the request has not been processed by this handler => see the next
if ($this->successor !== null) {
$processed = $this->successor->handle($request);
}
}
return $processed;
}
abstract protected function processing(RequestInterface $request);
}