Completed
Push — master ( 48c98e...fbf5b6 )
by Andrii
02:54
created

CompletePurchaseResponse   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.56%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 155
ccs 40
cts 41
cp 0.9756
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 4 1
A getCurrency() 0 4 1
A getTestMode() 0 4 1
A getPayer() 0 4 1
A getHash() 0 4 1
B calculateHash() 0 34 5
1
<?php
2
/**
3
 * Paxum plugin 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";
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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
        return date('c', strtotime($this->data['transaction_date'] . ' EDT'));
93
    }
94
95
    /**
96
     * Get payment currency.
97
     *
98
     * @return string
99
     */
100 1
    public function getCurrency()
101
    {
102 1
        return $this->data['transaction_currency'];
103
    }
104
105
    /**
106
     * Get test mode.
107
     *
108
     * @return string
109
     */
110 3
    public function getTestMode()
111
    {
112 3
        return $this->data['test'] === '1';
113
    }
114
115
    /**
116
     * Get payer info - name, username and id.
117
     *
118
     * @return string
119
     */
120 1
    public function getPayer()
121
    {
122 1
        return $this->data['buyer_name'] . '/' . $this->data['buyer_username'] . '/' . $this->data['buyer_id'];
123
    }
124
125
    /**
126
     * Get hash from request.
127
     *
128
     * @return string
129
     */
130 3
    public function getHash()
131
    {
132 3
        return $this->data['key'];
133
    }
134
135
    /**
136
     * Calculate hash to validate incoming IPN notifications.
137
     *
138
     * @return string
139
     */
140 3
    public function calculateHash()
141
    {
142
        // this is the documentation way
143 3
        $raw = file_get_contents('php://input');
144 3
        $fields = substr($raw, 0, strpos($raw, '&key='));
145 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...
146 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...
147
148
        // this is how they actually get it
149 3
        $kvs = '';
150 3
        foreach ($this->data as $k => $v) {
151 3
            if ($k !== 'key' && $k !== 'username') {
152 3
                $kvs .= ($kvs ? '&' : '') . "$k=$v";
153
            }
154
        }
155 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...
156
157
        /* Testing facility
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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