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.

ReadableFileStrategy   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 7
eloc 16
dl 0
loc 45
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A storeResponse() 0 3 1
A __construct() 0 3 1
A storeRequest() 0 3 1
A storeContent() 0 5 1
A getPromise() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EmanueleMinotto\GuzzleSnapshot\Strategy;
6
7
use GuzzleHttp\Promise\FulfilledPromise;
8
use GuzzleHttp\Promise\PromiseInterface;
9
use function GuzzleHttp\Psr7\parse_response;
10
use function GuzzleHttp\Psr7\str;
11
use Psr\Http\Message\RequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
use Throwable;
14
15
class ReadableFileStrategy implements StrategyInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private $directory;
21
22 12
    public function __construct(string $directory)
23
    {
24 12
        $this->directory = $directory;
25 12
    }
26
27 6
    public function getPromise(RequestInterface $request): ?PromiseInterface
28
    {
29 6
        $id = sha1(str($request));
30
31 6
        $responseFile = sprintf('%s/%s-response.txt', $this->directory, $id);
32 6
        if (!file_exists($responseFile)) {
33 3
            return null;
34
        }
35
36
        try {
37 3
            return new FulfilledPromise(
38 3
                parse_response(file_get_contents($responseFile))
39
            );
40
        } catch (Throwable $exception) {
41
            return null;
42
        }
43
    }
44
45 3
    public function storeRequest(RequestInterface $request): void
46
    {
47 3
        $this->storeContent($request, 'request', str($request));
48 3
    }
49
50 3
    public function storeResponse(RequestInterface $request, ResponseInterface $response): void
51
    {
52 3
        $this->storeContent($request, 'response', str($response));
53 3
    }
54
55 6
    private function storeContent(RequestInterface $request, string $type, string $content): void
56
    {
57 6
        $id = sha1(str($request));
58 6
        $filename = sprintf('%s/%s-%s.txt', $this->directory, $id, $type);
59 6
        file_put_contents($filename, $content);
60 6
    }
61
}
62