Completed
Push — master ( 5840b0...fcc03f )
by Frederik
04:51
created

EhloCapability   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 80
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B manifest() 0 31 3
A advertise() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Smtp\Capability;
5
6
use Genkgo\Mail\Protocol\ConnectionInterface;
7
use Genkgo\Mail\Protocol\Smtp\CapabilityInterface;
8
use Genkgo\Mail\Protocol\Smtp\Session;
9
10
final class EhloCapability implements CapabilityInterface
11
{
12
13
    /**
14
     * @var array
15
     */
16
    private $capabilities;
17
    /**
18
     * @var string
19
     */
20
    private $serverName;
21
    /**
22
     *
23
     */
24
    private CONST PROTOCOL_COMMANDS = [
25
        'MAIL FROM' => true,
26
        'EHLO' => true,
27
        'RCPT TO' => true,
28
        'RSET' => true,
29
        'QUIT' => true,
30
        'HELO' => true,
31
        'DATA' => true,
32
    ];
33
34
    /**
35
     * EhloCapability constructor.
36
     * @param string $serverName
37
     * @param array $capabilities
38
     */
39 5
    public function __construct(string $serverName, array $capabilities)
40
    {
41 5
        $this->serverName = $serverName;
42 5
        $this->capabilities = $capabilities;
43 5
    }
44
45
    /**
46
     * @param ConnectionInterface $connection
47
     * @param Session $session
48
     * @return Session
49
     */
50 2
    public function manifest(ConnectionInterface $connection, Session $session): Session
51
    {
52 2
        $command = $session->getCommand();
53
54 2
        $advertisements = array_filter(
55 2
            array_map(
56 2
                function (CapabilityInterface $capability) {
57 1
                    return $capability->advertise();
58 2
                },
59 2
                $this->capabilities
60
            ),
61 2
            function (string $advertisement) {
62 1
                return !isset(self::PROTOCOL_COMMANDS[$advertisement]);
63 2
            }
64
        );
65
66 2
        $messages = array_merge(
67 2
            [$this->serverName . ' Hello ' . substr($command, 5)],
68 2
            $advertisements
69
        );
70
71 2
        foreach ($messages as $message) {
72 2
            if (next($messages) === false) {
73 2
                $connection->send('250 ' . $message);
74
            } else {
75 2
                $connection->send('250-' . $message);
76
            }
77
        }
78
79 2
        return $session->withState(Session::STATE_EHLO);
80
    }
81
82
    /**
83
     * @return string
84
     */
85 3
    public function advertise(): string
86
    {
87 3
        return 'EHLO';
88
    }
89
}