1 | <?php |
||||
2 | |||||
3 | namespace Sludio\HelperBundle\Guzzle\Cache; |
||||
4 | |||||
5 | use GuzzleHttp\Psr7; |
||||
6 | use Psr\Http\Message\RequestInterface; |
||||
7 | use Psr\Http\Message\ResponseInterface; |
||||
8 | use Sludio\HelperBundle\Guzzle\Cache\NamingStrategy\NamingStrategyInterface; |
||||
9 | use Sludio\HelperBundle\Guzzle\Cache\NamingStrategy\SubfolderNamingStrategy; |
||||
10 | use Sludio\HelperBundle\Guzzle\GuzzleHttp\Middleware\CacheMiddleware; |
||||
11 | use Sludio\HelperBundle\Guzzle\GuzzleHttp\Middleware\MockMiddleware; |
||||
12 | use Symfony\Component\Filesystem\Filesystem; |
||||
13 | |||||
14 | class MockStorageAdapter implements StorageAdapterInterface |
||||
15 | { |
||||
16 | private $storagePath; |
||||
17 | /** @var NamingStrategyInterface[] */ |
||||
18 | private $namingStrategies = []; |
||||
19 | private $responseHeadersBlacklist = [ |
||||
20 | CacheMiddleware::DEBUG_HEADER, |
||||
21 | MockMiddleware::DEBUG_HEADER, |
||||
22 | ]; |
||||
23 | |||||
24 | /** |
||||
25 | * @param string $storagePath |
||||
26 | * @param array $requestHeadersBlacklist |
||||
27 | * @param array $responseHeadersBlacklist |
||||
28 | */ |
||||
29 | public function __construct($storagePath, array $requestHeadersBlacklist = [], array $responseHeadersBlacklist = []) |
||||
30 | { |
||||
31 | $this->storagePath = $storagePath; |
||||
32 | |||||
33 | $this->namingStrategies[] = new SubfolderNamingStrategy($requestHeadersBlacklist); |
||||
34 | |||||
35 | if (!empty($responseHeadersBlacklist)) { |
||||
36 | $this->responseHeadersBlacklist = $responseHeadersBlacklist; |
||||
37 | } |
||||
38 | } |
||||
39 | |||||
40 | /** |
||||
41 | * {@inheritdoc} |
||||
42 | */ |
||||
43 | public function fetch(RequestInterface $request) |
||||
44 | { |
||||
45 | foreach ($this->namingStrategies as $strategy) { |
||||
46 | if (file_exists($filename = $this->getFilename($strategy->filename($request)))) { |
||||
47 | return Psr7\parse_response(file_get_contents($filename)); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
48 | } |
||||
49 | } |
||||
50 | |||||
51 | return null; |
||||
52 | } |
||||
53 | |||||
54 | /** |
||||
55 | * Prefixes the generated file path with the adapter's storage path. |
||||
56 | * |
||||
57 | * @param string $name |
||||
58 | * |
||||
59 | * @return string The path to the mock file |
||||
60 | */ |
||||
61 | private function getFilename($name) |
||||
62 | { |
||||
63 | return $this->storagePath.'/'.$name.'.txt'; |
||||
64 | } |
||||
65 | |||||
66 | /** |
||||
67 | * {@inheritdoc} |
||||
68 | * @throws \Symfony\Component\Filesystem\Exception\IOException |
||||
69 | * @throws \RuntimeException |
||||
70 | */ |
||||
71 | public function save(RequestInterface $request, ResponseInterface $response) |
||||
72 | { |
||||
73 | foreach ($this->responseHeadersBlacklist as $header) { |
||||
74 | $response = $response->withoutHeader($header); |
||||
75 | } |
||||
76 | |||||
77 | list($strategy) = $this->namingStrategies; |
||||
78 | |||||
79 | $filename = $this->getFilename($strategy->filename($request)); |
||||
80 | |||||
81 | $fileSys = new Filesystem(); |
||||
82 | $fileSys->mkdir(\dirname($filename)); |
||||
83 | |||||
84 | file_put_contents($filename, Psr7\str($response)); |
||||
0 ignored issues
–
show
The function
str was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
85 | $response->getBody()->rewind(); |
||||
86 | } |
||||
87 | } |
||||
88 |