SMTPConnectCheck   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 20 3
A __construct() 0 13 5
1
<?php
2
3
namespace SilverStripe\EnvironmentCheck\Checks;
4
5
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
6
7
/**
8
 * Checks if the SMTP connection configured through PHP.ini works as expected.
9
 *
10
 * Only checks socket connection with HELO command, not actually sending the email.
11
 *
12
 * @package environmentcheck
13
 */
14
class SMTPConnectCheck implements EnvironmentCheck
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $host;
20
21
    /**
22
     * @var int
23
     */
24
    protected $port;
25
26
    /**
27
     * Timeout (in seconds).
28
     *
29
     * @var int
30
     */
31
    protected $timeout;
32
33
    /**
34
     * @param null|string $host
35
     * @param null|int $port
36
     * @param int $timeout
37
     */
38
    public function __construct($host = null, $port = null, $timeout = 15)
39
    {
40
        $this->host = ($host) ? $host : ini_get('SMTP');
41
        if (!$this->host) {
42
            $this->host = 'localhost';
43
        }
44
45
        $this->port = ($port) ? $port : ini_get('smtp_port');
0 ignored issues
show
Documentation Bug introduced by
It seems like $port ? $port : ini_get('smtp_port') can also be of type string. However, the property $port is declared as type integer. 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...
46
        if (!$this->port) {
47
            $this->port = 25;
48
        }
49
50
        $this->timeout = $timeout;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     *
56
     * @return array
57
     */
58
    public function check()
59
    {
60
        $f = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
61
        if (!$f) {
0 ignored issues
show
introduced by
$f is of type false|resource, thus it always evaluated to false.
Loading history...
62
            return [
63
                EnvironmentCheck::ERROR,
64
                sprintf("Couldn't connect to SMTP on %s:%s (Error: %s %s)", $this->host, $this->port, $errno, $errstr)
65
            ];
66
        }
67
68
        fwrite($f, "HELO its_me\r\n");
69
        $response = fread($f, 26);
70
        if (substr($response, 0, 3) != '220') {
71
            return [
72
                EnvironmentCheck::ERROR,
73
                sprintf('Invalid mail server response: %s', $response)
74
            ];
75
        }
76
77
        return [EnvironmentCheck::OK, ''];
78
    }
79
}
80