Completed
Push — master ( c76d1a...b80522 )
by Dmitry
13:01
created

CompletePurchaseResponse::generateSignature()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * RoboKassa driver for Omnipay PHP payment library.
4
 *
5
 * @link      https://github.com/hiqdev/omnipay-robokassa
6
 * @package   omnipay-robokassa
7
 * @license   MIT
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace Omnipay\RoboKassa\Message;
12
13
use Omnipay\Common\Exception\InvalidResponseException;
14
use Omnipay\Common\Message\AbstractResponse;
15
use Omnipay\Common\Message\RequestInterface;
16
17
/**
18
 * RoboKassa Complete Purchase Response.
19
 */
20
class CompletePurchaseResponse extends AbstractResponse
21
{
22
    /** @var RequestInterface|CompletePurchaseRequest */
23
    protected $request;
24
25
    public function __construct(RequestInterface $request, $data)
26
    {
27
        $this->request = $request;
28
        $this->data    = $data;
29
30
        if (strtolower($this->getSignatureValue()) !== $this->generateSignature()) {
31
            throw new InvalidResponseException('Invalid hash');
32
        }
33
    }
34
35 View Code Duplication
    public function generateSignature()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        $params = [
38
            $this->getAmount(),
39
            '',
40
            $this->request->getSecretKey()
0 ignored issues
show
Bug introduced by
The method getSecretKey does only exist in Omnipay\RoboKassa\Message\CompletePurchaseRequest, but not in Omnipay\Common\Message\RequestInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
41
        ];
42
43
        foreach ($this->getCustomFields() as $field => $value) {
44
            $params[] = "$field=$value";
45
        }
46
47
        return md5(implode(':', $params));
48
    }
49
50 View Code Duplication
    public function getCustomFields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        $fields = array_filter([
53
            'Shp_TransactionId' => $this->getTransactionId(),
54
            'Shp_Client' => $this->getClient(),
55
            'Shp_Currency' => $this->getCurrency(),
56
        ]);
57
58
        ksort($fields);
59
60
        return $fields;
61
    }
62
63
    public function getSignatureValue()
64
    {
65
        return $this->data['SignatureValue'];
66
    }
67
68
    public function getClient()
69
    {
70
        return $this->data['Shp_Client'];
71
    }
72
73
    public function getAmount()
74
    {
75
        return $this->data['OutSum'];
76
    }
77
78
    public function getPayer()
79
    {
80
        return $this->data['PaymentMethod'];
81
    }
82
83
    public function getTransactionId()
84
    {
85
        return $this->data['Shp_TransactionId'];
86
    }
87
88
    public function getCurrency()
89
    {
90
        return $this->data['Shp_Currency'];
91
    }
92
93
    public function getTransactionReference()
94
    {
95
        return '';
96
    }
97
98
    public function isSuccessful()
99
    {
100
        return true;
101
    }
102
}
103