Completed
Push — master ( 6a5102...5cf183 )
by Andrii
05:50
created

src/Message/CompletePurchaseResponse.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
{
22 3
    public function __construct(CompletePurchaseRequest $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
    /**
33
     * Whether the payment is successful.
34
     * @return boolean
35
     */
36 1
    public function isSuccessful()
37
    {
38 1
        return true;
39
    }
40
41
    /**
42
     * Whether the payment is test.
43
     * XXX TODO.
44
     * @return boolean
45
     */
46 1
    public function getTestMode()
47
    {
48 1
        return (bool) $this->data['TEST_VAR_TO_BE_SET'];
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     * @return string
54
     */
55 1
    public function getTransactionId()
56
    {
57 1
        return $this->data['ECM_INV_NO'];
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     * @return string
63
     */
64 1
    public function getTransactionReference()
65
    {
66 1
        return $this->data['ECM_TRANS_ID'];
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     * @return string
72
     */
73 1
    public function getAmount()
74
    {
75 1
        return $this->data['ECM_ITEM_COST'];
76
    }
77
78
    /**
79
     * Returns the currency.
80
     * @return string
81
     */
82 1
    public function getCurrency()
83
    {
84 1
        return 'USD';
85
    }
86
87
    /**
88
     * Returns the payer ID.
89
     * @return string
90
     */
91 1
    public function getPayer()
92
    {
93 1
        return $this->data['ECM_PAYER_ID'];
94
    }
95
96
    /**
97
     * Returns the payment date.
98
     * @return string
99
     */
100 1
    public function getTime()
101
    {
102 1
        return date('c', $this->data['ECM_TRANS_DATE']);
103
    }
104
105
    /**
106
     * Get hash from request.
107
     *
108
     * @return string
109
     */
110 3
    public function getHash()
111
    {
112 3
        return $this->data['ECM_HASH'];
113
    }
114
115
    /**
116
     * Calculate hash to validate incoming confirmation.
117
     *
118
     * @return string
119
     */
120 3
    public function calculateHash()
121
    {
122 3
        $str =  $this->data['ECM_TRANS_ID'] .
123 3
                $this->data['ECM_TRANS_DATE'] .
124 3
                $this->request->getPurse() .
0 ignored issues
show
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 3
                $this->request->getSecret();
0 ignored issues
show
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
130 3
        return md5($str);
131
    }
132
}
133