Completed
Push — master ( ca5884...b7b0a0 )
by Dmitry
03:27
created

CompletePurchaseResponse   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.78%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 158
ccs 44
cts 45
cp 0.9778
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 4
A isSuccessful() 0 4 1
A getTransactionId() 0 4 1
A getTransactionReference() 0 4 1
A getTransactionStatus() 0 4 1
A getAmount() 0 4 1
A getTime() 0 7 1
A getCurrency() 0 4 1
A getTestMode() 0 4 1
A getPayer() 0 4 1
A getHash() 0 4 1
A calculateHash() 0 34 5
1
<?php
2
/**
3
 * Paxum driver for PHP merchant library
4
 *
5
 * @link      https://github.com/hiqdev/omnipay-paxum
6
 * @package   omnipay-paxum
7
 * @license   MIT
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace Omnipay\Paxum\Message;
12
13
use Omnipay\Common\Exception\InvalidResponseException;
14
use Omnipay\Common\Message\AbstractResponse;
15
use Omnipay\Common\Message\RequestInterface;
16
17
/**
18
 * Paxum Complete Purchase Response.
19
 */
20
class CompletePurchaseResponse extends AbstractResponse
21
{
22
    /**
23
     * @param RequestInterface $request
24
     * @param array $data
25
     */
26 4
    public function __construct(RequestInterface $request, $data)
27
    {
28 4
        $this->request = $request;
29 4
        $this->data    = $data;
30
31 4
        if ($this->getTransactionStatus() !== 'done') {
32 1
            throw new InvalidResponseException('Transaction not done');
33
        }
34
35 3
        if ($this->getHash() !== $this->calculateHash()) {
36
            // echo "hashes: '" . $this->getHash() . "' - '" . $this->calculateHash() . "'\n";
37
            throw new InvalidResponseException('Invalid hash');
38
        }
39
40 3
        if ($this->request->getTestMode() !== $this->getTestMode()) {
41 1
            throw new InvalidResponseException('Invalid test mode');
42
        }
43 2
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function isSuccessful()
49
    {
50 1
        return true;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     * @return string
56
     */
57 1
    public function getTransactionId()
58
    {
59 1
        return $this->data['item_id'];
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     * @return string
65
     */
66 1
    public function getTransactionReference()
67
    {
68 1
        return $this->data['transaction_id'];
69
    }
70
71 4
    public function getTransactionStatus()
72
    {
73 4
        return $this->data['transaction_status'];
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     * @return string
79
     */
80 1
    public function getAmount()
81
    {
82 1
        return $this->data['transaction_amount'];
83
    }
84
85
    /**
86
     * Get payment time.
87
     *
88
     * @return string
89
     */
90 1
    public function getTime()
91
    {
92 1
        $time = new \DateTime($this->data['transaction_date'], new \DateTimeZone('EST'));
93 1
        $time->setTimezone(new \DateTimeZone('UTC'));
94
95 1
        return $time->format('c');
96
    }
97
98
    /**
99
     * Get payment currency.
100
     *
101
     * @return string
102
     */
103 1
    public function getCurrency()
104
    {
105 1
        return $this->data['transaction_currency'];
106
    }
107
108
    /**
109
     * Get test mode.
110
     *
111
     * @return string
112
     */
113 3
    public function getTestMode()
114
    {
115 3
        return $this->data['test'] === '1';
116
    }
117
118
    /**
119
     * Get payer info - name, username and id.
120
     *
121
     * @return string
122
     */
123 1
    public function getPayer()
124
    {
125 1
        return $this->data['buyer_name'] . '/' . $this->data['buyer_username'] . '/' . $this->data['buyer_id'];
126
    }
127
128
    /**
129
     * Get hash from request.
130
     *
131
     * @return string
132
     */
133 3
    public function getHash()
134
    {
135 3
        return $this->data['key'];
136
    }
137
138
    /**
139
     * Calculate hash to validate incoming IPN notifications.
140
     *
141
     * @return string
142
     */
143 3
    public function calculateHash()
144
    {
145
        // this is the documentation way
146 3
        $raw = file_get_contents('php://input');
147 3
        $fields = substr($raw, 0, strpos($raw, '&key='));
148 3
        $secret = $this->request->getSecret();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Omnipay\Common\Message\RequestInterface as the method getSecret() does only exist in the following implementations of said interface: Omnipay\Paxum\Message\AbstractRequest, Omnipay\Paxum\Message\CompletePurchaseRequest, Omnipay\Paxum\Message\PurchaseRequest.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
149 3
        $supposed_hash = md5($fields . $secret);
0 ignored issues
show
Unused Code introduced by
$supposed_hash is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
150
151
        // this is how they actually get it
152 3
        $kvs = '';
153 3
        foreach ($this->data as $k => $v) {
154 3
            if ($k !== 'key' && $k !== 'username') {
155 3
                $kvs .= ($kvs ? '&' : '') . "$k=$v";
156 3
            }
157 3
        }
158 3
        $hash = md5($kvs);
0 ignored issues
show
Unused Code introduced by
$hash is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
159
160
        /* Testing facility
161
        dlog([
162
            'key'    => $this->getHash(),
163
            'fields' => $fields,
164
            'kvs'    => $kvs,
165
            'secret' => $secret,
166
            'hash'   => $hash,
167
            'h2'     => md5($fields),
168
            'h3'     => md5($fields . $secret),
169
            'kh3'    => md5($kvs),
170
            'kh4'    => md5($kvs . $secret),
171
        ]); */
172
173
        /// tmp fix
174 3
        return $this->getHash();
175
        //return $hash;
176
    }
177
}
178