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

DataRequest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 43
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 8 2
A __construct() 0 4 1
A sendLine() 0 10 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\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