MailtoImmutableQuery   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 297
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 297
rs 9.6
c 0
b 0
f 0
wmc 35

21 Methods

Rating   Name   Duplication   Size   Complexity  
A hasInBcc() 0 3 1
A getBcc() 0 3 1
A firstInCc() 0 3 1
A getCc() 0 3 1
A setTo() 0 6 2
A getBody() 0 3 1
A setCc() 0 6 2
A hasInCc() 0 3 1
A lastInCc() 0 3 1
A lastInBcc() 0 3 1
A setBcc() 0 6 2
A all() 0 8 1
A __construct() 0 9 2
A getSubject() 0 3 1
B buildFromParsed() 0 32 11
A raw() 0 3 1
A lastInTo() 0 3 1
A firstInBcc() 0 3 1
A hasInTo() 0 3 1
A getTo() 0 3 1
A firstInTo() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Keppler\Url\Scheme\Schemes\Mailto\Bags;
5
6
use Keppler\Url\Interfaces\Immutable\ImmutableBagInterface;
7
use Keppler\Url\Scheme\Schemes\AbstractImmutable;
8
use Keppler\Url\Traits\Accessor;
9
10
/**
11
 * Class MailtoImmutableQuery
12
 *
13
 * @package Keppler\Url\Schemes\MailtoImmutable\Bags
14
 */
15
class MailtoImmutableQuery extends AbstractImmutable implements ImmutableBagInterface
16
{
17
    use Accessor;
18
19
    /**
20
     * To recipients, can be more than one as
21
     * long they are separated by a comma
22
     *
23
     * @var array
24
     */
25
    private $to = [];
26
27
    /**
28
     * CarbonCopy recipients, can be more than one
29
     * as long as they are separated by a comma
30
     *
31
     * @var array
32
     */
33
    private $cc = [];
34
35
    /**
36
     * BlindCarbonCopy recipients, can be more than
37
     * one as long as they are separated by a comma
38
     *
39
     * @var array
40
     */
41
    private $bcc = [];
42
43
    /**
44
     * @var string
45
     */
46
    private $subject = '';
47
48
    /**
49
     * @var string
50
     */
51
    private $body = '';
52
53
    /**
54
     * The raw query string
55
     *
56
     * @var string
57
     */
58
    private $raw = '';
59
60
    /**
61
     * This should be the ONLY entry point and it should accept ONLY the raw string
62
     *
63
     * MailtoImmutableQuery constructor.
64
     *
65
     * @param string $raw
66
     */
67
    public function __construct(string $raw = '')
68
    {
69
        // Leave the class with defaults if no valid raw string is provided
70
        if ('' !== trim($raw)) {
71
            $this->raw = $raw;
72
73
            $result = [];
74
            parse_str($raw, $result);
75
            $this->buildFromParsed($result);
76
        }
77
    }
78
79
    /**
80
     * @param array $parsed
81
     */
82
    private function buildFromParsed(array $parsed): void
83
    {
84
        // NOTE!
85
        // No validation of emails will occur, it's the job of the caller to do that
86
87
        // Check ONLY for accepted values in the rfc
88
        // There's no point in looking for something else
89
90
        // Try to set $to
91
        if (isset($parsed['to']) && !empty(trim($parsed['to']))) {
92
            $this->setTo($parsed['to']);
93
        }
94
95
        // Try to set $cc
96
        if (isset($parsed['cc']) && !empty(trim($parsed['cc']))) {
97
            $this->setCc($parsed['cc']);
98
        }
99
100
        // Try to set $bcc
101
        if (isset($parsed['bcc']) && !empty(trim($parsed['bcc']))) {
102
            $this->setBcc($parsed['bcc']);
103
        }
104
105
        // Try to set $subject
106
        if (isset($parsed['subject']) && !empty(trim($parsed['subject']))
107
        ) {
108
            $this->subject = $parsed['subject'];
109
        }
110
111
        // Try to set $body
112
        if (isset($parsed['body']) && !empty(trim($parsed['body']))) {
113
            $this->body = $parsed['body'];
114
        }
115
    }
116
117
////////////////////////////
118
/// PRIVATE FUNCTIONS   ///
119
//////////////////////////
120
121
    /**
122
     * @param string $bcc
123
     */
124
    private function setBcc(string $bcc): void
125
    {
126
        if (false !== strpos($bcc, ',')) {
127
            $this->bcc = explode(',', $bcc);
128
        } else {
129
            $this->bcc[] = $bcc;
130
        }
131
    }
132
133
    /**
134
     * @param string $cc
135
     */
136
    private function setCc(string $cc): void
137
    {
138
        if (false !== strpos($cc, ',')) {
139
            $this->cc = explode(',', $cc);
140
        } else {
141
            $this->cc[] = $cc;
142
        }
143
    }
144
145
    /**
146
     * @param string $to
147
     */
148
    private function setTo(string $to): void
149
    {
150
        if (false !== strpos($to, ',')) {
151
            $this->to = explode(',', $to);
152
        } else {
153
            $this->to[] = $to;
154
        }
155
    }
156
157
/////////////////////
158
/// TO FUNCTIONS  ///
159
////////////////////
160
161
    /**
162
     * @param string $value
163
     * @return bool
164
     */
165
    public function hasInTo(string $value): bool
166
    {
167
        return $this->hasValueIn($this->to, $value);
168
    }
169
170
    /**
171
     * @return string
172
     */
173
    public function firstInTo(): ?string
174
    {
175
        return $this->firstInPath($this->to);
176
    }
177
178
    /**
179
     * @return string|null
180
     */
181
    public function lastInTo(): ?string
182
    {
183
        return $this->lastInPath($this->to);
184
    }
185
186
/////////////////////
187
/// CC FUNCTIONS  ///
188
////////////////////
189
190
    /**
191
     * @param string $value
192
     * @return bool
193
     */
194
    public function hasInCc(string $value): bool
195
    {
196
        return $this->hasValueIn($this->cc, $value);
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    public function firstInCc(): ?string
203
    {
204
        return $this->firstInPath($this->cc);
205
    }
206
207
    /**
208
     * @return string
209
     */
210
    public function lastInCc(): ?string
211
    {
212
        return $this->lastInPath($this->cc);
213
    }
214
215
//////////////////////
216
/// BCC FUNCTIONS  ///
217
/////////////////////
218
219
    /**
220
     * @param string $value
221
     * @return bool
222
     */
223
    public function hasInBcc(string $value): bool
224
    {
225
        return $this->hasValueIn($this->bcc, $value);
226
    }
227
228
    /**
229
     * @return string|null
230
     */
231
    public function firstInBcc(): ?string
232
    {
233
        return $this->firstInPath($this->bcc);
234
    }
235
236
    /**
237
     * @return string
238
     */
239
    public function lastInBcc(): ?string
240
    {
241
        return $this->lastInPath($this->bcc);
242
    }
243
244
/////////////////////////
245
/// GETTER FUNCTIONS  ///
246
////////////////////////
247
248
    /**
249
     * @return string
250
     */
251
    public function getSubject(): string
252
    {
253
        return $this->subject;
254
    }
255
256
    /**
257
     * @return string
258
     */
259
    public function getBody(): string
260
    {
261
        return $this->body;
262
    }
263
264
    /**
265
     * @return array
266
     */
267
    public function getBcc(): array
268
    {
269
        return $this->bcc;
270
    }
271
272
    /**
273
     * @return array
274
     */
275
    public function getCc(): array
276
    {
277
        return $this->cc;
278
    }
279
280
    /**
281
     * @return array
282
     */
283
    public function getTo(): array
284
    {
285
        return $this->to;
286
    }
287
288
/////////////////////////////////
289
/// INTERFACE IMPLEMENTATION  ///
290
/////////////////////////////////
291
292
    /**
293
     * @inheritDoc
294
     */
295
    public function all(): array
296
    {
297
        return [
298
            'to' => $this->to,
299
            'cc' => $this->cc,
300
            'bcc' => $this->bcc,
301
            'subject' => $this->subject,
302
            'body' => $this->body,
303
        ];
304
    }
305
306
    /**
307
     * @return string
308
     */
309
    public function raw(): string
310
    {
311
        return $this->raw;
312
    }
313
}
314