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.
Completed
Pull Request — master (#3)
by
unknown
05:14 queued 42s
created

PathNamingStrategy   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 70
c 0
b 0
f 0
wmc 10
lcom 1
cbo 3
ccs 35
cts 35
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B name() 0 31 6
A configureOptions() 0 18 2
A hash() 0 4 1
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 path as filename.
13
 *
14
 * @author Gary PEGEOT <[email protected]>
15
 */
16
class PathNamingStrategy implements NamingStrategyInterface
17
{
18
    /**
19
     * @var array
20
     */
21
    private $options;
22
23 9
    public function __construct(array $options = [])
24
    {
25 9
        $resolver = new OptionsResolver();
26 9
        $this->configureOptions($resolver);
27 9
        $this->options = $resolver->resolve($options);
28 9
    }
29
30 8
    public function name(RequestInterface $request): string
31
    {
32 8
        $parts = [$this->options['name_prefix']];
33
34 8
        if ($this->options['hostname_prefix']) {
35 1
            $parts[] = $request->getUri()->getHost();
36
        }
37 8
        $method = strtoupper($request->getMethod());
38
39 8
        $parts[] = $method;
40 8
        $parts[] = str_replace(\DIRECTORY_SEPARATOR, '_', trim($request->getUri()->getPath(), '/'));
41
42 8
        if ($query = $request->getUri()->getQuery()) {
43 3
            $parts[] = $this->hash($query);
44
        }
45
46 8
        if ($this->options['use_headers']) {
47 2
            $headers = '';
48 2
            foreach ($request->getHeaders() as $header => $values) {
49 2
                $headers .= "$header:".implode(',', $values);
50
            }
51
52 2
            $parts[] = $this->hash($headers);
53
        }
54
55 8
        if (\in_array($method, $this->options['hash_body_methods'], true)) {
56 2
            $parts[] = $this->hash((string) $request->getBody());
57
        }
58
59 8
        return implode('_', array_filter($parts));
60
    }
61
62 9
    public function configureOptions(OptionsResolver $resolver): void
63
    {
64 9
        $resolver->setDefaults([
65 9
            'hostname_prefix' => false,
66
            'name_prefix' => '',
67
            'use_headers' => false,
68
            'hash_body_methods' => ['PUT', 'POST', 'PATCH'],
69
        ]);
70
71 9
        $resolver->setAllowedTypes('hostname_prefix', 'bool');
72 9
        $resolver->setAllowedTypes('name_prefix', ['null', 'string']);
73 9
        $resolver->setAllowedTypes('use_headers', 'bool');
74 9
        $resolver->setAllowedTypes('hash_body_methods', 'string[]');
75
76 9
        $resolver->setNormalizer('hash_body_methods', function (Options $options, $value) {
77 9
            return \is_array($value) ? array_map('strtoupper', $value) : $value;
78 9
        });
79 9
    }
80
81 5
    private function hash(string $value): string
82
    {
83 5
        return substr(sha1($value), 0, 5);
84
    }
85
}
86