CompletePurchaseResponse   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 52
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getHash() 0 4 1
A calculateHash() 0 4 1
A getTestMode() 0 4 1
A __construct() 0 9 2
A isSuccessful() 0 4 1
A getTransactionId() 0 4 1
A getTransactionReference() 0 4 1
A getAmount() 0 4 1
A getCurrency() 0 4 1
1
<?php
2
/**
3
 * ePayService driver for the Omnipay PHP payment processing library.
4
 *
5
 * @link      https://github.com/hiqdev/omnipay-epayservice
6
 * @package   omnipay-epayservice
7
 * @license   MIT
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace Omnipay\ePayService\Message;
12
13
use Omnipay\Common\Exception\InvalidResponseException;
14
use Omnipay\Common\Message\AbstractResponse;
15
use Omnipay\Common\Message\RequestInterface;
16
17
/**
18
 * ePayService Complete Purchase Response.
19
 */
20
class CompletePurchaseResponse extends AbstractResponse
21
{
22 3
    public function __construct(RequestInterface $request, $data)
23
    {
24 3
        $this->request = $request;
25 3
        $this->data    = $data;
26
27 3
        if ($this->getHash() !== $this->calculateHash()) {
28 1
            throw new InvalidResponseException('Invalid hash');
29
        }
30 2
    }
31
32 3
    public function getHash()
33
    {
34 3
        return strtolower($this->data['check_key']);
35
    }
36
37 3
    public function calculateHash()
38
    {
39 3
        return md5($this->data['EPS_AMOUNT'] . $this->data['EPS_GUID'] . $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\ePayService\Message\AbstractRequest, Omnipay\ePayService\Mess...CompletePurchaseRequest, Omnipay\ePayService\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...
40
    }
41
42 1
    public function isSuccessful()
43
    {
44 1
        return $this->data['EPS_RESULT'] === 'done';
45
    }
46
47 1
    public function getTransactionId()
48
    {
49 1
        return $this->data['EPS_TRID'];
50
    }
51
52 1
    public function getTransactionReference()
53
    {
54 1
        return $this->data['EPS_ACCNUM'];
55
    }
56
57 1
    public function getAmount()
58
    {
59 1
        return $this->data['EPS_AMOUNT'];
60
    }
61
62
    public function getTestMode()
63
    {
64
        return (bool) $this->data['testMode'];
65
    }
66
67 1
    public function getCurrency()
68
    {
69 1
        return $this->data['EPS_CURRENCY'];
70
    }
71
}
72