SurrogateKeys   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 36
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addTag() 0 5 2
A setSurrogateHeader() 0 11 2
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\Resource\AbstractUri;
8
use BEAR\Resource\ResourceObject;
9
10
use function array_key_exists;
11
use function array_merge;
12
use function array_unique;
13
use function explode;
14
use function http_build_query;
15
use function implode;
16
use function sprintf;
17
use function str_replace;
18
19
final class SurrogateKeys
20
{
21
    /** @var list<string> */
0 ignored issues
show
Bug introduced by
The type BEAR\QueryRepository\list 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...
22
    private array $surrogateKeys;
23
    private readonly UriTagInterface $uriTag;
24
25
    public function __construct(AbstractUri $uri)
26
    {
27
        $path = str_replace(['/', '\\'], '_', $uri->path);
28
        $uriKey = sprintf('%s_%s', $path, http_build_query($uri->query));
29
        $this->surrogateKeys = [$uriKey];
0 ignored issues
show
Documentation Bug introduced by
It seems like array($uriKey) of type array<integer,string> is incompatible with the declared type BEAR\QueryRepository\list of property $surrogateKeys.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
30
        $this->uriTag = new UriTag();
0 ignored issues
show
Bug introduced by
The property uriTag is declared read-only in BEAR\QueryRepository\SurrogateKeys.
Loading history...
31
    }
32
33
    /**
34
     * Add etag of embedded resource
35
     */
36
    public function addTag(ResourceObject $ro): void
37
    {
38
        $this->surrogateKeys[] = ($this->uriTag)($ro->uri);
39
        if (array_key_exists(Header::SURROGATE_KEY, $ro->headers)) {
40
            $this->surrogateKeys = array_merge($this->surrogateKeys, explode(' ', $ro->headers[Header::SURROGATE_KEY]));
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->surro...eader::SURROGATE_KEY])) of type array is incompatible with the declared type BEAR\QueryRepository\list of property $surrogateKeys.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
        }
42
    }
43
44
    public function setSurrogateHeader(ResourceObject $ro): void
45
    {
46
        $key = implode(' ', array_unique($this->surrogateKeys));
47
        $wasSetManually = isset($ro->headers[Header::SURROGATE_KEY]);
48
        if ($wasSetManually) {
49
            $ro->headers[Header::SURROGATE_KEY] .= ' ' . $key;
50
51
            return;
52
        }
53
54
        $ro->headers[Header::SURROGATE_KEY] = $key;
55
    }
56
}
57