PageCacheMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 18.75%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
eloc 17
c 3
b 0
f 0
dl 0
loc 26
ccs 3
cts 16
cp 0.1875
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 24 3
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