Passed
Push — master ( 7c85b2...443a3c )
by Andrey
03:17
created

IntercomMessage::from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @link      http://horoshop.ua
4
 * @copyright Copyright (c) 2015-2018 Horoshop TM
5
 * @author    Andrey Telesh <[email protected]>
6
 */
7
8
namespace FtwSoft\NotificationChannels\Intercom;
9
10
class IntercomMessage
11
{
12
13
    const TYPE_EMAIL = 'email';
14
15
    const TYPE_INAPP = 'inapp';
16
17
    const TEMPLATE_PLAIN = 'plain';
18
19
    const TEMPLATE_PERSONAL = 'personal';
20
21
    /**
22
     * @param string $body
23
     *
24
     * @return \FtwSoft\NotificationChannels\Intercom\IntercomMessage
25
     */
26
    public static function create(?string $body = null): IntercomMessage
27
    {
28
        return new static($body);
29
    }
30
31
    /**
32
     * @var array
33
     */
34
    public $payload;
35
36
    /**
37
     * IntercomMessage constructor.
38
     *
39
     * @param string $body
40
     */
41
    public function __construct(?string $body = null)
42
    {
43
        if ($body !== null) {
44
            $this->body($body);
45
        }
46
47
        $this->inapp();
48
    }
49
50
    /**
51
     * @param string $body
52
     *
53
     * @return \FtwSoft\NotificationChannels\Intercom\IntercomMessage
54
     */
55
    public function body(string $body): IntercomMessage
56
    {
57
        $this->payload['body'] = $body;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @return \FtwSoft\NotificationChannels\Intercom\IntercomMessage
64
     */
65
    public function email(): IntercomMessage
66
    {
67
        $this->payload['message_type'] = self::TYPE_EMAIL;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @return \FtwSoft\NotificationChannels\Intercom\IntercomMessage
74
     */
75
    public function inapp(): IntercomMessage
76
    {
77
        $this->payload['message_type'] = self::TYPE_INAPP;
78
79
        return $this;
80
    }
81
82
    /**
83
     * @param string $value
84
     *
85
     * @return \FtwSoft\NotificationChannels\Intercom\IntercomMessage
86
     */
87
    public function subject(string $value): IntercomMessage
88
    {
89
        $this->payload['subject'] = $value;
90
91
        return $this;
92
    }
93
94
    /**
95
     * @return \FtwSoft\NotificationChannels\Intercom\IntercomMessage
96
     */
97
    public function plain(): IntercomMessage
98
    {
99
        $this->payload['template'] = self::TEMPLATE_PLAIN;
100
101
        return $this;
102
    }
103
104
    /**
105
     * @return \FtwSoft\NotificationChannels\Intercom\IntercomMessage
106
     */
107
    public function personal(): IntercomMessage
108
    {
109
        $this->payload['template'] = self::TEMPLATE_PERSONAL;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @param string $adminId
116
     *
117
     * @return \FtwSoft\NotificationChannels\Intercom\IntercomMessage
118
     */
119
    public function from(string $adminId): IntercomMessage
120
    {
121
        $this->payload['from'] = [
122
            'type' => 'admin',
123
            'id'   => $adminId
124
        ];
125
126
        return $this;
127
    }
128
129
    /**
130
     * @param array $value
131
     *
132
     * @return \FtwSoft\NotificationChannels\Intercom\IntercomMessage
133
     */
134
    public function to(array $value): IntercomMessage
135
    {
136
        $this->payload['to'] = $value;
137
138
        return $this;
139
    }
140
141
    /**
142
     * @param string $id
143
     *
144
     * @return \FtwSoft\NotificationChannels\Intercom\IntercomMessage
145
     */
146
    public function toUserId(string $id): IntercomMessage
147
    {
148
        $this->payload['to'] = [
149
            'type' => 'user',
150
            'id'   => $id
151
        ];
152
153
        return $this;
154
    }
155
156
    /**
157
     * @param string $email
158
     *
159
     * @return \FtwSoft\NotificationChannels\Intercom\IntercomMessage
160
     */
161
    public function toUserEmail(string $email): IntercomMessage
162
    {
163
        $this->payload['to'] = [
164
            'type'  => 'user',
165
            'email' => $email
166
        ];
167
168
        return $this;
169
    }
170
171
    /**
172
     * @param string $id
173
     *
174
     * @return \FtwSoft\NotificationChannels\Intercom\IntercomMessage
175
     */
176
    public function toContactId(string $id): IntercomMessage
177
    {
178
        $this->payload['to'] = [
179
            'type' => 'contact',
180
            'id'   => $id
181
        ];
182
183
        return $this;
184
    }
185
186
    /**
187
     * @return bool
188
     */
189
    public function isValid(): bool
190
    {
191
        return isset(
192
            $this->payload['body'],
193
            $this->payload['from'],
194
            $this->payload['to']
195
        );
196
    }
197
198
    /**
199
     * @return bool
200
     */
201
    public function toIsGiven(): bool
202
    {
203
        return isset($this->payload['to']);
204
    }
205
206
    /**
207
     * @return array
208
     */
209
    public function toArray(): array
210
    {
211
        return $this->payload;
212
    }
213
214
}