Completed
Push — master ( e1389e...1f6470 )
by Frederik
02:10
created

SecureConnection::connect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
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
 * Class SecureConnection
10
 * @package Genkgo\Mail\Protocol
11
 * @codeCoverageIgnore
12
 */
13
final class SecureConnection extends AbstractConnection
14
{
15
    /**
16
     * @var string
17
     */
18
    private $protocol;
19
    /**
20
     * @var string
21
     */
22
    private $host;
23
    /**
24
     * @var int
25
     */
26
    private $port;
27
    /**
28
     * @var SecureConnectionOptions
29
     */
30
    private $options;
31
32
    /**
33
     * PlainTcpConnection constructor.
34
     * @param string $protocol
35
     * @param string $host
36
     * @param int $port
37
     * @param SecureConnectionOptions $options
38
     */
39
    public function __construct(string $protocol, string $host, int $port, SecureConnectionOptions $options)
40
    {
41
        $this->protocol = $protocol;
42
        $this->host = $host;
43
        $this->port = $port;
44
        $this->options = $options;
45
    }
46
47
    /**
48
     * @param int $type
49
     */
50
    public function upgrade(int $type): void
51
    {
52
        throw new \InvalidArgumentException('Cannot connection, already secure');
53
    }
54
55
    /**
56
     *
57
     */
58
    public function connect(): void
59
    {
60
        $resource = @stream_socket_client(
61
            $this->protocol . $this->host . ':' . $this->port,
62
            $errorCode,
63
            $errorMessage,
64
            $this->options->getTimeout()
65
        );
66
67
        if ($resource === false) {
68
            throw new ConnectionRefusedException(
69
                sprintf('Could not create secure connection. %s.', $errorMessage),
70
                $errorCode
71
            );
72
        }
73
74
        $this->resource = $resource;
75
        $this->fireEvent('connect');
76
    }
77
}