Passed
Push — main ( b1b5d7...536cb4 )
by
unknown
03:11
created

AcceptNotification   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 27
c 1
b 0
f 0
dl 0
loc 69
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getTransactionStatus() 0 7 4
A sendData() 0 26 3
A getMessage() 0 3 1
A getData() 0 3 1
A getAuthorised() 0 3 1
A getTransactionReference() 0 3 1
A getResponseText() 0 3 1
A getTransaction() 0 3 1
1
<?php
2
3
namespace Omnipay\WindcaveHpp\Message;
4
5
use Omnipay\Common\Exception\InvalidRequestException;
6
use Omnipay\Common\Http\ClientInterface;
7
use Omnipay\Common\Message\AbstractRequest;
8
use Omnipay\Common\Message\NotificationInterface;
9
use Symfony\Component\HttpFoundation\Request as HttpRequest;
10
11
class AcceptNotification extends PurchaseRequest implements NotificationInterface{
12
    protected $data;
13
14
    protected $transaction;
15
16
    public function getData()
17
    {
18
        return $this->data;
19
    }
20
    public function sendData($data)
21
    {
22
        $sessionId = $this->httpRequest->query->get('sessionId') ?? $this->httpRequest->request->get('sessionId') ?? '';
23
24
        if ( empty($sessionId) ) {
25
            throw new InvalidRequestException('Session id is required');
26
        }
27
28
        $headers = [
29
            'Accept' => 'application/json',
30
            'Content-Type' => 'application/json',
31
            'Authorization' => 'Basic ' . $this->getAuthorization()
32
        ];
33
34
        try {
35
            $httpResponse = $this->httpClient->request('GET', $this->getEndpoint('sessions/' . $sessionId), $headers);
36
        } catch (\Exception $exception) {
37
            throw new InvalidRequestException($exception->getMessage());
38
        }
39
40
        $transactionData = json_decode($httpResponse->getBody()->getContents(), true);
41
42
        $this->data = $transactionData;
43
        $this->transaction = $transactionData['transactions'][0] ?? [];
44
45
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Omnipay\WindcaveHpp\Message\AcceptNotification 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...
46
    }
47
48
    public function getTransaction()
49
    {
50
        return $this->transaction;
51
    }
52
53
    public function getTransactionReference()
54
    {
55
        return $this->getTransaction()['id'] ?? '';
56
    }
57
58
    public function getTransactionStatus()
59
    {
60
        if ( $this->getTransaction() && $this->getAuthorised() && $this->getResponseText() === 'APPROVED' )  {
61
            return static::STATUS_COMPLETED;
62
        }
63
64
        return static::STATUS_FAILED;
65
    }
66
67
    public function getAuthorised()
68
    {
69
        return $this->getTransaction()['authorised'] ?? false;
70
    }
71
72
    public function getResponseText()
73
    {
74
        return strtoupper($this->getTransaction()['responseText']) ?? '';
75
    }
76
77
    public function getMessage()
78
    {
79
        return $this->getResponseText() ?? '';
80
    }
81
}