Completed
Push — master ( 3f4b39...daa8e9 )
by Kirill
10:57
created

Notification::isConfirmed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Personnage\Tinkoff\SDK\Merchant;
4
5
use function Personnage\Tinkoff\SDK\message_get_body;
6
use Personnage\Tinkoff\SDK\Exception\InvalidToken;
7
use Personnage\Tinkoff\SDK\PaymentCard;
8
use Psr\Http\Message\RequestInterface;
9
10
final class Notification implements \ArrayAccess, \IteratorAggregate, \JsonSerializable
11
{
12
    use HasSignature;
13
14
    const AUTHORIZED = 'AUTHORIZED';
15
    const CONFIRMED = 'CONFIRMED';
16
    const PARTIAL_REFUNDED = 'PARTIAL_REFUNDED';
17
    const REFUNDED = 'REFUNDED';
18
    const REJECTED = 'REJECTED';
19
    const REVERSED = 'REVERSED';
20
21
    /**
22
     * @var array
23
     */
24
    private $values;
25
26
    /**
27
     * Create a new instance.
28
     *
29
     * @param array $values
30
     */
31
    public function __construct(array $values)
32
    {
33
        if (isset($values['DATA'])) {
34
            $values['DATA'] = urldecode($values['DATA']);
35
        }
36
37
        $this->values = $values;
38
    }
39
40
    /**
41
     * Create a new instance from request instance.
42
     *
43
     * @param  RequestInterface  $request
44
     * @return self
45
     */
46
    public static function fromRequest(RequestInterface $request): self
47
    {
48
        return new self(message_get_body($request));
49
    }
50
51
    /**
52
     * Throws an exception if token is invalid.
53
     *
54
     * @param  string  $secret
55
     * @throws InvalidToken
56
     */
57
    public function validate(string $secret)
58
    {
59
        $values = $this->values;
60
        unset($values['Token']);
61
62
        if ($this->offsetGet('Token') !== $this->sign($values, $secret)) {
63
            throw new InvalidToken('Invalid token.');
64
        }
65
    }
66
67
    /**
68
     * Is request successful?
69
     *
70
     * @return bool
71
     */
72
    public function isSuccessful(): bool
73
    {
74
        return 'true' === $this->get('Success');
75
    }
76
77
    /**
78
     * Get current status.
79
     *
80
     * @return string|null
81
     */
82
    public function getStatus()
83
    {
84
        return $this->get('Status');
85
    }
86
87
    /**
88
     * Is request authorized?
89
     *
90
     * @return bool
91
     */
92
    public function isAuthorized(): bool
93
    {
94
        return $this->getStatus() === self::AUTHORIZED;
95
    }
96
97
    /**
98
     * Is request confirmed?
99
     *
100
     * @return bool
101
     */
102
    public function isConfirmed(): bool
103
    {
104
        return $this->getStatus() === self::CONFIRMED;
105
    }
106
107
    /**
108
     * Is request reversed?
109
     *
110
     * @return bool
111
     */
112
    public function isReversed(): bool
113
    {
114
        return $this->getStatus() === self::REVERSED;
115
    }
116
117
    /**
118
     * Is request refunded?
119
     *
120
     * @return bool
121
     */
122
    public function isRefunded(): bool
123
    {
124
        return $this->getStatus() === self::REFUNDED;
125
    }
126
127
    /**
128
     * Is request refunded?
129
     *
130
     * @return bool
131
     */
132
    public function isPartialRefunded(): bool
133
    {
134
        return $this->getStatus() === self::PARTIAL_REFUNDED;
135
    }
136
137
    /**
138
     * Is request rejected?
139
     *
140
     * @return bool
141
     */
142
    public function isRejected(): bool
143
    {
144
        return $this->getStatus() === self::REJECTED;
145
    }
146
147
    /**
148
     * Get a payment card instance if card id exists.
149
     *
150
     * @return PaymentCard|null
151
     */
152
    public function getPaymentCard()
153
    {
154
        if ($this->offsetExists('CardId')) {
155
            return PaymentCard::make($this->offsetGet('CardId'), $this->offsetGet('Pan'), $this->offsetGet('ExpDate'));
156
        }
157
    }
158
159
    /**
160
     * Get an iterator for the values.
161
     *
162
     * @return \ArrayIterator
163
     */
164
    public function getIterator()
165
    {
166
        return new \ArrayIterator($this->values);
167
    }
168
169
    /**
170
     * Determine if a value exists at an offset.
171
     *
172
     * @param  mixed  $key
173
     * @return bool
174
     */
175
    public function offsetExists($key)
176
    {
177
        return array_key_exists($key, $this->values);
178
    }
179
180
    /**
181
     * Get a value at a given offset.
182
     *
183
     * @param  mixed  $key
184
     * @return mixed
185
     */
186
    public function offsetGet($key)
187
    {
188
        return $this->values[$key];
189
    }
190
    /**
191
     * Set the value at a given offset.
192
     *
193
     * @param  mixed  $key
194
     * @param  mixed  $value
195
     * @return void
196
     */
197
    public function offsetSet($key, $value)
198
    {
199
        if (null === $value) {
200
            $this->offsetUnset($key);
201
        } else {
202
            $this->values[$key] = $value;
203
        }
204
    }
205
    /**
206
     * Unset the value at a given offset.
207
     *
208
     * @param  string  $key
209
     * @return void
210
     */
211
    public function offsetUnset($key)
212
    {
213
        unset($this->values[$key]);
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219
    public function jsonSerialize()
220
    {
221
        return $this->toArray();
222
    }
223
224
    public function get(string $key)
225
    {
226
        if ($this->offsetExists($key)) {
227
            return $this->offsetGet($key);
228
        }
229
    }
230
231
    /**
232
     * Get all values as JSON.
233
     *
234
     * @param  int  $options
235
     * @return string
236
     */
237
    public function toJson($options = 0): string
238
    {
239
        return json_encode($this->jsonSerialize(), $options);
240
    }
241
242
    /**
243
     * Get all values as a plain array.
244
     *
245
     * @return array
246
     */
247
    public function toArray(): array
248
    {
249
        return $this->values;
250
    }
251
}
252