1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Client\Plugin\Vcr; |
4
|
|
|
|
5
|
|
|
use Http\Client\Plugin\Plugin; |
6
|
|
|
use Http\Client\Plugin\Vcr\Exception\CannotBeReplayed; |
7
|
|
|
use Http\Client\Plugin\Vcr\Exception\NotFound; |
8
|
|
|
use Http\Promise\FulfilledPromise; |
9
|
|
|
use Http\Promise\RejectedPromise; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
|
13
|
|
|
class VcrPlugin implements Plugin |
14
|
|
|
{ |
15
|
|
|
const HEADER_VCR = 'X-VCR'; |
16
|
|
|
const HEADER_VCR_REPLAY = 'replay'; |
17
|
|
|
const HEADER_VCR_RECORDED = 'recorded'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Vcr |
21
|
|
|
*/ |
22
|
|
|
private $vcr; |
23
|
|
|
|
24
|
6 |
|
public function __construct(Vcr $vcr) |
25
|
|
|
{ |
26
|
6 |
|
$this->vcr = $vcr; |
27
|
6 |
|
} |
28
|
|
|
|
29
|
5 |
|
public function handleRequest(RequestInterface $request, callable $next, callable $first) |
30
|
|
|
{ |
31
|
|
|
try { |
32
|
5 |
|
return $this->replay($request); |
33
|
3 |
|
} catch (CannotBeReplayed $e) { |
34
|
3 |
|
$this->record($request); |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
return $next($request)->then($this->onFulfilled($request), $this->onRejected($request)); |
38
|
|
|
} |
39
|
|
|
|
40
|
5 |
|
private function replay(RequestInterface $request) |
41
|
|
|
{ |
42
|
5 |
|
if (!$this->vcr->isTurnedOn() || !$this->vcr->hasTape()) { |
43
|
1 |
|
throw new CannotBeReplayed(); |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
$tape = $this->vcr->getTape(); |
47
|
|
|
|
48
|
|
|
try { |
49
|
4 |
|
$track = $tape->findTrackByRequest($request); |
50
|
4 |
|
} catch (NotFound $e) { |
51
|
1 |
|
throw new CannotBeReplayed(); |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
if ($track->hasException()) { |
55
|
1 |
|
return new RejectedPromise($track->getException()); |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
if ($track->hasResponse()) { |
59
|
1 |
|
$response = $track->getResponse()->withAddedHeader(self::HEADER_VCR, self::HEADER_VCR_REPLAY); |
60
|
|
|
|
61
|
1 |
|
return new FulfilledPromise($response); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
throw new CannotBeReplayed(); |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
private function record(RequestInterface $request) |
68
|
|
|
{ |
69
|
3 |
|
if (!$this->vcr->isTurnedOn() || !$this->vcr->hasTape()) { |
70
|
1 |
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
$tape = $this->vcr->getTape(); |
74
|
2 |
|
$tape->addTrack(Track::create($request)); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
3 |
|
private function onFulfilled(RequestInterface $request) |
78
|
|
|
{ |
79
|
3 |
|
$vcr = $this->vcr; |
80
|
|
|
|
81
|
|
|
return function (ResponseInterface $response) use ($vcr, $request) { |
82
|
3 |
|
if ($vcr->isTurnedOn() && $vcr->isRecording()) { |
83
|
|
|
$tape = $vcr->getTape(); |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
$track = $tape->findTrackByRequest($request); |
87
|
|
|
} catch (NotFound $e) { |
88
|
|
|
// The track should have been added already when the request was handled initially, |
89
|
|
|
// but who knows what weird stuff you are doing :) |
90
|
|
|
$track = Track::create($request); |
91
|
|
|
$tape->addTrack($track); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$track->setResponse($response); |
95
|
|
|
|
96
|
|
|
$response = $response->withAddedHeader(self::HEADER_VCR, self::HEADER_VCR_RECORDED); |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
return $response; |
100
|
3 |
|
}; |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
private function onRejected(RequestInterface $request) |
104
|
|
|
{ |
105
|
3 |
|
$vcr = $this->vcr; |
106
|
|
|
|
107
|
3 |
|
return function (\Exception $e) use ($vcr, $request) { |
108
|
|
|
if ($vcr->isTurnedOn() && $vcr->isRecording()) { |
109
|
|
|
$tape = $vcr->getTape(); |
110
|
|
|
|
111
|
|
|
try { |
112
|
|
|
$track = $tape->findTrackByRequest($request); |
113
|
|
|
} catch (NotFound $notFound) { |
114
|
|
|
// The track should have been added already when the request was handled initially, |
115
|
|
|
// but who knows what weird stuff you are doing :) |
116
|
|
|
$track = Track::create($request); |
117
|
|
|
$tape->addTrack($track); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$track->setException($e); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $e; |
124
|
3 |
|
}; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|