Completed
Push — master ( 292fe8...64fdd0 )
by
unknown
08:04
created

Message::getNotificationType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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