|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Mediapart LaPresseLibre Bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* CC BY-NC-SA <https://github.com/mediapart/lapresselibre-bundle> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Mediapart\Bundle\LaPresseLibreBundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
17
|
|
|
use Mediapart\Bundle\LaPresseLibreBundle\Handler; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Use to respond to the La Presse Libre requests. |
|
21
|
|
|
*/ |
|
22
|
|
|
class WebServicesController |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var Handler |
|
26
|
|
|
*/ |
|
27
|
|
|
private $handler; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param Handler $handler |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Handler $handler) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->handler = $handler; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Request $request |
|
39
|
|
|
* |
|
40
|
|
|
* @return Response |
|
41
|
|
|
* |
|
42
|
|
|
* @throws HttpException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __invoke(Request $request) |
|
45
|
|
|
{ |
|
46
|
|
|
$headers = array_merge( |
|
47
|
|
|
$this->handler->getHttpResponseHeaders(), |
|
48
|
|
|
['Content-Type' => 'application/json'] |
|
49
|
|
|
); |
|
50
|
|
|
try { |
|
51
|
|
|
$response = new Response( |
|
52
|
|
|
$this->handler->process($request), |
|
53
|
|
|
Response::HTTP_OK, |
|
54
|
|
|
$headers |
|
55
|
|
|
); |
|
56
|
|
|
} catch (\InvalidArgumentException $exception) { |
|
57
|
|
|
$this->throwHttpException( |
|
58
|
|
|
Response::HTTP_BAD_REQUEST, |
|
59
|
|
|
$exception->getMessage(), |
|
60
|
|
|
$exception, |
|
61
|
|
|
$headers |
|
62
|
|
|
); |
|
63
|
|
|
} catch (\UnexpectedValueException $exception) { |
|
64
|
|
|
$this->throwHttpException( |
|
65
|
|
|
Response::HTTP_UNAUTHORIZED, |
|
66
|
|
|
$exception->getMessage(), |
|
67
|
|
|
$exception, |
|
68
|
|
|
$headers |
|
69
|
|
|
); |
|
70
|
|
|
} catch (\Exception $exception) { |
|
71
|
|
|
$this->throwHttpException( |
|
72
|
|
|
Response::HTTP_INTERNAL_SERVER_ERROR, |
|
73
|
|
|
'Internal Error', |
|
74
|
|
|
$exception, |
|
75
|
|
|
$headers |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $response; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param integer $code |
|
84
|
|
|
* @param string $message |
|
85
|
|
|
* @param \Exception $exception |
|
86
|
|
|
* @param array $headers |
|
87
|
|
|
* |
|
88
|
|
|
* @throws HttpException |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function throwHttpException($code, $message = '', \Exception $exception, array $headers = []) |
|
91
|
|
|
{ |
|
92
|
|
|
throw new HttpException($code, $message, $exception, $headers); |
|
93
|
|
|
} |
|
94
|
|
|
} |