88use Psr \Http \Message \ResponseInterface ;
99use Psr \Log \LoggerAwareInterface ;
1010use Psr \Log \LoggerAwareTrait ;
11+ use Psr \Log \NullLogger ;
1112use Symfony \Component \Filesystem \Exception \IOException ;
1213use Symfony \Component \Filesystem \Filesystem ;
1314
@@ -32,10 +33,13 @@ final class FilesystemRecorder implements RecorderInterface, PlayerInterface, Lo
3233 private $ filesystem ;
3334
3435 /**
35- * @var array
36+ * @var array<string, string>
3637 */
3738 private $ filters ;
3839
40+ /**
41+ * @param array<string, string> $filters
42+ */
3943 public function __construct (string $ directory , ?Filesystem $ filesystem = null , array $ filters = [])
4044 {
4145 $ this ->filesystem = $ filesystem ?? new Filesystem ();
@@ -50,6 +54,7 @@ public function __construct(string $directory, ?Filesystem $filesystem = null, a
5054
5155 $ this ->directory = realpath ($ directory ).\DIRECTORY_SEPARATOR ;
5256 $ this ->filters = $ filters ;
57+ $ this ->logger = new NullLogger ();
5358 }
5459
5560 public function replay (string $ name ): ?ResponseInterface
@@ -65,24 +70,32 @@ public function replay(string $name): ?ResponseInterface
6570
6671 $ this ->log ('Response replayed from {filename} ' , $ context );
6772
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 );
6978 }
7079
7180 public function record (string $ name , ResponseInterface $ response ): void
7281 {
7382 $ filename = "{$ this ->directory }$ name.txt " ;
7483 $ context = compact ('name ' , 'filename ' );
7584
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+
7789 $ this ->filesystem ->dumpFile ($ filename , $ content );
7890
7991 $ this ->log ('Response for {name} stored into {filename} ' , $ context );
8092 }
8193
94+ /**
95+ * @param array<string, string> $context
96+ */
8297 private function log (string $ message , array $ context = []): void
8398 {
84- if ($ this ->logger ) {
85- $ this ->logger ->debug ("[VCR-PLUGIN][FilesystemRecorder] $ message " , $ context );
86- }
99+ $ this ->logger ->debug ("[VCR-PLUGIN][FilesystemRecorder] $ message " , $ context );
87100 }
88101}
0 commit comments