8
8
use Psr \Http \Message \ResponseInterface ;
9
9
use Psr \Log \LoggerAwareInterface ;
10
10
use Psr \Log \LoggerAwareTrait ;
11
+ use Psr \Log \NullLogger ;
11
12
use Symfony \Component \Filesystem \Exception \IOException ;
12
13
use Symfony \Component \Filesystem \Filesystem ;
13
14
@@ -32,10 +33,13 @@ final class FilesystemRecorder implements RecorderInterface, PlayerInterface, Lo
32
33
private $ filesystem ;
33
34
34
35
/**
35
- * @var array
36
+ * @var array<string, string>
36
37
*/
37
38
private $ filters ;
38
39
40
+ /**
41
+ * @param array<string, string> $filters
42
+ */
39
43
public function __construct (string $ directory , ?Filesystem $ filesystem = null , array $ filters = [])
40
44
{
41
45
$ this ->filesystem = $ filesystem ?? new Filesystem ();
@@ -50,6 +54,7 @@ public function __construct(string $directory, ?Filesystem $filesystem = null, a
50
54
51
55
$ this ->directory = realpath ($ directory ).\DIRECTORY_SEPARATOR ;
52
56
$ this ->filters = $ filters ;
57
+ $ this ->logger = new NullLogger ();
53
58
}
54
59
55
60
public function replay (string $ name ): ?ResponseInterface
@@ -65,24 +70,32 @@ public function replay(string $name): ?ResponseInterface
65
70
66
71
$ this ->log ('Response replayed from {filename} ' , $ context );
67
72
68
- return Psr7 \parse_response (file_get_contents ($ filename ));
73
+ if (false === $ content = file_get_contents ($ filename )) {
74
+ throw new \RuntimeException (sprintf ('Unable to read "%s" file content ' , $ filename ));
75
+ }
76
+
77
+ return Psr7 \parse_response ($ content );
69
78
}
70
79
71
80
public function record (string $ name , ResponseInterface $ response ): void
72
81
{
73
82
$ filename = "{$ this ->directory }$ name.txt " ;
74
83
$ context = compact ('name ' , 'filename ' );
75
84
76
- $ content = preg_replace (array_keys ($ this ->filters ), array_values ($ this ->filters ), Psr7 \str ($ response ));
85
+ if (null === $ content = preg_replace (array_keys ($ this ->filters ), array_values ($ this ->filters ), Psr7 \str ($ response ))) {
86
+ throw new \RuntimeException ('Some of the provided response filters are invalid. ' );
87
+ }
88
+
77
89
$ this ->filesystem ->dumpFile ($ filename , $ content );
78
90
79
91
$ this ->log ('Response for {name} stored into {filename} ' , $ context );
80
92
}
81
93
94
+ /**
95
+ * @param array<string, string> $context
96
+ */
82
97
private function log (string $ message , array $ context = []): void
83
98
{
84
- if ($ this ->logger ) {
85
- $ this ->logger ->debug ("[VCR-PLUGIN][FilesystemRecorder] $ message " , $ context );
86
- }
99
+ $ this ->logger ->debug ("[VCR-PLUGIN][FilesystemRecorder] $ message " , $ context );
87
100
}
88
101
}
0 commit comments