-
Notifications
You must be signed in to change notification settings - Fork 4
/
ResponseAdapter.php
113 lines (92 loc) · 3.38 KB
/
ResponseAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace Aac\Oauth2\Http;
use OAuth2\ResponseInterface;
/**
* A sample adapter to use the same request object for slim and bshaffer/Oauth2
* Class ResponseAdapter.
*/
class ResponseAdapter extends \Slim\Http\Response implements ResponseInterface
{
public function __construct($body = '', $status = 200, $headers = array())
{
parent::__construct($body, $status, $headers);
$this->parameters = array();
}
public function finalize()
{
if (!empty($this->parameters)) {
$body = $this->getBody();
if (!empty($body)) {
throw new \Exception("Params where added to the response, but other output was found also");
}
$this->body(json_encode($this->parameters));
$this->addHttpHeaders(array("Content-Type" => 'application/json'));
}
return parent::finalize();
}
public function addParameters(array $parameters)
{
$this->parameters = array_merge($this->parameters, $parameters);
}
public function getParameter($name, $default = null)
{
return isset($this->parameters[$name]) ? $this->parameters[$name] : $default;
}
public function addHttpHeaders(array $httpHeaders)
{
foreach ($httpHeaders as $key => $value) {
$this->headers->set($key, $value);
}
}
public function setStatusCode($statusCode)
{
$this->setStatus($statusCode);
}
public function setError($statusCode, $error, $errorDescription = null, $errorUri = null)
{
//@todo: copy paste from oauth2 php
$parameters = array(
'error' => $error,
'error_description' => $errorDescription,
);
if (!is_null($errorUri)) {
if (strlen($errorUri) > 0 && $errorUri[0] == '#') {
// we are referencing an oauth bookmark (for brevity)
$errorUri = 'http://tools.ietf.org/html/rfc6749'.$errorUri;
}
$parameters['error_uri'] = $errorUri;
}
$httpHeaders = array(
'Cache-Control' => 'no-store',
);
$this->setStatusCode($statusCode);
$this->addParameters($parameters);
$this->addHttpHeaders($httpHeaders);
if (!$this->isClientError() && !$this->isServerError()) {
throw new \InvalidArgumentException(sprintf('The HTTP status code is not an error ("%s" given).', $statusCode));
}
}
public function setRedirect($statusCode = 302, $url, $state = null, $error = null, $errorDescription = null, $errorUri = null)
{
//@todo: copy paste from oauth2 php
if (empty($url)) {
throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
}
$parameters = array();
if (!is_null($state)) {
$parameters['state'] = $state;
}
if (!is_null($error)) {
$this->setError(400, $error, $errorDescription, $errorUri);
}
$this->setStatusCode($statusCode);
$this->addParameters($parameters);
if (count($this->parameters) > 0) {
// add parameters to URL redirection
$parts = parse_url($url);
$sep = isset($parts['query']) && count($parts['query']) > 0 ? '&' : '?';
$url .= $sep.http_build_query($this->parameters);
}
$this->redirect($url, $statusCode);
}
}