Completed
Push — master ( a6178b...331f3d )
by Frederik
02:03
created

DataRequest::sendLine()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 2
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Smtp\Request;
5
6
use Genkgo\Mail\Protocol\ConnectionInterface;
7
use Genkgo\Mail\Protocol\Smtp\RequestInterface;
8
use Genkgo\Mail\StreamInterface;
9
10
final class DataRequest implements RequestInterface
11
{
12
    /**
13
     * @var StreamInterface
14
     */
15
    private $stream;
16
17
    /**
18
     * DataRequest constructor.
19
     * @param StreamInterface $stream
20
     */
21 6
    public function __construct(StreamInterface $stream)
22
    {
23 6
        $this->stream = $stream;
24 6
    }
25
26
    /**
27
     * @param ConnectionInterface $connection
28
     * @return void
29
     */
30 6
    public function execute(ConnectionInterface $connection): void
31
    {
32 6
        $bytes = '';
33
34 6
        while (!$this->stream->eof()) {
35 6
            $bytes .= $this->stream->read(1000);
36
37 6
            $index = 0;
38 6
            while (isset($bytes[$index])) {
39 6
                if ($bytes[$index] === "\r" && isset($bytes[$index+1]) && $bytes[$index+1] === "\n") {
40 3
                    $line = substr($bytes, 0, $index);
41 3
                    $bytes = substr($bytes, $index + 2);
42 3
                    $index = -1;
43
44 3
                    $this->sendLine($connection, $line);
45
                }
46
47 6
                $index++;
48
            }
49
        }
50
51 6
        $this->sendLine($connection, $bytes);
52
53 6
        $connection->send('.');
54 6
    }
55
56
    /**
57
     * @param ConnectionInterface $connection
58
     * @param string $line
59
     */
60 6
    private function sendLine(ConnectionInterface $connection, string $line): void {
61 6
        $line = rtrim($line, "\r");
62
63 6
        if (isset($line[0]) && $line[0] === '.') {
64 1
            $line = '.' . $line;
65
        }
66
67 6
        $connection->send($line);
68 6
    }
69
70
}