|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Guzzle\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Sludio\HelperBundle\Guzzle\Cache\NamingStrategy\NamingStrategyInterface; |
|
6
|
|
|
use Sludio\HelperBundle\Guzzle\Cache\NamingStrategy\SubfolderNamingStrategy; |
|
7
|
|
|
use Sludio\HelperBundle\Guzzle\GuzzleHttp\Middleware\CacheMiddleware; |
|
8
|
|
|
use Sludio\HelperBundle\Guzzle\GuzzleHttp\Middleware\MockMiddleware; |
|
9
|
|
|
use GuzzleHttp\Psr7; |
|
10
|
|
|
use Psr\Http\Message\RequestInterface; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
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)); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function save(RequestInterface $request, ResponseInterface $response) |
|
56
|
|
|
{ |
|
57
|
|
|
foreach ($this->responseHeadersBlacklist as $header) { |
|
58
|
|
|
$response = $response->withoutHeader($header); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
list($strategy) = $this->namingStrategies; |
|
62
|
|
|
|
|
63
|
|
|
$filename = $this->getFilename($strategy->filename($request)); |
|
64
|
|
|
|
|
65
|
|
|
$fs = new Filesystem(); |
|
|
|
|
|
|
66
|
|
|
$fs->mkdir(dirname($filename)); |
|
67
|
|
|
|
|
68
|
|
|
file_put_contents($filename, Psr7\str($response)); |
|
69
|
|
|
$response->getBody()->rewind(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Prefixes the generated file path with the adapter's storage path. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $name |
|
76
|
|
|
* |
|
77
|
|
|
* @return string The path to the mock file |
|
78
|
|
|
*/ |
|
79
|
|
|
private function getFilename($name) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->storagePath.'/'.$name.'.txt'; |
|
82
|
|
|
} |
|
83
|
|
|
} |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.