Completed
Push — master ( f6a106...f56acc )
by Frederik
01:42
created

TryTlsUpgradeNegotiation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
ccs 5
cts 5
cp 1
cc 1
eloc 7
nc 1
nop 3
crap 1
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
     */
37 6
    public function __construct(
38
        ConnectionInterface $connection,
39
        string $ehlo,
40
        int $crypto
41
    ) {
42 6
        $this->connection = $connection;
43 6
        $this->ehlo = $ehlo;
44 6
        $this->crypto = $crypto;
45 6
    }
46
47
48
    /**
49
     * @param Client $client
50
     * @throws ConnectionInsecureException
51
     */
52 5
    public function negotiate(Client $client): void
53
    {
54 5
        if (!empty($this->connection->getMetaData(['crypto']))) {
55 1
            return;
56
        }
57
58 4
        $reply = $client->request(new EhloCommand($this->ehlo));
59
60 4
        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 2
            $reply->assertCompleted();
64
65
            try {
66
                $client
67 2
                    ->request(new StartTlsCommand())
68 2
                    ->assertCompleted();
69
70 1
                $this->connection->upgrade($this->crypto);
71 2
            } catch (AssertionFailedException $e) {
72
                // apparently HELO OR STARTTLS command is also not implemented
73
                // but failure of STARTTLS is allowed
74
            }
75
        } else {
76 2
            $reply->assertCompleted();
77
78 2
            $ehloResponse = new EhloResponse($reply);
79
80 2
            if ($ehloResponse->isAdvertising('STARTTLS')) {
81
                $client
82 1
                    ->request(new StartTlsCommand())
83 1
                    ->assertCompleted();
84
85 1
                $this->connection->upgrade($this->crypto);
86
            }
87
        }
88 4
    }
89
}
90