NotificationFilter::target()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace Link0\Bunq\Domain;
4
5
final class NotificationFilter
6
{
7
    const DELIVERYMETHOD_PUSH = 'PUSH';
8
    const DELIVERYMETHOD_CALLBACK = 'URL';
9
10
    const CATEGORY_BANK_SWITCH_SERVICE = 'BANK_SWITCH_SERVICE';
11
    const CATEGORY_BANK_SWITCH_SERVICE_PAYMENT = 'BANK_SWITCH_SERVICE_PAYMENT';
12
    const CATEGORY_BILLING = 'BILLING';
13
    const CATEGORY_CARD_TRANSACTION_FAILED = 'CARD_TRANSACTION_FAILED';
14
    const CATEGORY_CARD_TRANSACTION_SUCCESSFUL = 'CARD_TRANSACTION_SUCCESSFUL';
15
    const CATEGORY_CHAT = 'CHAT';
16
    const CATEGORY_DRAFT_PAYMENT = 'DRAFT_PAYMENT';
17
    const CATEGORY_IDEAL = 'IDEAL';
18
    const CATEGORY_SOFORT = 'SOFORT';
19
    const CATEGORY_FRIEND_SIGN_UP = 'FRIEND_SIGN_UP';
20
    const CATEGORY_MONETARY_ACCOUNT_PROFILE = 'MONETARY_ACCOUNT_PROFILE';
21
    const CATEGORY_MUTATION = 'MUTATION';
22
    const CATEGORY_PAYMENT = 'PAYMENT';
23
    const CATEGORY_PROMOTION = 'PROMOTION';
24
    const CATEGORY_REQUEST = 'REQUEST';
25
    const CATEGORY_SCHEDULE_RESULT = 'SCHEDULE_RESULT';
26
    const CATEGORY_SCHEDULE_STATUS = 'SCHEDULE_STATUS';
27
    const CATEGORY_SHARE = 'SHARE';
28
    const CATEGORY_SUPPORT = 'SUPPORT';
29
    const CATEGORY_TAB_RESULT = 'TAB_RESULT';
30
    const CATEGORY_USER_APPROVAL = 'USER_APPROVAL';
31
    const CATEGORY_USE_RESPONSE = 'USE_RESPONSE';
32
    const CATEGORY_USE_RESPONSE_NATIVE_COMMENT = 'USE_RESPONSE_NATIVE_COMMENT';
33
    const CATEGORY_USE_RESPONSE_NATIVE_TOPIC = 'USE_RESPONSE_NATIVE_TOPIC';
34
    
35
    const CATEGORY_SLICE_CHAT = 'SLICE_CHAT';
36
    const CATEGORY_SLICE_REGISTRY_ENTRY = 'SLICE_REGISTRY_ENTRY';
37
    const CATEGORY_SLICE_REGISTRY_MEMBERSHIP = 'SLICE_REGISTRY_MEMBERSHIP';
38
    const CATEGORY_SLICE_REGISTRY_SETTLEMENT = 'SLICE_REGISTRY_SETTLEMENT';
39
40
    const CATEGORY_WHITELIST = 'WHITELIST';
41
    const CATEGORY_WHITELIST_RESULT = 'WHITELIST_RESULT';
42
43
    /**
44
     * @var string
45
     */
46
    private $deliveryMethod;
47
48
    /**
49
     * @var string
50
     */
51
    private $target;
52
53
    /**
54
     * @var string
55
     */
56
    private $category;
57
58
    /**
59
     * @param string $deliveryMethod
60
     * @param string $target
61
     * @param string $category
62
     */
63
    private function __construct(string $deliveryMethod, string $target, string $category)
64
    {
65
        $this->guardDeliveryMethod($deliveryMethod);
66
        $this->guardCategory($category);
67
68
        $this->deliveryMethod = $deliveryMethod;
69
        $this->target = $target;
70
        $this->category = $category;
71
    }
72
73
    /**
74
     * @param array $notificationFilterStruct
75
     * @return NotificationFilter
76
     */
77
    public static function fromArray(array $notificationFilterStruct): NotificationFilter
78
    {
79
        // Target is optional (required for callback, not push)
80
        $notificationTarget = '';
81
        if (isset($notificationFilterStruct['notification_target'])) {
82
            $notificationTarget = $notificationFilterStruct['notification_target'];
83
        }
84
85
        return new self(
86
            $notificationFilterStruct['notification_delivery_method'],
87
            $notificationTarget,
88
            $notificationFilterStruct['category']
89
        );
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function toArray()
96
    {
97
        return [
98
            'notification_delivery_method' => $this->deliveryMethod(),
99
            'notification_target' => $this->target(),
100
            'category' => $this->category(),
101
        ];
102
    }
103
104
    /**
105
     * @param string $category
106
     * @return NotificationFilter
107
     */
108
    public static function createPush(string $category)
109
    {
110
        return new self(self::DELIVERYMETHOD_PUSH, '', $category);
111
    }
112
113
    /**
114
     * @param string $callbackUrl
115
     * @param string $category
116
     * @return NotificationFilter
117
     */
118
    public static function createCallback(string $callbackUrl, string $category)
119
    {
120
        return new self(self::DELIVERYMETHOD_CALLBACK, $callbackUrl, $category);
121
    }
122
123
    /**
124
     * @return void
125
     * @throws \Exception
126
     */
127
    private function guardCategory($category)
128
    {
129
        $reflectionClass = new \ReflectionClass(__CLASS__);
130
        if (!array_key_exists('CATEGORY_' . $category, $reflectionClass->getConstants())) {
131
            throw new \Exception("Invalid NotificationFilter->category '{$category}'");
132
        }
133
    }
134
135
    /**
136
     * @param $deliveryMethod
137
     * @return void
138
     * @throws \Exception
139
     */
140
    private function guardDeliveryMethod($deliveryMethod)
141
    {
142
        if (!in_array($deliveryMethod, [
143
            self::DELIVERYMETHOD_PUSH,
144
            self::DELIVERYMETHOD_CALLBACK,
145
        ])) {
146
            throw new \Exception("Invalid NotificationFilter->deliveryMethod '{$deliveryMethod}'");
147
        }
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function deliveryMethod(): string
154
    {
155
        return $this->deliveryMethod;
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function target(): string
162
    {
163
        return $this->deliveryMethod;
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function category(): string
170
    {
171
        return $this->category;
172
    }
173
}
174