Completed
Pull Request — master (#48)
by Frederik
02:12
created

DataRequest::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
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\Stream\LineIterator;
9
use Genkgo\Mail\StreamInterface;
10
11
final class DataRequest implements RequestInterface
12
{
13
    /**
14
     * @var StreamInterface
15
     */
16
    private $stream;
17
18
    /**
19
     * @param StreamInterface $stream
20
     */
21 7
    public function __construct(StreamInterface $stream)
22
    {
23 7
        $this->stream = $stream;
24 7
    }
25
26
    /**
27
     * @param ConnectionInterface $connection
28
     * @return void
29
     */
30 7
    public function execute(ConnectionInterface $connection): void
31
    {
32 7
        foreach (new LineIterator($this->stream) as $line) {
33 7
            $this->sendLine($connection, $line);
34
        }
35
36 7
        $connection->send('.');
37 7
    }
38
39
    /**
40
     * @param ConnectionInterface $connection
41
     * @param string $line
42
     */
43 7
    private function sendLine(ConnectionInterface $connection, string $line): void
44
    {
45 7
        $line = \rtrim($line, "\r");
46
47 7
        if (isset($line[0]) && $line[0] === '.') {
48 1
            $line = '.' . $line;
49
        }
50
51 7
        $connection->send($line);
52 7
    }
53
}
54