Completed
Push — master ( d6a1be...0d1f44 )
by Barry vd.
12s queued 10s
created

src/Common/Message/AbstractResponse.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Abstract Response
4
 */
5
6
namespace Omnipay\Common\Message;
7
8
use Omnipay\Common\Exception\RuntimeException;
9
use Symfony\Component\HttpFoundation\RedirectResponse as HttpRedirectResponse;
10
use Symfony\Component\HttpFoundation\Response as HttpResponse;
11
12
/**
13
 * Abstract Response
14
 *
15
 * This abstract class implements ResponseInterface and defines a basic
16
 * set of functions that all Omnipay Requests are intended to include.
17
 *
18
 * Objects of this class or a subclass are usually created in the Request
19
 * object (subclass of AbstractRequest) as the return parameters from the
20
 * send() function.
21
 *
22
 * Example -- validating and sending a request:
23
 *
24
 * <code>
25
 *   $myResponse = $myRequest->send();
26
 *   // now do something with the $myResponse object, test for success, etc.
27
 * </code>
28
 *
29
 */
30
abstract class AbstractResponse implements ResponseInterface
31
{
32
33
    /**
34
     * The embodied request object.
35
     *
36
     * @var RequestInterface
37
     */
38
    protected $request;
39
40
    /**
41
     * The data contained in the response.
42
     *
43
     * @var mixed
44
     */
45
    protected $data;
46
47
    /**
48
     * Constructor
49
     *
50
     * @param RequestInterface $request the initiating request.
51
     * @param mixed $data
52
     */
53 3
    public function __construct(RequestInterface $request, $data)
54
    {
55 3
        $this->request = $request;
56 3
        $this->data = $data;
57 3
    }
58
59
    /**
60
     * Get the initiating request object.
61
     *
62
     * @return RequestInterface
63
     */
64 3
    public function getRequest()
65
    {
66 3
        return $this->request;
67
    }
68
69
    /**
70
     * Is the response successful?
71
     *
72
     * @return boolean
73
     */
74 3
    public function isPending()
75
    {
76 3
        return false;
77
    }
78
79
    /**
80
     * Does the response require a redirect?
81
     *
82
     * @return boolean
83
     */
84 3
    public function isRedirect()
85
    {
86 3
        return false;
87
    }
88
89
    /**
90
     * Is the response a transparent redirect?
91
     *
92
     * @return boolean
93
     */
94 3
    public function isTransparentRedirect()
95
    {
96 3
        return false;
97
    }
98
99
    /**
100
     * Is the transaction cancelled by the user?
101
     *
102
     * @return boolean
103
     */
104 3
    public function isCancelled()
105
    {
106 3
        return false;
107
    }
108
109
    /**
110
     * Get the response data.
111
     *
112
     * @return mixed
113
     */
114 6
    public function getData()
115
    {
116 6
        return $this->data;
117
    }
118
119
    /**
120
     * Response Message
121
     *
122
     * @return null|string A response message from the payment gateway
123
     */
124 3
    public function getMessage()
125
    {
126 3
        return null;
127
    }
128
129
    /**
130
     * Response code
131
     *
132
     * @return null|string A response code from the payment gateway
133
     */
134 3
    public function getCode()
135
    {
136 3
        return null;
137
    }
138
139
    /**
140
     * Gateway Reference
141
     *
142
     * @return null|string A reference provided by the gateway to represent this transaction
143
     */
144 3
    public function getTransactionReference()
145
    {
146 3
        return null;
147
    }
148
149
    /**
150
     * Get the transaction ID as generated by the merchant website.
151
     *
152
     * @return string
153
     */
154 3
    public function getTransactionId()
155
    {
156 3
        return null;
157
    }
158
159
    /**
160
     * Gets the redirect target url.
161
     *
162
     * @return string
163
     */
164 3
    public function getRedirectUrl()
165
    {
166 3
        return null;
167
    }
168
169
    /**
170
     * Get the required redirect method (either GET or POST).
171
     *
172
     * @return string
173
     */
174 3
    public function getRedirectMethod()
175
    {
176 3
        return 'GET';
177
    }
178
179
    /**
180
     * Gets the redirect form data array, if the redirect method is POST.
181
     *
182
     * @return array
183
     */
184 3
    public function getRedirectData()
185
    {
186 3
        return [];
187
    }
188
189
    /**
190
     * Automatically perform any required redirect
191
     *
192
     * This method is meant to be a helper for simple scenarios. If you want to customize the
193
     * redirection page, just call the getRedirectUrl() and getRedirectData() methods directly.
194
     *
195
     * @return void
196
     */
197 3
    public function redirect()
198
    {
199 3
        $this->getRedirectResponse()->send();
200 3
    }
201
202
    /**
203
     * @return HttpRedirectResponse|HttpResponse
204
     */
205 21
    public function getRedirectResponse()
206
    {
207 21
        $this->validateRedirect();
208
209 9
        if ('GET' === $this->getRedirectMethod()) {
210 6
            return HttpRedirectResponse::create($this->getRedirectUrl());
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpFo...irectResponse::create() has been deprecated with message: since Symfony 5.1, use __construct() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
211
        }
212
213 3
        $hiddenFields = '';
214 3
        foreach ($this->getRedirectData() as $key => $value) {
215 3
            $hiddenFields .= sprintf(
216 3
                '<input type="hidden" name="%1$s" value="%2$s" />',
217 3
                htmlentities($key, ENT_QUOTES, 'UTF-8', false),
218 3
                htmlentities($value, ENT_QUOTES, 'UTF-8', false)
219 3
            )."\n";
220
        }
221
222 3
        $output = '<!DOCTYPE html>
223
<html>
224
<head>
225
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
226
    <title>Redirecting...</title>
227
</head>
228
<body onload="document.forms[0].submit();">
229
    <form action="%1$s" method="post">
230
        <p>Redirecting to payment page...</p>
231
        <p>
232
            %2$s
233
            <input type="submit" value="Continue" />
234
        </p>
235
    </form>
236
</body>
237
</html>';
238 3
        $output = sprintf(
239 3
            $output,
240 3
            htmlentities($this->getRedirectUrl(), ENT_QUOTES, 'UTF-8', false),
241 3
            $hiddenFields
242
        );
243
244 3
        return HttpResponse::create($output);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpFo...tion\Response::create() has been deprecated with message: since Symfony 5.1, use __construct() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
245
    }
246
247
    /**
248
     * Validate that the current Response is a valid redirect.
249
     *
250
     * @return void
251
     */
252 21
    protected function validateRedirect()
253
    {
254 21
        if (!$this instanceof RedirectResponseInterface || !$this->isRedirect()) {
255 6
            throw new RuntimeException('This response does not support redirection.');
256
        }
257
258 15
        if (empty($this->getRedirectUrl())) {
259 3
            throw new RuntimeException('The given redirectUrl cannot be empty.');
260
        }
261
262 12
        if (!in_array($this->getRedirectMethod(), ['GET', 'POST'])) {
263 3
            throw new RuntimeException('Invalid redirect method "'.$this->getRedirectMethod().'".');
264
        }
265 9
    }
266
}
267