1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the AMFWebServicesClientBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Amine Fattouch <http://github.com/fattouchsquall> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AMF\WebServicesClientBundle\Rest; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
15
|
|
|
use AMF\WebServicesClientBundle\Rest\Exception\RestException; |
16
|
|
|
use AMF\WebServicesClientBundle\Rest\Event\GetResponseEvent; |
17
|
|
|
use AMF\WebServicesClientBundle\Rest\Component\Request; |
18
|
|
|
use AMF\WebServicesClientBundle\Rest\Component\Url; |
19
|
|
|
use AMF\WebServicesClientBundle\Rest\Security\Wsse; |
20
|
|
|
use AMF\WebServicesClientBundle\Rest\Encoder\EncoderProviderInterface; |
21
|
|
|
use AMF\WebServicesClientBundle\Rest\Decoder\DecoderProviderInterface; |
22
|
|
|
use AMF\WebServicesClientBundle\Rest\Encoder\EncoderInterface; |
23
|
|
|
use AMF\WebServicesClientBundle\Rest\Decoder\DecoderInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The endpoint of rest webservice. |
27
|
|
|
* |
28
|
|
|
* @author Mohamed Amine Fattouch <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
abstract class AbstractEndpoint |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var EventDispatcherInterface |
34
|
|
|
*/ |
35
|
|
|
protected $dispatcher; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var EncoderInterface |
39
|
|
|
*/ |
40
|
|
|
protected $encoder; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var DecoderInterface |
44
|
|
|
*/ |
45
|
|
|
protected $decoder; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Url |
49
|
|
|
*/ |
50
|
|
|
protected $url; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Wsse |
54
|
|
|
*/ |
55
|
|
|
protected $wsse; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Constructor class. |
59
|
|
|
* |
60
|
|
|
* @param EventDispatcherInterface $dispatcher The service of dispatcher. |
61
|
|
|
* @param EncoderProviderInterface $encoderProvider Instance of provider for the encoder. |
62
|
|
|
* @param DecoderProviderInterface $decoderProvider Instance of provider for the decoder. |
63
|
|
|
* @param Url $url The url component for rest API. |
64
|
|
|
* @param Wsse $wsse The generator of wsse header. |
65
|
|
|
* @param string $requestFormat The format of request (default json). |
66
|
|
|
* @param string $responseFormat The format of response (default json). |
67
|
|
|
*/ |
68
|
|
|
public function __construct( |
69
|
|
|
EventDispatcherInterface $dispatcher, |
70
|
|
|
EncoderProviderInterface $encoderProvider, |
71
|
|
|
DecoderProviderInterface $decoderProvider, |
72
|
|
|
Url $url = null, |
73
|
|
|
Wsse $wsse = null, |
74
|
|
|
$requestFormat = 'json', |
75
|
|
|
$responseFormat = 'json' |
76
|
|
|
) { |
77
|
|
|
$this->dispatcher = $dispatcher; |
78
|
|
|
$this->encoder = $encoderProvider->getEncoder($requestFormat); |
79
|
|
|
$this->decoder = $decoderProvider->getDecoder($responseFormat); |
80
|
|
|
$this->url = $url; |
81
|
|
|
$this->wsse = $wsse; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Calls Rest API method. |
86
|
|
|
* |
87
|
|
|
* @param string $event |
88
|
|
|
* @param string $path |
89
|
|
|
* @param array $query |
90
|
|
|
* @param array $request |
91
|
|
|
* @param array $server |
92
|
|
|
* |
93
|
|
|
* @return RestResponse |
94
|
|
|
*/ |
95
|
|
|
public function call($event, $path = null, array $query = [], array $request = [], array $server = []) |
96
|
|
|
{ |
97
|
|
|
$request = $this->prepareRequest($path, $query, $request, $server); |
98
|
|
|
|
99
|
|
|
$getResponseEvent = new GetResponseEvent($request); |
100
|
|
|
$this->dispatcher->dispatch($event, $getResponseEvent); |
101
|
|
|
|
102
|
|
|
$response = $getResponseEvent->getResponse(); |
103
|
|
|
|
104
|
|
|
$content = $response->getContent(); |
105
|
|
|
if ($response->isSuccess() === true) { |
106
|
|
|
$data = $this->decodeResponse($content); |
107
|
|
|
|
108
|
|
|
return $data; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
throw new RestException($content); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Prepares a request. |
116
|
|
|
* |
117
|
|
|
* @param string $actionPath The path of action. |
118
|
|
|
* @param array $query The query of url. |
119
|
|
|
* @param array $request The body post of request. |
120
|
|
|
* @param array $server The server parameters. |
121
|
|
|
* |
122
|
|
|
* @return Request |
123
|
|
|
*/ |
124
|
|
|
protected function prepareRequest($actionPath = null, array $query = [], array $request = [], array $server = []) |
125
|
|
|
{ |
126
|
|
|
$uri = null; |
127
|
|
|
if (isset($this->wsse)) { |
128
|
|
|
$wsseHeader = $this->wsse->generateHeader(); |
129
|
|
|
$server = array_merge($wsseHeader, $server); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (isset($this->url)) { |
133
|
|
|
$uri = $this->url->getUriForPath($actionPath, $query); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (!empty($request) && !isset($server['REQUEST_STRING'])) { |
137
|
|
|
$server['REQUEST_STRING'] = $this->encodeRequest($request); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$request = Request::create($uri, $query, $request, $server, 'json'); |
141
|
|
|
|
142
|
|
|
return $request; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Encodes request. |
147
|
|
|
* |
148
|
|
|
* @param array $request The request data (default empty). |
149
|
|
|
* |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
protected function encodeRequest(array $request = []) |
153
|
|
|
{ |
154
|
|
|
return $this->encoder->encode($request); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Decodes response. |
159
|
|
|
* |
160
|
|
|
* @param string $content The response's content (default null). |
161
|
|
|
* |
162
|
|
|
* @return mixed |
163
|
|
|
*/ |
164
|
|
|
protected function decodeResponse($content = null) |
165
|
|
|
{ |
166
|
|
|
return $this->decoder->decode($content); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|