Completed
Push — master ( 3ff097...503b9d )
by Frederik
06:54
created

PlainTcpConnection::upgrade()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol;
5
6
/**
7
 * Class PlainTcpConnection
8
 * @package Genkgo\Mail\Protocol
9
 * @codeCoverageIgnore
10
 */
11
final class PlainTcpConnection extends AbstractConnection
12
{
13
    /**
14
     *
15
     */
16
    private const UPGRADE_TO = [
17
        STREAM_CRYPTO_METHOD_TLS_CLIENT => true,
18
        STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT => true,
19
        STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT => true,
20
        STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT => true,
21
    ];
22
23
    /**
24
     * @var string
25
     */
26
    private $host;
27
    /**
28
     * @var int
29
     */
30
    private $port;
31
    /**
32
     * @var float
33
     */
34
    private $connectionTimeout;
35
36
    /**
37
     * PlainTcpConnection constructor.
38
     * @param string $host
39
     * @param int $port
40
     * @param float $connectionTimeout
41
     */
42
    public function __construct(string $host, int $port, float $connectionTimeout = 1)
43
    {
44
        $this->host = $host;
45
        $this->port = $port;
46
        $this->connectionTimeout = $connectionTimeout;
0 ignored issues
show
Documentation Bug introduced by
It seems like $connectionTimeout can also be of type integer. However, the property $connectionTimeout is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47
    }
48
49
    /**
50
     * @param int $type
51
     */
52
    public function upgrade(int $type): void
53
    {
54
        if (!isset(self::UPGRADE_TO[$type])) {
55
            throw new \InvalidArgumentException('No support for requested encryption type');
56
        }
57
58
        $resource = stream_socket_enable_crypto($this->resource, true, $type);
59
        if ($resource === false) {
60
            throw new \InvalidArgumentException('Cannot upgrade connection to requested encryption type');
61
        }
62
63
        $this->resource = $resource;
64
    }
65
66
    /**
67
     *
68
     */
69
    public function connect(): void
70
    {
71
        $this->resource = @stream_socket_client(
72
            'tcp://' . $this->host . ':' . $this->port,
73
            $errorCode,
74
            $errorMessage,
75
            $this->connectionTimeout
76
        );
77
78
        if ($this->resource === false) {
79
            throw new \RuntimeException(sprintf('Could not create resource: %s', $errorMessage), $errorCode);
80
        }
81
    }
82
}