WebhookEventNotification::getTransactionStatus()   B
last analyzed

Complexity

Conditions 11
Paths 11

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 11
eloc 16
nc 11
nop 0
dl 0
loc 26
rs 7.3166
c 2
b 0
f 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Omnipay\GoCardless\Message;
4
5
use Omnipay\Common\Message\AbstractRequest;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Omnipay\GoCardless\Message\AbstractRequest. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Omnipay\Common\Message\NotificationInterface;
7
8
class WebhookEventNotification extends AbstractRequest implements NotificationInterface
9
{
10
    public function setNotification($value)
11
    {
12
        return $this->setParameter('notification', $value);
13
    }
14
15
    public function getNotification()
16
    {
17
        return $this->getParameter('notification');
18
    }
19
20
    public function getData()
21
    {
22
        $this->validate('notification');
23
        return $this->getNotification();
24
    }
25
26
    /**
27
     * @return null|string
28
     */
29
    public function getAction()
30
    {
31
        $data = $this->getData();
32
        if (isset($data['action'])) {
33
            return $data['action'];
34
        }
35
    }
36
37
    /**
38
     * @return null|string
39
     */
40
    public function getCode()
41
    {
42
        $data = $this->getData();
43
        if (isset($data['details']['cause'])) {
44
            return $data['details']['cause'];
45
        }
46
    }
47
48
    /**
49
     * @return null|string  One of: bank, gocardless, api, customer
50
     */
51
    public function getEventOrigin()
52
    {
53
        $data = $this->getData();
54
        if (isset($data['details']['origin'])) {
55
            return $data['details']['origin'];
56
        }
57
    }
58
59
    /**
60
     * @return null|string
61
     */
62
    public function getMessage()
63
    {
64
        $data = $this->getData();
65
        if (isset($data['details']['description'])) {
66
            return $data['details']['description'];
67
        }
68
    }
69
70
    /**
71
     * @return null|string
72
     */
73
    public function getTransactionReference()
74
    {
75
        $data = $this->getData();
76
        if (isset($data['id'])) {
77
            return $data['id'];
78
        }
79
    }
80
81
    /**
82
     * @link https://developer.gocardless.com/api-reference/#event-actions-payment
83
     *
84
     * @return null|string
85
     */
86
    public function getTransactionStatus()
87
    {
88
        if ($this->getType() === 'payments') {
89
            switch ($this->getAction()) {
90
                case 'confirmed':
91
                case 'paid_out':
92
                    return NotificationInterface::STATUS_COMPLETED;
93
                case 'pending_customer_approval':
94
                case 'pending_submission':
95
                case 'submitted':
96
                    return NotificationInterface::STATUS_PENDING;
97
                case 'cancelled':
98
                case 'charged_back':
99
                case 'customer_approval_denied':
100
                case 'failed':
101
                    return NotificationInterface::STATUS_FAILED;
102
                default:
103
                    // no state change, do nothing? other events from docs:
104
                    // created -- pending?
105
                    // customer_approval_granted -- pending?
106
                    // chargeback_cancelled
107
                    // late_failure_settled -- failure?
108
                    // chargeback_settled -- failure?
109
                    // surcharge_fee_debited
110
                    // resubmission_requested -- pending?
111
                    break;
112
            }
113
        }
114
    }
115
116
    /**
117
     * @return null|mixed
118
     */
119
    public function getMetaData()
120
    {
121
        $data = $this->getData();
122
        if (isset($data['metadata'])) {
123
            return $data['metadata'];
124
        }
125
    }
126
127
    /**
128
     * @return null|string
129
     */
130
    public function getType()
131
    {
132
        $data = $this->getData();
133
        if (isset($data['resource_type'])) {
134
            return $data['resource_type'];
135
        }
136
    }
137
138
    /**
139
     * @return null|string
140
     */
141
    public function getPaymentId()
142
    {
143
        $data = $this->getData();
144
        if (isset($data['links']['payment'])) {
145
            return $data['links']['payment'];
146
        }
147
    }
148
149
    /**
150
     * Implemented as part of {@see AbstractRequest}} for legacy support
151
     */
152
    public function sendData($data)
153
    {
154
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Omnipay\GoCardless\Messa...ebhookEventNotification which is incompatible with the return type mandated by Omnipay\Common\Message\R...stInterface::sendData() of Omnipay\Common\Message\ResponseInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
155
    }
156
}
157