Completed
Push — master ( 3d8f66...15d5e9 )
by Andrii
02:06
created

CompletePurchaseResponse::getCurrency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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
use Omnipay\Common\Message\RequestInterface;
17
18
/**
19
 * eCoin Complete Purchase Response.
20
 */
21
class CompletePurchaseResponse extends AbstractResponse
22
{
23 3
    public function __construct(RequestInterface $request, $data)
24
    {
25 3
        $this->request = $request;
26 3
        $this->data    = $data;
27
28 3
        if ($this->getHash() !== $this->calculateHash()) {
29 1
            throw new InvalidResponseException('Invalid hash');
30
        }
31 2
    }
32
33
    /**
34
     * Whether the payment is successful.
35
     * @return boolean
36
     */
37 1
    public function isSuccessful()
38
    {
39 1
        return true;
40
    }
41
42
    /**
43
     * Whether the payment is test.
44
     * XXX TODO.
45
     * @return boolean
46
     */
47 1
    public function getTestMode()
48
    {
49 1
        return (bool) $this->data['TEST_VAR_TO_BE_SET'];
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     * @return string
55
     */
56 1
    public function getTransactionId()
57
    {
58 1
        return $this->data['ECM_INV_NO'];
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     * @return string
64
     */
65 1
    public function getTransactionReference()
66
    {
67 1
        return $this->data['ECM_TRANS_ID'];
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     * @return string
73
     */
74 1
    public function getAmount()
75
    {
76 1
        return $this->data['ECM_ITEM_COST'];
77
    }
78
79
    /**
80
     * Returns the currency.
81
     * @return string
82
     */
83 1
    public function getCurrency()
84
    {
85 1
        return 'USD';
86
    }
87
88
    /**
89
     * Returns the payer ID.
90
     * @return string
91
     */
92 1
    public function getPayer()
93
    {
94 1
        return $this->data['ECM_PAYER_ID'];
95
    }
96
97
    /**
98
     * Returns the payment date.
99
     * @return string
100
     */
101 1
    public function getTime()
102
    {
103 1
        return date('c', $this->data['ECM_TRANS_DATE']);
104
    }
105
106
    /**
107
     * Get hash from request.
108
     *
109
     * @return string
110
     */
111 3
    public function getHash()
112
    {
113 3
        return $this->data['ECM_HASH'];
114
    }
115
116
    /**
117
     * Calculate hash to validate incoming confirmation.
118
     *
119
     * @return string
120
     */
121 3
    public function calculateHash()
122
    {
123 3
        $str =  $this->data['ECM_TRANS_ID'] .
124 3
                $this->data['ECM_TRANS_DATE'] .
125 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...
126 3
                $this->data['ECM_PAYER_ID'] .
127 3
                $this->data['ECM_ITEM_COST'] .
128 3
                $this->data['ECM_QTY'] .
129 3
                $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...
130
131 3
        return md5($str);
132
    }
133
}
134