CompletePurchaseResponse   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 113
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A isSuccessful() 0 4 1
A getTestMode() 0 4 1
A getTransactionId() 0 4 1
A getTransactionReference() 0 4 1
A getAmount() 0 4 1
A getCurrency() 0 4 1
A getPayer() 0 4 1
A getTime() 0 4 1
A getHash() 0 4 1
A calculateHash() 0 12 1
1
<?php
2
3
/*
4
 * eCoin driver for Omnipay PHP payment library
5
 *
6
 * @link      https://github.com/hiqdev/omnipay-ecoin
7
 * @package   omnipay-ecoin
8
 * @license   MIT
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace Omnipay\eCoin\Message;
13
14
use Omnipay\Common\Exception\InvalidResponseException;
15
use Omnipay\Common\Message\AbstractResponse;
16
17
/**
18
 * eCoin Complete Purchase Response.
19
 */
20
class CompletePurchaseResponse extends AbstractResponse
21 3
{
22
    public function __construct(CompletePurchaseRequest $request, $data)
23 3
    {
24 3
        $this->request = $request;
25
        $this->data    = $data;
26 3
27 1
        if ($this->getHash() !== $this->calculateHash()) {
28
            throw new InvalidResponseException('Invalid hash');
29 2
        }
30
    }
31
32
    /**
33
     * Whether the payment is successful.
34
     * @return boolean
35 1
     */
36
    public function isSuccessful()
37 1
    {
38
        return true;
39
    }
40
41
    /**
42
     * Whether the payment is test.
43
     * XXX TODO.
44
     * @return boolean
45 1
     */
46
    public function getTestMode()
47 1
    {
48
        return (bool) $this->data['TEST_VAR_TO_BE_SET'];
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     * @return string
54 1
     */
55
    public function getTransactionId()
56 1
    {
57
        return $this->data['ECM_INV_NO'];
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     * @return string
63 1
     */
64
    public function getTransactionReference()
65 1
    {
66
        return $this->data['ECM_TRANS_ID'];
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     * @return string
72 1
     */
73
    public function getAmount()
74 1
    {
75
        return $this->data['ECM_ITEM_COST'];
76
    }
77
78
    /**
79
     * Returns the currency.
80
     * @return string
81 1
     */
82
    public function getCurrency()
83 1
    {
84
        return 'USD';
85
    }
86
87
    /**
88
     * Returns the payer ID.
89
     * @return string
90 1
     */
91
    public function getPayer()
92 1
    {
93
        return $this->data['ECM_PAYER_ID'];
94
    }
95
96
    /**
97
     * Returns the payment date.
98
     * @return string
99 1
     */
100
    public function getTime()
101 1
    {
102
        return date('c', $this->data['ECM_TRANS_DATE']);
103
    }
104
105
    /**
106
     * Get hash from request.
107
     *
108
     * @return string
109 3
     */
110
    public function getHash()
111 3
    {
112
        return $this->data['ECM_HASH'];
113
    }
114
115
    /**
116
     * Calculate hash to validate incoming confirmation.
117
     *
118
     * @return string
119 3
     */
120
    public function calculateHash()
121 3
    {
122 3
        $str =  $this->data['ECM_TRANS_ID'] .
123 3
                $this->data['ECM_TRANS_DATE'] .
124 3
                $this->request->getPurse() .
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 getPurse() does only exist in the following implementations of said interface: Omnipay\eCoin\Message\AbstractRequest, Omnipay\eCoin\Message\CompletePurchaseRequest, Omnipay\eCoin\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...
125 3
                $this->data['ECM_PAYER_ID'] .
126 3
                $this->data['ECM_ITEM_COST'] .
127 3
                $this->data['ECM_QTY'] .
128
                $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\eCoin\Message\AbstractRequest, Omnipay\eCoin\Message\CompletePurchaseRequest, Omnipay\eCoin\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...
129 3
130
        return md5($str);
131
    }
132
}
133