Passed
Pull Request — master (#147)
by
unknown
02:46
created

WhereQuery::whereRecent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/*
3
* File:     Query.php
4
* Category: -
5
* Author:   M. Goldenbaum
6
* Created:  21.07.18 18:54
7
* Updated:  -
8
*
9
* Description:
10
*  -
11
*/
12
13
namespace Webklex\PHPIMAP\Query;
14
15
use Closure;
16
use Illuminate\Support\Str;
17
use Webklex\PHPIMAP\Exceptions\InvalidWhereQueryCriteriaException;
18
use Webklex\PHPIMAP\Exceptions\MethodNotFoundException;
19
use Webklex\PHPIMAP\Exceptions\MessageSearchValidationException;
20
21
/**
22
 * Class WhereQuery
23
 *
24
 * @package Webklex\PHPIMAP\Query
25
 *
26
 * @method WhereQuery all()
27
 * @method WhereQuery answered()
28
 * @method WhereQuery deleted()
29
 * @method WhereQuery new()
30
 * @method WhereQuery old()
31
 * @method WhereQuery recent()
32
 * @method WhereQuery seen()
33
 * @method WhereQuery unanswered()
34
 * @method WhereQuery undeleted()
35
 * @method WhereQuery unflagged()
36
 * @method WhereQuery unseen()
37
 * @method WhereQuery not()
38
 * @method WhereQuery unkeyword($value)
39
 * @method WhereQuery to($value)
40
 * @method WhereQuery text($value)
41
 * @method WhereQuery subject($value)
42
 * @method WhereQuery since($date)
43
 * @method WhereQuery on($date)
44
 * @method WhereQuery keyword($value)
45
 * @method WhereQuery from($value)
46
 * @method WhereQuery flagged()
47
 * @method WhereQuery cc($value)
48
 * @method WhereQuery body($value)
49
 * @method WhereQuery before($date)
50
 * @method WhereQuery bcc($value)
51
 * @method WhereQuery inReplyTo($value)
52
 * @method WhereQuery messageId($value)
53
 *
54
 * @mixin Query
55
 */
56
class WhereQuery extends Query {
57
58
    /**
59
     * @var array $available_criteria
60
     */
61
    protected $available_criteria = [
62
        'OR', 'AND',
63
        'ALL', 'ANSWERED', 'BCC', 'BEFORE', 'BODY', 'CC', 'DELETED', 'FLAGGED', 'FROM', 'KEYWORD',
64
        'NEW', 'NOT', 'OLD', 'ON', 'RECENT', 'SEEN', 'SINCE', 'SUBJECT', 'TEXT', 'TO',
65
        'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNKEYWORD', 'UNSEEN'
66
    ];
67
68
    /**
69
     * Magic method in order to allow alias usage of all "where" methods in an optional connection with "NOT"
70
     * @param string $name
71
     * @param array|null $arguments
72
     *
73
     * @return mixed
74
     * @throws InvalidWhereQueryCriteriaException
75
     * @throws MethodNotFoundException
76
     */
77
    public function __call($name, $arguments) {
78
        $that = $this;
79
80
        $name = Str::camel($name);
81
82
        if (strtolower(substr($name, 0, 3)) === 'not') {
83
            $that = $that->whereNot();
84
            $name = substr($name, 3);
85
        }
86
87
        if (strpos(strtolower($name), "where") === false) {
88
            $method = 'where' . ucfirst($name);
89
        } else {
90
            $method = lcfirst($name);
91
        }
92
93
        if (method_exists($this, $method) === true) {
94
            return call_user_func_array([$that, $method], $arguments);
0 ignored issues
show
Bug introduced by
It seems like $arguments can also be of type null; however, parameter $args of call_user_func_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
            return call_user_func_array([$that, $method], /** @scrutinizer ignore-type */ $arguments);
Loading history...
95
        }
96
97
        throw new MethodNotFoundException("Method " . self::class . '::' . $method . '() is not supported');
98
    }
99
100
    /**
101
     * Validate a given criteria
102
     * @param $criteria
103
     *
104
     * @return string
105
     * @throws InvalidWhereQueryCriteriaException
106
     */
107
    protected function validate_criteria($criteria) {
108
        $criteria = strtoupper($criteria);
109
        if (substr($criteria, 0, 7) === "CUSTOM ") {
110
            return substr($criteria, 7);
111
        }
112
        if (in_array($criteria, $this->available_criteria) === false) {
113
            throw new InvalidWhereQueryCriteriaException();
114
        }
115
116
        return $criteria;
117
    }
118
119
    /**
120
     * @param mixed $criteria
121
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
122
     *
123
     * @return $this
124
     * @throws InvalidWhereQueryCriteriaException
125
     */
126
    public function where($criteria, $value = null) {
127
        if (is_array($criteria)) {
128
            foreach ($criteria as $key => $value) {
129
                if (is_numeric($key)) {
130
                    return $this->where($value);
131
                }
132
                return $this->where($key, $value);
133
            }
134
        } else {
135
            $criteria = $this->validate_criteria($criteria);
136
            $value = $this->parse_value($value);
137
138
            if ($value === null || $value === '') {
139
                $this->query->push([$criteria]);
140
            } else {
141
                $this->query->push([$criteria, $value]);
142
            }
143
        }
144
145
        return $this;
146
    }
147
148
    /**
149
     * @param Closure $closure
150
     *
151
     * @return $this
152
     */
153
    public function orWhere(Closure $closure = null) {
154
        $this->query->push(['OR']);
155
        if ($closure !== null) $closure($this);
156
157
        return $this;
158
    }
159
160
    /**
161
     * @param Closure $closure
162
     *
163
     * @return $this
164
     */
165
    public function andWhere(Closure $closure = null) {
166
        $this->query->push(['AND']);
167
        if ($closure !== null) $closure($this);
168
169
        return $this;
170
    }
171
172
    /**
173
     * @return WhereQuery
174
     * @throws InvalidWhereQueryCriteriaException
175
     */
176
    public function whereAll() {
177
        return $this->where('ALL');
178
    }
179
180
    /**
181
     * @return WhereQuery
182
     * @throws InvalidWhereQueryCriteriaException
183
     */
184
    public function whereAnswered() {
185
        return $this->where('ANSWERED');
186
    }
187
188
    /**
189
     * @param string $value
190
     *
191
     * @return WhereQuery
192
     * @throws InvalidWhereQueryCriteriaException
193
     */
194
    public function whereBcc($value) {
195
        return $this->where('BCC', $value);
196
    }
197
198
    /**
199
     * @param mixed $value
200
     * @return WhereQuery
201
     * @throws InvalidWhereQueryCriteriaException
202
     * @throws MessageSearchValidationException
203
     */
204
    public function whereBefore($value) {
205
        $date = $this->parse_date($value);
206
        return $this->where('BEFORE', $date);
207
    }
208
209
    /**
210
     * @param string $value
211
     *
212
     * @return WhereQuery
213
     * @throws InvalidWhereQueryCriteriaException
214
     */
215
    public function whereBody($value) {
216
        return $this->where('BODY', $value);
217
    }
218
219
    /**
220
     * @param string $value
221
     *
222
     * @return WhereQuery
223
     * @throws InvalidWhereQueryCriteriaException
224
     */
225
    public function whereCc($value) {
226
        return $this->where('CC', $value);
227
    }
228
229
    /**
230
     * @return WhereQuery
231
     * @throws InvalidWhereQueryCriteriaException
232
     */
233
    public function whereDeleted() {
234
        return $this->where('DELETED');
235
    }
236
237
    /**
238
     * @param string $value
239
     *
240
     * @return WhereQuery
241
     * @throws InvalidWhereQueryCriteriaException
242
     */
243
    public function whereFlagged($value) {
244
        return $this->where('FLAGGED', $value);
245
    }
246
247
    /**
248
     * @param string $value
249
     *
250
     * @return WhereQuery
251
     * @throws InvalidWhereQueryCriteriaException
252
     */
253
    public function whereFrom($value) {
254
        return $this->where('FROM', $value);
255
    }
256
257
    /**
258
     * @param string $value
259
     *
260
     * @return WhereQuery
261
     * @throws InvalidWhereQueryCriteriaException
262
     */
263
    public function whereKeyword($value) {
264
        return $this->where('KEYWORD', $value);
265
    }
266
267
    /**
268
     * @return WhereQuery
269
     * @throws InvalidWhereQueryCriteriaException
270
     */
271
    public function whereNew() {
272
        return $this->where('NEW');
273
    }
274
275
    /**
276
     * @return WhereQuery
277
     * @throws InvalidWhereQueryCriteriaException
278
     */
279
    public function whereNot() {
280
        return $this->where('NOT');
281
    }
282
283
    /**
284
     * @return WhereQuery
285
     * @throws InvalidWhereQueryCriteriaException
286
     */
287
    public function whereOld() {
288
        return $this->where('OLD');
289
    }
290
291
    /**
292
     * @param mixed $value
293
     *
294
     * @return WhereQuery
295
     * @throws MessageSearchValidationException
296
     * @throws InvalidWhereQueryCriteriaException
297
     */
298
    public function whereOn($value) {
299
        $date = $this->parse_date($value);
300
        return $this->where('ON', $date);
301
    }
302
303
    /**
304
     * @return WhereQuery
305
     * @throws InvalidWhereQueryCriteriaException
306
     */
307
    public function whereRecent() {
308
        return $this->where('RECENT');
309
    }
310
311
    /**
312
     * @return WhereQuery
313
     * @throws InvalidWhereQueryCriteriaException
314
     */
315
    public function whereSeen() {
316
        return $this->where('SEEN');
317
    }
318
319
    /**
320
     * @param mixed $value
321
     *
322
     * @return WhereQuery
323
     * @throws MessageSearchValidationException
324
     * @throws InvalidWhereQueryCriteriaException
325
     */
326
    public function whereSince($value) {
327
        $date = $this->parse_date($value);
328
        return $this->where('SINCE', $date);
329
    }
330
331
    /**
332
     * @param string $value
333
     *
334
     * @return WhereQuery
335
     * @throws InvalidWhereQueryCriteriaException
336
     */
337
    public function whereSubject($value) {
338
        return $this->where('SUBJECT', $value);
339
    }
340
341
    /**
342
     * @param string $value
343
     *
344
     * @return WhereQuery
345
     * @throws InvalidWhereQueryCriteriaException
346
     */
347
    public function whereText($value) {
348
        return $this->where('TEXT', $value);
349
    }
350
351
    /**
352
     * @param string $value
353
     *
354
     * @return WhereQuery
355
     * @throws InvalidWhereQueryCriteriaException
356
     */
357
    public function whereTo($value) {
358
        return $this->where('TO', $value);
359
    }
360
361
    /**
362
     * @param string $value
363
     *
364
     * @return WhereQuery
365
     * @throws InvalidWhereQueryCriteriaException
366
     */
367
    public function whereUnkeyword($value) {
368
        return $this->where('UNKEYWORD', $value);
369
    }
370
371
    /**
372
     * @return WhereQuery
373
     * @throws InvalidWhereQueryCriteriaException
374
     */
375
    public function whereUnanswered() {
376
        return $this->where('UNANSWERED');
377
    }
378
379
    /**
380
     * @return WhereQuery
381
     * @throws InvalidWhereQueryCriteriaException
382
     */
383
    public function whereUndeleted() {
384
        return $this->where('UNDELETED');
385
    }
386
387
    /**
388
     * @return WhereQuery
389
     * @throws InvalidWhereQueryCriteriaException
390
     */
391
    public function whereUnflagged() {
392
        return $this->where('UNFLAGGED');
393
    }
394
395
    /**
396
     * @return WhereQuery
397
     * @throws InvalidWhereQueryCriteriaException
398
     */
399
    public function whereUnseen() {
400
        return $this->where('UNSEEN');
401
    }
402
403
    /**
404
     * @return WhereQuery
405
     * @throws InvalidWhereQueryCriteriaException
406
     */
407
    public function whereNoXSpam() {
408
        return $this->where("CUSTOM X-Spam-Flag NO");
409
    }
410
411
    /**
412
     * @return WhereQuery
413
     * @throws InvalidWhereQueryCriteriaException
414
     */
415
    public function whereIsXSpam() {
416
        return $this->where("CUSTOM X-Spam-Flag YES");
417
    }
418
419
    /**
420
     * Search for a specific header value
421
     * @param $header
422
     * @param $value
423
     *
424
     * @return WhereQuery
425
     * @throws InvalidWhereQueryCriteriaException
426
     */
427
    public function whereHeader($header, $value) {
428
        return $this->where("CUSTOM HEADER $header $value");
429
    }
430
431
    /**
432
     * Search for a specific message id
433
     * @param $messageId
434
     *
435
     * @return WhereQuery
436
     * @throws InvalidWhereQueryCriteriaException
437
     */
438
    public function whereMessageId($messageId) {
439
        return $this->whereHeader("Message-ID", $messageId);
440
    }
441
442
    /**
443
     * Search for a specific message id
444
     * @param $messageId
445
     *
446
     * @return WhereQuery
447
     * @throws InvalidWhereQueryCriteriaException
448
     */
449
    public function whereInReplyTo($messageId) {
450
        return $this->whereHeader("In-Reply-To", $messageId);
451
    }
452
453
    /**
454
     * @param $country_code
455
     *
456
     * @return WhereQuery
457
     * @throws InvalidWhereQueryCriteriaException
458
     */
459
    public function whereLanguage($country_code) {
460
        return $this->where("Content-Language $country_code");
461
    }
462
463
    /**
464
     * Apply the callback if the given "value" is truthy.
465
     * copied from @url https://github.com/laravel/framework/blob/8.x/src/Illuminate/Support/Traits/Conditionable.php
466
     *
467
     * @param mixed  $value
468
     * @param callable  $callback
469
     * @param callable|null  $default
470
471
     * @return $this|mixed
472
     */
473
    public function when($value, $callback, $default = null) {
474
        if ($value) {
475
            return $callback($this, $value) ?: $this;
476
        } elseif ($default) {
477
            return $default($this, $value) ?: $this;
478
        }
479
480
        return $this;
481
    }
482
483
    /**
484
     * Apply the callback if the given "value" is falsy.
485
     * copied from @url https://github.com/laravel/framework/blob/8.x/src/Illuminate/Support/Traits/Conditionable.php
486
     *
487
     * @param mixed  $value
488
     * @param callable  $callback
489
     * @param callable|null  $default
490
491
     * @return $this|mixed
492
     */
493
    public function unless($value, $callback, $default = null) {
494
        if (! $value) {
495
            return $callback($this, $value) ?: $this;
496
        } elseif ($default) {
497
            return $default($this, $value) ?: $this;
498
        }
499
500
        return $this;
501
    }
502
}