Completed
Push — master ( 4edb1e...e1389e )
by Frederik
02:12
created

TryTlsUpgradeNegotiation   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 54
c 0
b 0
f 0
rs 10
ccs 15
cts 15
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A negotiate() 0 17 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Smtp\Negotiation;
5
6
use Genkgo\Mail\Exception\ConnectionInsecureException;
7
use Genkgo\Mail\Protocol\ConnectionInterface;
8
use Genkgo\Mail\Protocol\CryptoConstant;
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\StartTlsCommand;
13
use Genkgo\Mail\Protocol\Smtp\Response\EhloResponse;
14
15
final class TryTlsUpgradeNegotiation implements NegotiationInterface
16
{
17
    /**
18
     * @var ConnectionInterface
19
     */
20
    private $connection;
21
    /**
22
     * @var string
23
     */
24
    private $ehlo;
25
    /**
26
     * @var int
27
     */
28
    private $crypto;
29
30
    /**
31
     * ConnectionNegotiation constructor.
32
     * @param ConnectionInterface $connection
33
     * @param string $ehlo
34
     * @param int $crypto
35
     */
36 3
    public function __construct(
37
        ConnectionInterface $connection,
38
        string $ehlo,
39
        int $crypto = CryptoConstant::TYPE_SECURE
40
    ) {
41 3
        $this->connection = $connection;
42 3
        $this->ehlo = $ehlo;
43 3
        $this->crypto = $crypto;
44 3
    }
45
46
47
    /**
48
     * @param Client $client
49
     * @throws ConnectionInsecureException
50
     */
51 2
    public function negotiate(Client $client): void
52
    {
53 2
        if (empty($this->connection->getMetaData(['crypto']))) {
54 2
            $reply = $client->request(new EhloCommand($this->ehlo));
55 2
            $reply->assertCompleted();
56
57 2
            $ehloResponse = new EhloResponse($reply);
58
59 2
            if ($ehloResponse->isAdvertising('STARTTLS')) {
60
                $client
61 1
                    ->request(new StartTlsCommand())
62 1
                    ->assertCompleted();
63
64 1
                $this->connection->upgrade($this->crypto);
65
            }
66
        }
67
    }
68
}