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

DataRequest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 61
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B execute() 0 25 6
A sendLine() 0 9 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
}