GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PathNamingStrategy   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 75
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A name() 0 20 3
A hash() 0 4 1
A getHeaderHash() 0 12 4
A configureOptions() 0 16 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\Client\Plugin\Vcr\NamingStrategy;
6
7
use Psr\Http\Message\RequestInterface;
8
use Symfony\Component\OptionsResolver\Options;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
11
/**
12
 * Will use the request attributes (hostname, path, headers & body) as filename.
13
 *
14
 * @author Gary PEGEOT <[email protected]>
15
 */
16
class PathNamingStrategy implements NamingStrategyInterface
17
{
18
    /**
19
     * @var array<string, string[]>
20
     */
21
    private $options;
22
23
    /**
24
     * @param array<string, string[]> $options available options:
25
     *                                         - hash_headers:      the list of header names to hash,
26
     *                                         - hash_body_methods: Methods for which the body will be hashed (Default: PUT, POST, PATCH)
27
     */
28 8
    public function __construct(array $options = [])
29
    {
30 8
        $resolver = new OptionsResolver();
31 8
        $this->configureOptions($resolver);
32 8
        $this->options = $resolver->resolve($options);
33 8
    }
34
35 8
    public function name(RequestInterface $request): string
36
    {
37 8
        $parts = [$request->getUri()->getHost()];
38
39 8
        $method = strtoupper($request->getMethod());
40
41 8
        $parts[] = $method;
42 8
        $parts[] = str_replace('/', '_', trim($request->getUri()->getPath(), '/'));
43 8
        $parts[] = $this->getHeaderHash($request);
44
45 8
        if ($query = $request->getUri()->getQuery()) {
46 3
            $parts[] = $this->hash($query);
47
        }
48
49 8
        if (\in_array($method, $this->options['hash_body_methods'], true)) {
50 2
            $parts[] = $this->hash((string) $request->getBody());
51
        }
52
53 8
        return implode('_', array_filter($parts));
54
    }
55
56 8
    private function configureOptions(OptionsResolver $resolver): void
57
    {
58 8
        $resolver->setDefaults([
59 8
            'hash_headers' => [],
60
            'hash_body_methods' => ['PUT', 'POST', 'PATCH'],
61
        ]);
62
63 8
        $resolver->setAllowedTypes('hash_headers', 'string[]');
64 8
        $resolver->setAllowedTypes('hash_body_methods', 'string[]');
65
66
        $normalizer = function (Options $options, $value) {
67 8
            return \is_array($value) ? array_map('strtoupper', $value) : $value;
68 8
        };
69 8
        $resolver->setNormalizer('hash_headers', $normalizer);
70 8
        $resolver->setNormalizer('hash_body_methods', $normalizer);
71 8
    }
72
73 5
    private function hash(string $value): string
74
    {
75 5
        return substr(sha1($value), 0, 5);
76
    }
77
78 8
    private function getHeaderHash(RequestInterface $request): ?string
79
    {
80 8
        $headers = [];
81
82 8
        foreach ($this->options['hash_headers'] as $name) {
83 2
            if ($request->hasHeader($name)) {
84 2
                $headers[] = "$name:".implode(',', $request->getHeader($name));
85
            }
86
        }
87
88 8
        return empty($headers) ? null : $this->hash(implode(';', $headers));
89
    }
90
}
91