Completed
Push — master ( 11f8f0...b6346a )
by
unknown
04:00
created

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