Completed
Push — master ( bd2b4b...e19a0a )
by Frederik
03:38 queued 01:06
created

TryTlsUpgradeNegotiation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 55
c 0
b 0
f 0
ccs 18
cts 19
cp 0.9474
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A negotiate() 0 26 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\Negotiation;
5
6
use Genkgo\Mail\Exception\ConnectionInsecureException;
7
use Genkgo\Mail\Exception\SecureConnectionUpgradeException;
8
use Genkgo\Mail\Protocol\ConnectionInterface;
9
use Genkgo\Mail\Protocol\Imap\Client;
10
use Genkgo\Mail\Protocol\Imap\NegotiationInterface;
11
use Genkgo\Mail\Protocol\Imap\Request\CapabilityCommand;
12
use Genkgo\Mail\Protocol\Imap\Request\StartTlsCommand;
13
use Genkgo\Mail\Protocol\Imap\Response\Command\CapabilityCommandResponse;
14
use Genkgo\Mail\Protocol\Imap\Response\CompletionResult;
15
16
final class TryTlsUpgradeNegotiation implements NegotiationInterface
17
{
18
    /**
19
     * @var ConnectionInterface
20
     */
21
    private $connection;
22
23
    /**
24
     * @var int
25
     */
26
    private $crypto;
27
28
    /**
29
     * @param ConnectionInterface $connection
30
     * @param int $crypto
31
     */
32 4
    public function __construct(
33
        ConnectionInterface $connection,
34
        int $crypto
35
    ) {
36 4
        $this->connection = $connection;
37 4
        $this->crypto = $crypto;
38 4
    }
39
40
    /**
41
     * @param Client $client
42
     * @throws ConnectionInsecureException
43
     */
44 3
    public function negotiate(Client $client): void
45
    {
46 3
        if (empty($this->connection->getMetaData(['crypto']))) {
47 2
            $responseList = $client->emit(new CapabilityCommand($client->newTag()));
48
49 2
            $capabilities = CapabilityCommandResponse::fromString($responseList->first()->getBody());
50
51
            $responseList
52 2
                ->last()
53 2
                ->assertCompletion(CompletionResult::ok())
54 2
                ->assertTagged();
55
56 2
            if ($capabilities->isAdvertising('STARTTLS')) {
57
                $client
58 1
                    ->emit(new StartTlsCommand($client->newTag()))
59 1
                    ->last()
60 1
                    ->assertCompletion(CompletionResult::ok())
61 1
                    ->assertTagged();
62
63
                try {
64 1
                    $this->connection->upgrade($this->crypto);
65
                } catch (SecureConnectionUpgradeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
66
                }
67
            }
68
        }
69 3
    }
70
}
71