Completed
Pull Request — master (#48)
by Frederik
05:48
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 7
     */
21
    public function __construct(StreamInterface $stream)
22 7
    {
23 7
        $this->stream = $stream;
24
    }
25
26
    /**
27
     * @param ConnectionInterface $connection
28
     * @return void
29 7
     */
30
    public function execute(ConnectionInterface $connection): void
31 7
    {
32
        foreach (new LineIterator($this->stream) as $line) {
33 7
            $this->sendLine($connection, $line);
34 7
        }
35 7
36
        $connection->send('.');
37 7
    }
38 7
39 7
    /**
40 3
     * @param ConnectionInterface $connection
41 3
     * @param string $line
42 3
     */
43
    private function sendLine(ConnectionInterface $connection, string $line): void
44 3
    {
45
        $line = \rtrim($line, "\r");
46
47 7
        if (isset($line[0]) && $line[0] === '.') {
48
            $line = '.' . $line;
49
        }
50
51 7
        $connection->send($line);
52
    }
53
}
54