Completed
Pull Request — master (#42)
by Frederik
03:52
created

TryTlsUpgradeNegotiation::negotiate()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
dl 0
loc 37
c 0
b 0
f 0
ccs 10
cts 11
cp 0.9091
rs 8.439
cc 5
eloc 21
nc 6
nop 1
crap 5.0187
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Smtp\Negotiation;
5
6
use Genkgo\Mail\Exception\AssertionFailedException;
7
use Genkgo\Mail\Exception\ConnectionInsecureException;
8
use Genkgo\Mail\Protocol\ConnectionInterface;
9
use Genkgo\Mail\Protocol\Smtp\Client;
10
use Genkgo\Mail\Protocol\Smtp\NegotiationInterface;
11
use Genkgo\Mail\Protocol\Smtp\Request\EhloCommand;
12
use Genkgo\Mail\Protocol\Smtp\Request\HeloCommand;
13
use Genkgo\Mail\Protocol\Smtp\Request\StartTlsCommand;
14
use Genkgo\Mail\Protocol\Smtp\Response\EhloResponse;
15
16
final class TryTlsUpgradeNegotiation implements NegotiationInterface
17
{
18
    /**
19
     * @var ConnectionInterface
20
     */
21
    private $connection;
22
    /**
23
     * @var string
24
     */
25
    private $ehlo;
26
    /**
27
     * @var int
28
     */
29
    private $crypto;
30
31
    /**
32
     * ConnectionNegotiation constructor.
33
     * @param ConnectionInterface $connection
34
     * @param string $ehlo
35
     * @param int $crypto
36 3
     */
37
    public function __construct(
38
        ConnectionInterface $connection,
39
        string $ehlo,
40
        int $crypto
41 3
    ) {
42 3
        $this->connection = $connection;
43 3
        $this->ehlo = $ehlo;
44 3
        $this->crypto = $crypto;
45
    }
46
47
48
    /**
49
     * @param Client $client
50
     * @throws ConnectionInsecureException
51 2
     */
52
    public function negotiate(Client $client): void
53 2
    {
54 2
        if (!empty($this->connection->getMetaData(['crypto']))) {
55 2
            return;
56
        }
57
58 2
        $reply = $client->request(new EhloCommand($this->ehlo));
59
60 2
        if ($reply->isCommandNotImplemented()) {
61
            // since EHLO is not implemented, let's try HELO and then STARTTLS
62 2
            $reply = $client->request(new HeloCommand($this->ehlo));
63
            $reply->assertCompleted();
64 1
65 1
            try {
66
                $client
67 1
                    ->request(new StartTlsCommand())
68
                    ->assertCompleted();
69
70 2
                $this->connection->upgrade($this->crypto);
71
            } catch (AssertionFailedException $e) {
72
                // apparently HELO OR STARTTLS command is also not implemented
73
                // but failure of STARTTLS is allowed
74
            }
75
        } else {
76
            $reply->assertCompleted();
77
78
            $ehloResponse = new EhloResponse($reply);
79
80
            if ($ehloResponse->isAdvertising('STARTTLS')) {
81
                $client
82
                    ->request(new StartTlsCommand())
83
                    ->assertCompleted();
84
85
                $this->connection->upgrade($this->crypto);
86
            }
87
        }
88
    }
89
}
90