1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ghc\Rosetta\Connectors; |
4
|
|
|
|
5
|
|
|
use Ghc\Rosetta\Messages\Message; |
6
|
|
|
use Ghc\Rosetta\Pipes\Pipeable; |
7
|
|
|
|
8
|
|
|
class Request implements Pipeable |
9
|
|
|
{ |
10
|
|
|
const SHOW = 'show'; |
11
|
|
|
const CREATE = 'create'; |
12
|
|
|
const UPDATE = 'update'; |
13
|
|
|
const REPLACE = 'replace'; |
14
|
|
|
const DELETE = 'delete'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Connector |
18
|
|
|
*/ |
19
|
|
|
protected $connector; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $method; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $uri; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var mixed |
33
|
|
|
*/ |
34
|
|
|
protected $data; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $options; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Request constructor. |
43
|
|
|
* |
44
|
|
|
* @param Connector $connector |
45
|
|
|
* @param string $method |
46
|
|
|
* @param string $uri |
47
|
|
|
* @param mixed $data |
48
|
|
|
* @param array $options |
49
|
|
|
*/ |
50
|
|
|
public function __construct(Connector $connector, $method, $uri, $data = null, $options = []) |
51
|
|
|
{ |
52
|
|
|
$this->setConnector($connector); |
53
|
|
|
$this->setMethod($method); |
54
|
|
|
$this->setUri($uri); |
55
|
|
|
$this->setData($data); |
56
|
|
|
$this->setOptions($options); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Connector |
61
|
|
|
*/ |
62
|
|
|
public function getConnector() |
63
|
|
|
{ |
64
|
|
|
return $this->connector; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Connector $connector |
69
|
|
|
* |
70
|
|
|
* @return Request |
71
|
|
|
*/ |
72
|
|
|
public function setConnector($connector) |
73
|
|
|
{ |
74
|
|
|
$this->connector = $connector; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getMethod() |
83
|
|
|
{ |
84
|
|
|
return $this->method; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $method |
89
|
|
|
* |
90
|
|
|
* @return Request |
91
|
|
|
*/ |
92
|
|
|
public function setMethod($method) |
93
|
|
|
{ |
94
|
|
|
$this->method = $method; |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getUri() |
103
|
|
|
{ |
104
|
|
|
return $this->uri; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $uri |
109
|
|
|
* |
110
|
|
|
* @return Request |
111
|
|
|
*/ |
112
|
|
|
public function setUri($uri) |
113
|
|
|
{ |
114
|
|
|
$this->uri = $uri; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
public function getData() |
123
|
|
|
{ |
124
|
|
|
return $this->data; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param mixed $data |
129
|
|
|
* |
130
|
|
|
* @return Request |
131
|
|
|
*/ |
132
|
|
|
public function setData($data) |
133
|
|
|
{ |
134
|
|
|
$this->data = $data; |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function getOptions() |
143
|
|
|
{ |
144
|
|
|
return $this->options; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param array $options |
149
|
|
|
* |
150
|
|
|
* @return Request |
151
|
|
|
*/ |
152
|
|
|
public function setOptions($options) |
153
|
|
|
{ |
154
|
|
|
$this->options = $options; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return mixed |
161
|
|
|
*/ |
162
|
|
|
public function handle() |
163
|
|
|
{ |
164
|
|
|
switch ($this->getMethod()) { |
165
|
|
|
case self::SHOW: |
166
|
|
View Code Duplication |
case self::DELETE: |
|
|
|
|
167
|
|
|
return call_user_func_array([ |
168
|
|
|
$this->getConnector(), |
169
|
|
|
$this->getMethod(), |
170
|
|
|
], [ |
171
|
|
|
$this->getUri(), |
172
|
|
|
$this->getOptions(), |
173
|
|
|
]); |
174
|
|
View Code Duplication |
default: |
|
|
|
|
175
|
|
|
return call_user_func_array([ |
176
|
|
|
$this->getConnector(), |
177
|
|
|
$this->getMethod(), |
178
|
|
|
], [ |
179
|
|
|
$this->getUri(), |
180
|
|
|
$this->getData(), |
181
|
|
|
$this->getOptions(), |
182
|
|
|
]); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param array $options |
188
|
|
|
* |
189
|
|
|
* @return \Closure |
190
|
|
|
*/ |
191
|
|
|
public function pipe($options = []) |
192
|
|
|
{ |
193
|
|
|
return function ($inputData) use ($options) { |
|
|
|
|
194
|
|
|
if (!is_null($inputData)) { |
195
|
|
|
$this->setData($inputData); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** @var Message $message */ |
199
|
|
|
$message = $this->handle(); |
200
|
|
|
|
201
|
|
|
return $message->toArray(); |
202
|
|
|
}; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.