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
02:19
created

FilesystemRecorder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
dl 0
loc 62
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3
ccs 23
cts 26
cp 0.8846
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A replay() 0 15 2
A record() 0 9 1
A log() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\Client\Plugin\Vcr\Recorder;
6
7
use GuzzleHttp\Psr7;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Log\LoggerAwareInterface;
10
use Psr\Log\LoggerAwareTrait;
11
use Symfony\Component\Filesystem\Exception\IOException;
12
use Symfony\Component\Filesystem\Filesystem;
13
14
/**
15
 * Stores responses in a defined directory, preferably accessible to your VCS.
16
 *
17
 * @author Gary PEGEOT <[email protected]>
18
 */
19
final class FilesystemRecorder implements RecorderInterface, PlayerInterface, LoggerAwareInterface
20
{
21
    use LoggerAwareTrait;
22
23
    /**
24
     * @var string
25
     */
26
    private $directory;
27
28
    /**
29
     * @var Filesystem
30
     */
31
    private $filesystem;
32
33 2
    public function __construct(string $directory, ?Filesystem $filesystem = null)
34
    {
35 2
        $this->filesystem = $filesystem ?? new Filesystem();
36
37 2
        if (!$this->filesystem->exists($directory)) {
38
            try {
39
                $this->filesystem->mkdir($directory);
40
            } catch (IOException $e) {
41
                throw new \InvalidArgumentException("Unable to create directory \"$directory\"/: {$e->getMessage()}", $e->getCode(), $e);
42
            }
43
        }
44
45 2
        $this->directory = realpath($directory).\DIRECTORY_SEPARATOR;
46 2
    }
47
48 2
    public function replay(string $name): ?ResponseInterface
49
    {
50 2
        $filename = "{$this->directory}$name.txt";
51 2
        $context = compact('filename');
52
53 2
        if (!$this->filesystem->exists($filename)) {
54 1
            $this->log('Unable to replay {filename}', $context);
55
56 1
            return null;
57
        }
58
59 1
        $this->log('Response replayed from {filename}', $context);
60
61 1
        return Psr7\parse_response(file_get_contents($filename));
62
    }
63
64 1
    public function record(string $name, ResponseInterface $response): void
65
    {
66 1
        $filename = "{$this->directory}$name.txt";
67 1
        $context = compact('name', 'filename');
68
69 1
        $this->filesystem->dumpFile($filename, Psr7\str($response));
70
71 1
        $this->log('Response for {name} stored into {filename}', $context);
72 1
    }
73
74 2
    private function log(string $message, array $context = []): void
75
    {
76 2
        if ($this->logger) {
77 1
            $this->logger->debug("[VCR-PLUGIN][FilesystemRecorder] $message", $context);
78
        }
79 2
    }
80
}
81