Completed
Pull Request — master (#17)
by Frederik
02:00
created

ectionListener.php$0   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 28
c 0
b 0
f 0
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol;
5
6
use Genkgo\Mail\Exception\ConnectionListenerException;
7
8
/**
9
 * Class PlainTcpConnection
10
 * @package Genkgo\Mail\Protocol
11
 * @codeCoverageIgnore
12
 */
13
final class PlainTcpConnectionListener implements ConnectionListenerInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $host;
19
    /**
20
     * @var int
21
     */
22
    private $port;
23
    /**
24
     * @var resource
25
     */
26
    private $resource;
27
    /**
28
     * @var float
29
     */
30
    private $timeout;
31
32
    /**
33
     * PlainTcpConnection constructor.
34
     * @param string $host
35
     * @param int $port
36
     * @param float $timeout
37
     */
38
    public function __construct(string $host, int $port, float $timeout = -1)
39
    {
40
        $this->host = $host;
41
        $this->port = $port;
42
        $this->timeout = $timeout;
0 ignored issues
show
Documentation Bug introduced by
It seems like $timeout can also be of type integer. However, the property $timeout 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...
43
    }
44
45
    /**
46
     * @return ConnectionInterface
47
     * @throws ConnectionListenerException
48
     */
49
    public function listen(): ConnectionInterface
50
    {
51
        $this->validateResource();
52
53
        $resource = @stream_socket_accept($this->resource, $this->timeout);
54
        if (is_resource($resource)) {
55
            return new class($resource) extends AbstractConnection {
56
57
                /**
58
                 * @param $resource
59
                 */
60
                public function __construct($resource)
61
                {
62
                    $this->resource = $resource;
63
                }
64
65
                /**
66
                 *
67
                 */
68
                public function connect(): void
69
                {
70
                    $this->fireEvent('connect');
71
                }
72
73
                /**
74
                 * @param int $type
75
                 */
76
                public function upgrade(int $type): void
77
                {
78
                    if (stream_socket_enable_crypto($this->resource, true, $type) === false) {
79
                        throw new \InvalidArgumentException('Cannot upgrade connection to requested encryption type');
80
                    }
81
                }
82
            };
83
        }
84
85
        throw new ConnectionListenerException('Could not accept connection');
86
    }
87
88
    /**
89
     *
90
     */
91
    private function validateResource(): void
92
    {
93
        if ($this->resource === null) {
94
            $this->resource = @stream_socket_server(
95
                'tcp://' . $this->host . ':' . $this->port,
96
                $errorCode,
97
                $errorMessage
98
            );
99
        }
100
    }
101
}