|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Meare\Juggler\Imposter\Stub\Response; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* TODO add cert, key |
|
8
|
|
|
* |
|
9
|
|
|
* @package Meare\Juggler\Response |
|
10
|
|
|
*/ |
|
11
|
|
|
class ProxyResponse implements IResponse |
|
12
|
|
|
{ |
|
13
|
|
|
const MODE_PROXY_ONCE = 'proxyOnce'; |
|
14
|
|
|
const MODE_PROXY_ALWAYS = 'proxyAlways'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $type = self::TYPE_PROXY; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $to; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $mode = self::MODE_PROXY_ONCE; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $predicateGenerators; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $to |
|
38
|
|
|
* @param string $mode |
|
39
|
|
|
* @param array $predicate_generators |
|
40
|
|
|
*/ |
|
41
|
9 |
|
public function __construct($to, $mode = null, array $predicate_generators = []) |
|
42
|
|
|
{ |
|
43
|
9 |
|
$this->setTo($to); |
|
44
|
7 |
|
if (null !== $mode) { |
|
45
|
3 |
|
$this->setMode($mode); |
|
46
|
2 |
|
} |
|
47
|
6 |
|
$this->setPredicateGenerators($predicate_generators); |
|
48
|
6 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param array $contract |
|
52
|
|
|
* @return ProxyResponse |
|
53
|
|
|
*/ |
|
54
|
3 |
|
public static function createFromContract($contract) |
|
55
|
|
|
{ |
|
56
|
3 |
|
if (!isset($contract['to'])) { |
|
57
|
1 |
|
throw new \InvalidArgumentException("Cannot create ProxyResponse from contract: Invalid contract; 'to' field does not exists"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
return new self( |
|
61
|
2 |
|
$contract['to'], |
|
62
|
2 |
|
isset($contract['mode']) ? $contract['mode'] : null, |
|
63
|
2 |
|
isset($contract['predicateGenerators']) ? $contract['predicateGenerators'] : [] |
|
64
|
2 |
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function jsonSerialize() |
|
71
|
|
|
{ |
|
72
|
|
|
return [ |
|
73
|
1 |
|
IResponse::TYPE_PROXY => [ |
|
74
|
1 |
|
'to' => $this->getTo(), |
|
75
|
1 |
|
'mode' => $this->getMode(), |
|
76
|
1 |
|
'predicateGenerators' => $this->getPredicateGenerators(), |
|
77
|
1 |
|
], |
|
78
|
1 |
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
2 |
|
public function getTo() |
|
85
|
|
|
{ |
|
86
|
2 |
|
return $this->to; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $to |
|
91
|
|
|
*/ |
|
92
|
9 |
|
public function setTo($to) |
|
93
|
|
|
{ |
|
94
|
9 |
|
if (!filter_var($to, FILTER_VALIDATE_URL) || null !== parse_url($to, PHP_URL_PATH)) { |
|
95
|
2 |
|
throw new \InvalidArgumentException( |
|
96
|
|
|
"Unable to set ProxyResponse 'to; 'to' value is not valid url without path" |
|
97
|
2 |
|
); |
|
98
|
|
|
} |
|
99
|
7 |
|
$this->to = $to; |
|
100
|
7 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
2 |
|
public function getMode() |
|
106
|
|
|
{ |
|
107
|
2 |
|
return $this->mode; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $mode |
|
112
|
|
|
*/ |
|
113
|
3 |
|
public function setMode($mode) |
|
114
|
|
|
{ |
|
115
|
3 |
|
if (!in_array($mode, [self::MODE_PROXY_ALWAYS, self::MODE_PROXY_ONCE])) { |
|
116
|
1 |
|
throw new \InvalidArgumentException( |
|
117
|
1 |
|
"Unable to set ProxyResponse mode; Allowed modes: '" . self::MODE_PROXY_ONCE . "', '" . self::MODE_PROXY_ONCE . "'" |
|
118
|
1 |
|
); |
|
119
|
|
|
} |
|
120
|
2 |
|
$this->mode = $mode; |
|
121
|
2 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function getPredicateGenerators() |
|
127
|
|
|
{ |
|
128
|
1 |
|
return $this->predicateGenerators; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param array $predicate_generators |
|
133
|
|
|
*/ |
|
134
|
6 |
|
public function setPredicateGenerators(array $predicate_generators) |
|
135
|
|
|
{ |
|
136
|
6 |
|
$this->predicateGenerators = $predicate_generators; |
|
137
|
6 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
1 |
|
public function getType() |
|
143
|
|
|
{ |
|
144
|
1 |
|
return $this->type; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|