ServerNotifyRequest::getMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Omnipay\Yoomoney\Message;
4
5
use Omnipay\Common\Exception\InvalidResponseException;
6
use Omnipay\Common\Http\ClientInterface;
7
use Omnipay\Common\Message\NotificationInterface;
8
use Omnipay\Yoomoney\Traits\ResponseFieldsTrait;
9
use Symfony\Component\HttpFoundation\Request as HttpRequest;
10
11
class ServerNotifyRequest extends AbstractRequest implements NotificationInterface
12
{
13
    use ResponseFieldsTrait;
14
15
    protected $data;
16
17
    public function __construct(ClientInterface $httpClient, HttpRequest $httpRequest)
18
    {
19
        parent::__construct($httpClient, $httpRequest);
20
21
        $this->data = $httpRequest->request->all();
22
    }
23
24
    public function getData()
25
    {
26
        if (!$this->isValid()) {
27
            throw new InvalidResponseException("Callback hash does not match expected value");
28
        }
29
30
        return $this->data;
31
    }
32
33
    public function sendData($data)
34
    {
35
        return $this->data;
36
    }
37
38
    public function isValid()
39
    {
40
        return $this->getSignature() === $this->buildSignature();
41
    }
42
43
    /**
44
     * Alias for isValid
45
     */
46
    public function isSuccessful()
47
    {
48
        return $this->isValid();
49
    }
50
51
    private function getSignature()
52
    {
53
        return $this->getSha1Hash();
54
    }
55
56
    private function buildSignature()
57
    {
58
        $params = [
59
            $this->getDataItem('notification_type'),
60
            $this->getDataItem('operation_id'),
61
            $this->getDataItem('amount'),
62
            $this->getDataItem('currency'),
63
            $this->getDataItem('datetime'),
64
            $this->getDataItem('sender'),
65
            $this->getDataItem('codepro'),
66
            $this->getSecret(),
67
            $this->getDataItem('label', ''),
68
        ];
69
70
        return hash('sha1', implode("&", $params));
71
    }
72
73
    /**
74
     * Was the transaction successful?
75
     *
76
     * @return string Transaction status, one of {@see STATUS_COMPLETED}, {@see #STATUS_PENDING},
77
     * or {@see #STATUS_FAILED}.
78
     */
79
    public function getTransactionStatus()
80
    {
81
        if (!$this->isValid()) {
82
            return self::STATUS_FAILED;
83
        }
84
85
        $isUnaccepted = $this->getUnaccepted();
86
87
        return filter_var($isUnaccepted, FILTER_VALIDATE_BOOL) ?
88
            self::STATUS_PENDING : self::STATUS_COMPLETED;
89
    }
90
91
    public function getMessage()
92
    {
93
        if (!$this->isValid()) {
94
            return sprintf('Callback hash does not match expected value');
95
        }
96
97
        return '';
98
    }
99
}
100