Completed
Pull Request — master (#42)
by Frederik
02:22
created

SearchCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 58
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A createStream() 0 10 2
A getTag() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\Request;
5
6
use Genkgo\Mail\Protocol\Imap\Request\SearchCriteria\Query;
7
use Genkgo\Mail\Protocol\Imap\Tag;
8
use Genkgo\Mail\Stream\StringStream;
9
use Genkgo\Mail\StreamInterface;
10
11
/**
12
 * Class SearchCommand
13
 * @package Genkgo\Mail\Protocol\Imap\Request
14
 */
15
final class SearchCommand extends AbstractCommand
16
{
17
    /**
18
     *
19
     */
20
    private CONST CHARSET_VALID = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+.:_";
21
    /**
22
     * @var Tag
23
     */
24
    private $tag;
25
    /**
26
     * @var Query
27
     */
28
    private $query;
29
    /**
30
     * @var string
31
     */
32
    private $charset;
33
34
    /**
35
     * LogoutCommand constructor.
36
     * @param Tag $tag
37
     * @param Query $query
38
     * @param string $charset
39
     */
40
    public function __construct(Tag $tag, Query $query, string $charset = '')
41
    {
42
        if (strlen($this->charset) !== strcspn($charset, self::CHARSET_VALID)) {
43
            throw new \InvalidArgumentException("Invalid charset");
44
        }
45
46
        $this->tag = $tag;
47
        $this->query = $query;
48
        $this->charset = $charset;
49
    }
50
51
    /**
52
     * @return StreamInterface
53
     */
54
    protected function createStream(): StreamInterface
55
    {
56
        return new StringStream(
57
            sprintf(
58
                'SEARCH %s%s',
59
                $this->charset ? 'CHARSET ' . $this->charset . ' ' : '',
60
                (string)$this->query
61
            )
62
        );
63
    }
64
65
    /**
66
     * @return Tag
67
     */
68
    public function getTag(): Tag
69
    {
70
        return $this->tag;
71
    }
72
}