Completed
Push — master ( d73f1a...00b534 )
by Frederik
02:19
created

SecureConnection::connect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol;
5
6
use Genkgo\Mail\Exception\ConnectionRefusedException;
7
8
/**
9
 * @codeCoverageIgnore
10
 */
11
final class SecureConnection extends AbstractConnection
12
{
13
    /**
14
     * @var string
15
     */
16
    private $host;
17
18
    /**
19
     * @var int
20
     */
21
    private $port;
22
23
    /**
24
     * @var SecureConnectionOptions
25
     */
26
    private $options;
27
28
    /**
29
     * @param string $host
30
     * @param int $port
31
     * @param SecureConnectionOptions $options
32
     */
33
    public function __construct(string $host, int $port, SecureConnectionOptions $options)
34
    {
35
        $this->host = $host;
36
        $this->port = $port;
37
        $this->options = $options;
38
    }
39
40
    /**
41
     * @param int $type
42
     */
43
    public function upgrade(int $type): void
44
    {
45
        throw new \InvalidArgumentException('Cannot upgrade connection, already secure');
46
    }
47
    
48
    public function connect(): void
49
    {
50
        $context = $this->options->createContext();
51
52
        $resource = @\stream_socket_client(
53
            'tls://' . $this->host . ':' . $this->port,
54
            $errorCode,
55
            $errorMessage,
56
            $this->options->getTimeout(),
57
            STREAM_CLIENT_CONNECT,
58
            $context
59
        );
60
61
        if ($resource === false) {
62
            throw new ConnectionRefusedException(
63
                \sprintf('Could not create secure connection. %s.', $errorMessage),
64
                $errorCode
65
            );
66
        }
67
68
        $this->resource = $resource;
69
        $this->fireEvent('connect');
70
    }
71
}
72