Completed
Push — master ( f7424d...b53e51 )
by Alejandro
07:43
created

AbstractRestAction::dispatch()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
1
<?php
2
namespace Shlinkio\Shlink\Rest\Action;
3
4
use Fig\Http\Message\RequestMethodInterface;
5
use Fig\Http\Message\StatusCodeInterface;
6
use Interop\Http\ServerMiddleware\DelegateInterface;
7
use Interop\Http\ServerMiddleware\MiddlewareInterface;
8
use Psr\Http\Message\ResponseInterface as Response;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
use Psr\Log\LoggerInterface;
11
use Psr\Log\NullLogger;
12
use Zend\Diactoros\Response\EmptyResponse;
13
14
abstract class AbstractRestAction implements MiddlewareInterface, RequestMethodInterface, StatusCodeInterface
15
{
16
    /**
17
     * @var LoggerInterface
18
     */
19
    protected $logger;
20
21 20
    public function __construct(LoggerInterface $logger = null)
22
    {
23 20
        $this->logger = $logger ?: new NullLogger();
24 20
    }
25
26
    /**
27
     * Process an incoming server request and return a response, optionally delegating
28
     * to the next middleware component to create the response.
29
     *
30
     * @param Request $request
31
     * @param DelegateInterface $delegate
32
     *
33
     * @return Response
34
     */
35 20
    public function process(Request $request, DelegateInterface $delegate)
36
    {
37 20
        if ($request->getMethod() === self::METHOD_OPTIONS) {
38
            return new EmptyResponse();
39
        }
40
41 20
        return $this->dispatch($request, $delegate);
42
    }
43
44
    /**
45
     * @param Request $request
46
     * @param DelegateInterface $delegate
47
     * @return null|Response
48
     */
49
    abstract protected function dispatch(Request $request, DelegateInterface $delegate);
50
}
51