Completed
Branch master (e11220)
by John
02:37
created

AuthnetWebhooksRequest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the AuthnetJSON package.
5
 *
6
 * (c) John Conde <[email protected]>
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 JohnConde\Authnet;
13
14
/**
15
 * Creates a request to the Authorize.Net Webhooks endpoints
16
 *
17
 * @package     AuthnetJSON
18
 * @author      John Conde <[email protected]>
19
 * @copyright   John Conde <[email protected]>
20
 * @license     http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0
21
 * @link        https://github.com/stymiee/authnetjson
22
 * @see         https://developer.authorize.net/api/reference/features/webhooks.html
23
 */
24
class AuthnetWebhooksRequest
25
{
26
    /**
27
     * @var     string  Base URL for processing a webhook
28
     */
29
    private $url;
30
31
    /**
32
     * @var     string  Endpoint for processing a webhook
33
     */
34
    private $endpoint;
35
36
    /**
37
     * @var     string  JSON formatted API request
38
     */
39
    private $requestJson;
40
41
    /**
42
     * @var     object  Wrapper object representing an endpoint
43
     */
44
    private $processor;
45
46
    /**
47
     * Creates the request object by setting the Authorize.Net credentials and URL of the endpoint to be used
48
     * for the API call
49
     *
50
     * @param   string  $api_url            URL endpoint for processing a transaction
51
     */
52
    public function __construct($api_url)
53
    {
54
        $this->url = $api_url;
55
    }
56
57
    /**
58
     * Outputs the account credentials, endpoint URL, and request JSON in a human readable format
59
     *
60
     * @return  string  HTML table containing debugging information
61
     */
62
    public function __toString()
63
    {
64
        $output  = '';
65
        $output .= '<table summary="Authorize.Net Request" id="authnet-request">' . "\n";
66
        $output .= '<tr>' . "\n\t\t" . '<th colspan="2"><b>Class Parameters</b></th>' . "\n" . '</tr>' . "\n";
67
        $output .= '<tr>' . "\n\t\t" . '<td><b>Authnet Server URL</b></td><td>' . $this->url . '</td>' . "\n" . '</tr>' . "\n";
68
        $output .= '<tr>' . "\n\t\t" . '<th colspan="2"><b>Request JSON</b></th>' . "\n" . '</tr>' . "\n";
69 View Code Duplication
        if (!empty($this->requestJson)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
70
            $output .= '<tr><td colspan="2"><pre>' . "\n";
71
            $output .= $this->requestJson . "\n";
72
            $output .= '</pre></td></tr>' . "\n";
73
        }
74
        $output .= '</table>';
75
76
        return $output;
77
    }
78
79
    /**
80
     * Outputs the endpoint URL and request JSON in a human readable format
81
     *
82
     * @return  array   list of event types
83
     */
84 View Code Duplication
    public function getEventTypes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
85
    {
86
        $this->endpoint = 'eventtypes';
87
        $this->url = sprintf('%s%s', $this->url, $this->endpoint);
88
        $response = $this->get($this->url);
0 ignored issues
show
Documentation introduced by
$this->url is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
        return new AuthnetWebhooksResponse($response);
90
    }
91
92
    /**
93
     * Outputs the endpoint URL and request JSON in a human readable format
94
     *
95
     * @param   array   $webhooks   Array of webhooks to be created or modified
96
     * @param   string  $webhookUrl URL of where webhook notifications will be sent
97
     * @param   string  $status     Status of webhooks to be created or modified [active/inactive]
98
     *
99
     * @throws  \JohnConde\Authnet\AuthnetInvalidParameterException
100
     *
101
     * @return  array   list of event types
102
     */
103 View Code Duplication
    public function createWebhooks(Array $webhooks, $webhookUrl, $status = 'active')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
104
    {
105
        $this->endpoint = 'webhooks';
106
        $this->url = sprintf('%s%s', $this->url, $this->endpoint);
107
        $request = [
108
            'url'        => $webhookUrl,
109
            'eventTypes' => $webhooks,
110
            'status'     => $status
111
        ];
112
        $this->requestJson = json_encode($request);
113
        $response = $this->post($this->url, $this->requestJson);
0 ignored issues
show
Documentation introduced by
$this->url is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->requestJson is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
        return new AuthnetWebhooksResponse($response);
115
    }
116
117
    /**
118
     * Sends a test ping to a URL for (a) designated webhook(s)
119
     *
120
     * @param   string   $webhookId   Webhook ID to be tested
121
     *
122
     * @throws  \JohnConde\Authnet\AuthnetInvalidParameterException
123
     */
124
    public function testWebhook($webhookId)
125
    {
126
        $this->endpoint = 'webhooks';
127
        $this->url = sprintf('%s%s/%s/pings', $this->url, $this->endpoint, $webhookId);
128
        $this->requestJson = json_encode([]);
129
        $this->post($this->url, $this->requestJson);
0 ignored issues
show
Documentation introduced by
$this->url is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->requestJson is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130
    }
131
132
    /**
133
     * List all of your webhooks
134
     *
135
     * @return  array   list of event types
136
     */
137 View Code Duplication
    public function getWebhooks()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
138
    {
139
        $this->endpoint = 'webhooks';
140
        $this->url = sprintf('%s%s', $this->url, $this->endpoint);
141
        $response = $this->get($this->url);
0 ignored issues
show
Documentation introduced by
$this->url is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
142
        return new AuthnetWebhooksResponse($response);
143
    }
144
145
    /**
146
     * Get a webhook
147
     *
148
     * @param   string   $webhookId   Webhook ID to be retrieved
149
     *
150
     * @return  array   list of event types
151
     */
152 View Code Duplication
    public function getWebhook($webhookId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
153
    {
154
        $this->endpoint = 'webhooks';
155
        $this->url = sprintf('%s%s/%s', $this->url, $this->endpoint, $webhookId);
156
        $response = $this->get($this->url);
0 ignored issues
show
Documentation introduced by
$this->url is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
157
        return new AuthnetWebhooksResponse($response);
158
    }
159
160
    /**
161
     * Outputs the endpoint URL and request JSON in a human readable format
162
     *
163
     * @param   array   $webhookId      Webhook ID to be modified
164
     * @param   string  $webhookUrl     URL of where webhook notifications will be sent
165
     * @param   array   $eventTypes     Array of event types to be added/removed
166
     * @param   string  $status         Status of webhooks to be modified [active/inactive]
167
     *
168
     * @throws  \JohnConde\Authnet\AuthnetInvalidParameterException
169
     *
170
     * @return  array   list of event types
171
     */
172 View Code Duplication
    public function updateWebhook($webhookId, $webhookUrl, Array $eventTypes, $status = 'active')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
173
    {
174
        $this->endpoint = 'webhooks';
175
        $this->url = sprintf('%s%s/%s', $this->url, $this->endpoint, $webhookId);
176
        $request = [
177
            'url'        => $webhookUrl,
178
            'eventTypes' => $eventTypes,
179
            'status'     => $status
180
        ];
181
        $this->requestJson = json_encode($request);
182
        $response = $this->put($this->url, $this->requestJson);
0 ignored issues
show
Documentation introduced by
$this->url is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->requestJson is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
183
        return new AuthnetWebhooksResponse($response);
184
    }
185
186
    /**
187
     * Delete a webhook
188
     *
189
     * @param   string   $webhookId   Webhook ID to be retrieved
190
     *
191
     * @return  array   list of event types
192
     */
193
    public function deleteWebhook($webhookId)
194
    {
195
        $this->endpoint = 'webhooks';
196
        $this->url = sprintf('%s%s/%s', $this->url, $this->endpoint, $webhookId);
197
        $this->delete($this->url);
0 ignored issues
show
Documentation introduced by
$this->url is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
198
    }
199
200
    /**
201
     * Retrieve Notification History
202
     *
203
     * @param   integer   $limit    Default: 1000
204
     * @param   integer   $offset   Default: 0
205
     *
206
     * @return  array   list of event types
207
     */
208
    public function getNotificationHistory($limit = 1000, $offset = 0)
209
    {
210
        $this->endpoint = 'notifications';
211
        $this->url = sprintf('%s%s', $this->url, $this->endpoint);
212
        $response = $this->get($this->url, [
0 ignored issues
show
Documentation introduced by
$this->url is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to AuthnetWebhooksRequest::get() has too many arguments starting with array('offset' => $offset, 'limit' => $limit).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
213
            'offset' => $offset,
214
            'limit'  => $limit
215
        ]);
216
        return new AuthnetWebhooksResponse($response);
217
    }
218
219
    /**
220
     * Tells the handler to make the API call to Authorize.Net
221
     *
222
     * @throws  \JohnConde\Authnet\AuthnetCurlException
223
     */
224
    private function handleResponse()
225
    {
226
        if(!$this->processor->error) {
227
            return $this->processor->response;
228
        }
229
        $error_message = null;
230
        $error_code    = null;
231
        if ($this->processor->error_code) {
232
            $error_message = $this->processor->error_message;
233
            $error_code    = $this->processor->error_code;
234
            if (empty($error_message)) {
235
                $response = json_decode($this->processor->response);
236
                $error_message = sprintf('(%u) %s: %s', $response->status, $response->reason, $response->message);
237
            }
238
        }
239
        throw new AuthnetCurlException('Connection error: ' . $error_message . ' (' . $error_code . ')');
240
    }
241
242
    /**
243
     * Make GET request via Curl
244
     *
245
     * @param   object  $url
246
     *
247
     * @throws  \JohnConde\Authnet\AuthnetCurlException
248
     */
249
    private function get($url) {
250
        $this->processor->get($url);
251
        return $this->handleResponse();
252
    }
253
254
    /**
255
     * Make POST request via Curl
256
     *
257
     * @param   object  $url        API endpoint
258
     * @param   object  $request    JSON request payload
259
     *
260
     * @throws  \JohnConde\Authnet\AuthnetCurlException
261
     */
262
    private function post($url, $request) {
263
        $this->processor->post($url, $request);
264
        return $this->handleResponse();
265
    }
266
267
    /**
268
     * Make PUT request via Curl
269
     *
270
     * @param   object  $url        API endpoint
271
     * @param   object  $request    JSON request payload
272
     *
273
     * @throws  \JohnConde\Authnet\AuthnetCurlException
274
     */
275
    private function put($url, $request) {
276
        $this->processor->put($url, $request, true);
277
        return $this->handleResponse();
278
    }
279
280
    /**
281
     * Make DELETE request via Curl
282
     *
283
     * @param   object  $url        API endpoint
284
     * @param   object  $request    JSON request payload
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
285
     *
286
     * @throws  \JohnConde\Authnet\AuthnetCurlException
287
     */
288
    private function delete($url) {
289
        $this->processor->delete($url, [], true);
290
        return $this->handleResponse();
291
    }
292
293
    /**
294
     * Sets the handler to be used to handle our API call. Mainly used for unit testing as Curl is used by default.
295
     *
296
     * @param   object  $processor
297
     */
298
    public function setProcessHandler($processor)
299
    {
300
        $this->processor = $processor;
301
    }
302
303
    /**
304
     * Gets the request sent to Authorize.Net in JSON format for logging purposes
305
     *
306
     * @return  string transaction request sent to Authorize.Net in JSON format
307
     */
308
    public function getRawRequest()
309
    {
310
        return $this->requestJson;
311
    }
312
}