PageCacheMiddleware::process()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 7.8273

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 16
c 3
b 0
f 0
dl 0
loc 24
ccs 3
cts 16
cp 0.1875
rs 9.7333
cc 3
nc 3
nop 2
crap 7.8273
1
<?php
2
declare(strict_types=1);
3
4
namespace Ctw\Middleware\PageCacheMiddleware;
5
6
use Laminas\Diactoros\Response\ArraySerializer as ResponseSerializer;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
11
class PageCacheMiddleware extends AbstractPageCacheMiddleware
12
{
13 2
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
14
    {
15 2
        if (!$this->shouldCache($request)) {
16 2
            return $handler->handle($request);
17
        }
18
19
        $idGenerator = $this->getIdGenerator();
20
        $cache       = $this->getStorageAdapter();
21
22
        $cacheId    = $idGenerator->generate($request);
23
        $serialized = $cache->getItem($cacheId, $success, $casToken);
24
25
        if (!$success) {
26
            $cacheStatus = self::STATUS_MISS;
27
            $response    = $handler->handle($request);
28
            $serialized  = ResponseSerializer::toArray($response);
29
            $cache->setItem($cacheId, $serialized);
30
        } else {
31
            $cacheStatus = self::STATUS_HIT;
32
            assert(is_array($serialized));
33
            $response    = ResponseSerializer::fromArray($serialized);
34
        }
35
36
        return $response->withHeader('X-Page-Cache', $cacheStatus);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response->withHe...e-Cache', $cacheStatus) could return the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
37
    }
38
}
39