Message::isPayment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Xsolla\SDK\Webhook\Message;
4
5
use Xsolla\SDK\Exception\Webhook\InvalidParameterException;
6
7
abstract class Message
8
{
9
    const USER_VALIDATION = 'user_validation';
10
    const USER_SEARCH = 'user_search';
11
    const PAYMENT = 'payment';
12
    const REFUND = 'refund';
13
    const CREATE_SUBSCRIPTION = 'create_subscription';
14
    const CANCEL_SUBSCRIPTION = 'cancel_subscription';
15
    const UPDATE_SUBSCRIPTION = 'update_subscription';
16
    const USER_BALANCE = 'user_balance_operation';
17
    const GET_PIN_CODE = 'get_pincode';
18
    const AFS_REJECT = 'afs_reject';
19
20
    protected static $classMap = [
21
        self::USER_VALIDATION => '\Xsolla\SDK\Webhook\Message\UserValidationMessage',
22
        self::USER_SEARCH => '\Xsolla\SDK\Webhook\Message\UserSearchMessage',
23
        self::PAYMENT => '\Xsolla\SDK\Webhook\Message\PaymentMessage',
24
        self::REFUND => '\Xsolla\SDK\Webhook\Message\RefundMessage',
25
        self::CREATE_SUBSCRIPTION => '\Xsolla\SDK\Webhook\Message\CreateSubscriptionMessage',
26
        self::CANCEL_SUBSCRIPTION => '\Xsolla\SDK\Webhook\Message\CancelSubscriptionMessage',
27
        self::UPDATE_SUBSCRIPTION => '\Xsolla\SDK\Webhook\Message\UpdateSubscriptionMessage',
28
        self::USER_BALANCE => '\Xsolla\SDK\Webhook\Message\UserBalanceMessage',
29
        self::GET_PIN_CODE => '\Xsolla\SDK\Webhook\Message\GetPinCodeMessage',
30
        self::AFS_REJECT => '\Xsolla\SDK\Webhook\Message\AfsRejectMessage'
31
    ];
32
33
    /**
34
     * @var array
35
     */
36
    protected $request;
37
38
    /**
39
     * @param array $request
40
     *
41
     * @throws InvalidParameterException
42 9
     * @return Message
43
     */
44 9
    public static function fromArray(array $request)
45
    {
46
        if (!array_key_exists('notification_type', $request)) {
47 9
            throw new InvalidParameterException('notification_type key not found in Xsolla webhook request');
48 9
        }
49
        $notificationType = $request['notification_type'];
50
        if (!array_key_exists($notificationType, self::$classMap)) {
51 9
            throw new InvalidParameterException('Unknown notification_type in Xsolla webhook request: '.$notificationType);
52
        }
53 9
        $className = self::$classMap[$notificationType];
54
55
        return new $className($request);
56
    }
57
58
    /**
59 20
     * @param array $request
60
     */
61 20
    public function __construct(array $request)
62
    {
63
        $this->request = $request;
64
    }
65
66
    /**
67 9
     * @return array
68
     */
69 9
    public function toArray()
70
    {
71
        return $this->request;
72
    }
73
74
    /**
75 9
     * @return string
76
     */
77 9
    public function getNotificationType()
78
    {
79
        return $this->request['notification_type'];
80
    }
81
82
    /**
83 9
     * @return bool
84
     */
85 9
    public function isUserValidation()
86
    {
87
        return self::USER_VALIDATION === $this->getNotificationType();
88
    }
89
90
    /**
91 9
     * @return bool
92
     */
93 9
    public function isPayment()
94
    {
95
        return self::PAYMENT === $this->getNotificationType();
96
    }
97
98
    /**
99 9
     * @return bool
100
     */
101 9
    public function isRefund()
102
    {
103
        return self::REFUND === $this->getNotificationType();
104
    }
105
106
    /**
107 10
     * @return array
108
     */
109 10
    public function getUser()
110
    {
111
        return $this->request['user'];
112
    }
113
114
    /**
115 8
     * @return string
116
     */
117 8
    public function getUserId()
118
    {
119
        return $this->request['user']['id'];
120
    }
121
}
122