SearchCommand::createStream()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 1
nop 0
crap 2
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
final class SearchCommand extends AbstractCommand
12
{
13
    private const CHARSET_VALID = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+.:_";
14
15
    /**
16
     * @var Tag
17
     */
18
    private $tag;
19
20
    /**
21
     * @var Query
22
     */
23
    private $query;
24
25
    /**
26
     * @var string
27
     */
28
    private $charset;
29
30
    /**
31
     * @param Tag $tag
32
     * @param Query $query
33
     * @param string $charset
34
     */
35 3
    public function __construct(Tag $tag, Query $query, string $charset = '')
36
    {
37 3
        if (\strlen($charset) !== \strspn($charset, self::CHARSET_VALID)) {
38 1
            throw new \InvalidArgumentException("Invalid charset");
39
        }
40
41 2
        $this->tag = $tag;
42 2
        $this->query = $query;
43 2
        $this->charset = $charset;
44 2
    }
45
46
    /**
47
     * @return StreamInterface
48
     */
49 2
    protected function createStream(): StreamInterface
50
    {
51 2
        return new StringStream(
52 2
            \sprintf(
53 2
                'SEARCH %s%s',
54 2
                $this->charset ? 'CHARSET ' . $this->charset . ' ' : '',
55 2
                (string)$this->query
56
            )
57
        );
58
    }
59
60
    /**
61
     * @return Tag
62
     */
63 2
    public function getTag(): Tag
64
    {
65 2
        return $this->tag;
66
    }
67
}
68