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:17
created

FilesystemRecorder::replay()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.7666
cc 2
nc 2
nop 1
crap 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