Design Patterns PHP - Chain of responsibility Java

1) What we have done?

Because code of passing "Request" for the next "Handler" is the same for every "Handler" object.
We just move this to the parent abstract "Handler" class


abstract class Handler
{
    /**
     * @var Handler
     */
    protected $handler;

    public function setHandler(Handler $handler)
    {
        $this->handler = $handler;
    }

    public function handleRequest(Request $request)
    {
        if ($this->canHandle($request)) {
            $this->handle($request);
        } else {
            $this->handler->handleRequest($request);
        }
    }

    /**
     * @param Request
     * @return boolean
     */
    protected abstract function canHandle(Request $request);

    protected abstract function handle(Request $request);
}

class HandlerA extends Handler
{
    /**
     * @param Request
     * @return boolean
     */
    protected function canHandle(Request $request)
    {
        return $request->getAttribute() == 'handlerA';
    }

    protected function handle(Request $request)
    {
        echo 'handlerA doing something with request' . PHP_EOL;
    }
}

class HandlerB extends Handler
{
    /**
     * @param Request
     * @return boolean
     */
    protected function canHandle(Request $request)
    {
        return $request->getAttribute() == 'handlerB';
    }

    protected function handle(Request $request)
    {
        echo 'handlerB doing something with request' . PHP_EOL;
    }
}