Completed
Push — master ( b3736d...e8cb60 )
by Matthew
02:30 queued 01:11
created

Psr6Store::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PublishingKit\HttpProxy\Store;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use PublishingKit\HttpProxy\Contracts\StoreInterface;
10
use Psr\Cache\CacheItemPoolInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Cache\CacheItemPoolInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
final class Psr6Store implements StoreInterface
13
{
14
    /**
15
     * @var CacheItemPoolInterface
16
     */
17
    private $cache;
18
19 15
    public function __construct(CacheItemPoolInterface $cache)
20
    {
21 15
        $this->cache = $cache;
22 15
    }
23
24 6
    public function has(ServerRequestInterface $request): bool
25
    {
26 6
        $key = $this->getCacheKey($request);
27 6
        $item = $this->cache->getItem($key);
28 6
        return $item->isHit();
29
    }
30
31 6
    public function get(ServerRequestInterface $request): ?string
32
    {
33 6
        $key = $this->getCacheKey($request);
34 6
        $item = $this->cache->getItem($key);
35 6
        if ($item->isHit()) {
36 3
            return $item->get();
37
        }
38 3
        return null;
39
    }
40
41 3
    public function put(ServerRequestInterface $request, ResponseInterface $response): void
42
    {
43 3
        $key = $this->getCacheKey($request);
44 3
        $item = $this->cache->getItem($key);
45 3
        $item->set($response->getBody()->__toString());
46 3
        $this->cache->save($item);
47 3
    }
48
49 15
    private function getCacheKey(ServerRequestInterface $request): string
50
    {
51 15
        $uri = $request->getUri();
52
        return 'cached-'
53 15
            . trim($uri->getPath(), '/')
54 15
            . ($uri->getQuery() ? '?' . $uri->getQuery() : '')
55 15
            . '.html';
56
    }
57
}
58