Passed
Push — master ( 85716f...d04990 )
by John
05:03 queued 10s
created

AuthnetWebhook::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 15
ccs 13
cts 13
cp 1
crap 1
rs 9.8666
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style introduced by
Class found in ".php" file; use ".inc" extension instead
Loading history...
Coding Style introduced by
The PHP open tag does not have a corresponding PHP close tag
Loading history...
Coding Style introduced by
Filename "AuthnetWebhook.php" doesn't match the expected filename "authnetwebhook.php"
Loading history...
2
3
declare(strict_types=1);
4
5
/**
0 ignored issues
show
Coding Style introduced by
Block comments must be started with /*
Loading history...
6
 * This file is part of the AuthnetJSON package.
7
 *
8
 * (c) John Conde <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Authnetjson;
15
16
/**
17
 * Handles a Webhook notification from the Authorize.Net Webhooks API
18
 *
19
 * @package   AuthnetJSON
20
 * @author    John Conde <[email protected]>
21
 * @copyright John Conde <[email protected]>
22
 * @license   http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0
23
 * @link      https://github.com/stymiee/authnetjson
24
 * @see       https://developer.authorize.net/api/reference/
25
 */
26
class AuthnetWebhook
27
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class AuthnetWebhook
Loading history...
28
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
29
     * @var object  SimpleXML object representing the Webhook notification
30
     */
31
    private $webhook;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
Coding Style introduced by
Private member variable "webhook" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "webhook" must be prefixed with an underscore
Loading history...
32
33
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
34
     * @var string  JSON string that is the Webhook notification sent by Authorize.Net
35
     */
36
    private $webhookJson;
0 ignored issues
show
Coding Style introduced by
Private member variable "webhookJson" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "webhookJson" must be prefixed with an underscore
Loading history...
37
38
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
39
     * @var array  HTTP headers sent with the notification
40
     */
41
    private $headers;
0 ignored issues
show
Coding Style introduced by
Private member variable "headers" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "headers" must be prefixed with an underscore
Loading history...
42
43
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
44
     * @var string  Authorize.Net Signature Key
45
     */
46
    private $signature;
0 ignored issues
show
Coding Style introduced by
Private member variable "signature" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "signature" must be prefixed with an underscore
Loading history...
47
48
    /**
49
     * Creates the response object with the response json returned from the API call
50
     *
51
     * @param  string $signature Authorize.Net Signature Key
0 ignored issues
show
introduced by
Parameter comment must end with a full stop
Loading history...
52
     * @param  string $payload   Webhook Notification sent by Authorize.Net
0 ignored issues
show
introduced by
Parameter comment must end with a full stop
Loading history...
53
     * @param  array  $headers   HTTP headers sent with Webhook. Optional if PHP is run as an Apache module
0 ignored issues
show
introduced by
Parameter comment must end with a full stop
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 107 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
54
     * @throws AuthnetInvalidCredentialsException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
introduced by
Comment missing for @throws tag in function comment
Loading history...
55
     * @throws AuthnetInvalidJsonException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
introduced by
Comment missing for @throws tag in function comment
Loading history...
56
     */
57 3
    public function __construct(string $signature, string $payload, array $headers = [])
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$headers" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$headers"; expected 0 but found 1
Loading history...
58
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
59 3
        $this->signature   = $signature;
60 3
        $this->webhookJson = $payload;
61 3
        $this->headers     = $headers;
62 3
        if (empty($this->headers)) {
63 2
            $this->headers = $this->getAllHeaders();
64
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
65 3
        if (empty($this->signature)) {
66 1
            throw new AuthnetInvalidCredentialsException('You have not configured your signature properly.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 109 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
67
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
68 2
        if (($this->webhook = json_decode($this->webhookJson, false)) === null) {
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of null please use NULL.
Loading history...
69 1
            throw new AuthnetInvalidJsonException('Invalid JSON sent in the Webhook notification');
70
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
71 1
        $this->headers = array_change_key_case($this->headers, CASE_UPPER);
72 1
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end __construct()
Loading history...
73
74
    /**
75
     * Outputs the response JSON in a human readable format
76
     *
77
     * @return string  HTML table containing debugging information
78
     */
79 1
    public function __toString()
80
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
81 1
        $output  = '<table id="authnet-webhook">'."\n";
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "."; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "."; 0 found
Loading history...
82 1
        $output .= '<caption>Authorize.Net Webhook</caption>'."\n";
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "."; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "."; 0 found
Loading history...
83 1
        $output .= '<tr><th colspan="2"><b>Response HTTP Headers</b></th></tr>'."\n";
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "."; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "."; 0 found
Loading history...
84 1
        $output .= '<tr><td colspan="2"><pre>'."\n";
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "."; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "."; 0 found
Loading history...
85 1
        $output .= var_export($this->headers)."\n";
0 ignored issues
show
Bug introduced by
Are you sure the usage of var_export($this->headers) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Coding Style introduced by
Expected at least 1 space before "."; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "."; 0 found
Loading history...
86 1
        $output .= '</pre></td></tr>'."\n";
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "."; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "."; 0 found
Loading history...
87 1
        $output .= '<tr><th colspan="2"><b>Response JSON</b></th></tr>'."\n";
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "."; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "."; 0 found
Loading history...
88 1
        $output .= '<tr><td colspan="2"><pre>'."\n";
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "."; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "."; 0 found
Loading history...
89 1
        $output .= $this->webhookJson."\n";
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "."; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "."; 0 found
Loading history...
90 1
        $output .= '</pre></td></tr>'."\n";
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "."; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "."; 0 found
Loading history...
91 1
        $output .= '</table>';
92
93 1
        return $output;
94
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end __toString()
Loading history...
95
96
    /**
97
     * Gets a response variable from the Webhook notification
98
     *
99
     * @param  string $var
0 ignored issues
show
Documentation introduced by
Missing parameter comment
Loading history...
100
     * @return string          requested variable from the API call response
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
101
     */
102 1
    public function __get($var)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $var
Loading history...
103
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
104 1
        return $this->webhook->{$var};
105
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end __get()
Loading history...
106
107
    /**
108
     * Validates a webhook signature to determine if the webhook is valid
109
     *
110
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
111
     */
112 3
    public function isValid(): bool
113
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
114 3
        $hashedBody = strtoupper(hash_hmac('sha512', $this->webhookJson, $this->signature));
115 3
        return (isset($this->headers['X-ANET-SIGNATURE']) &&
0 ignored issues
show
Coding Style introduced by
Boolean operators are not allowed outside of control structure conditions
Loading history...
116 3
            strtoupper(explode('=', $this->headers['X-ANET-SIGNATURE'])[1]) === $hashedBody);
117
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end isValid()
Loading history...
118
119
    /**
120
     * Validates a webhook signature to determine if the webhook is valid
121
     *
122
     * @return string|null
123
     */
124 1
    public function getRequestId(): ?string
125
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
126 1
        return $this->headers['X-REQUEST-ID'] ?? null;
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of null please use NULL.
Loading history...
127
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end getRequestId()
Loading history...
128
129
    /**
130
     * Retrieves all HTTP headers of a given request
131
     *
132
     * @return array
133
     */
134 1
    protected function getAllHeaders(): array
135
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
136 1
        if (function_exists('apache_request_headers')) {
137
            $headers = apache_request_headers();
138
        } else {
139 1
            $headers = [];
140 1
            foreach ($_SERVER as $key => $value) {
141 1
                if (strpos($key, 'HTTP_') === 0) {
142 1
                    $headers[str_replace('_', '-', substr($key, 5))] = $value;
143
                }
144
            }
145
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
146 1
        return $headers;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $headers could return the type false which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
147
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end getAllHeaders()
Loading history...
148
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
149