Passed
Pull Request — master (#194)
by
unknown
02:29
created

SmtpSocketHelper::isResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Egulias\EmailValidator\Helper;
4
5
class SmtpSocketHelper
6
{
7
    /**
8
     * @var int
9
     */
10
    private $port;
11
12
    /**
13
     * @var int
14
     */
15
    private $timeout;
16
17
    /**
18
     * @var resource
19
     */
20
    private $handle;
21
22 1
    public function __construct($port = 25, $timeout = 15)
23
    {
24 1
        $this->port = $port;
25 1
        $this->timeout = $timeout;
26 1
    }
27
28
    /**
29
     * Checks is resource
30
     *
31
     * @return bool
32
     */
33
    public function isResource()
34
    {
35
        return is_resource($this->handle);
36
    }
37
38
    /**
39
     * Opens resource
40
     *
41
     * @param string $hostname
42
     * @param int $errno
43
     * @param string $errstr
44
     */
45
    public function open($hostname, &$errno, &$errstr)
46
    {
47
        $this->handle = @fsockopen($hostname, $this->port, $errno, $errstr, $this->timeout);
48
    }
49
50
    /**
51
     * Writes message
52
     *
53
     * @param string $message
54
     *
55
     * @return bool|int
56
     */
57
    public function write($message)
58
    {
59
        if (!$this->isResource()) {
60
            return false;
61
        }
62
63
        return @fwrite($this->handle, $message);
64
    }
65
66
    /**
67
     * Get last response code
68
     *
69
     * @return int
70
     */
71
    public function getResponseCode()
72
    {
73
        if (!$this->isResource()) {
74
            return -1;
75
        }
76
77
        $data = '';
78
        while (substr($data, 3, 1) !== ' ') {
79
            if (!($data = @fgets($this->handle, 256))) {
80
                return -1;
81
            }
82
        }
83
84
        return intval(substr($data, 0, 3));
85
    }
86
87
    /**
88
     * Closes resource
89
     */
90
    public function close()
91
    {
92
        if (!$this->isResource()) {
93
            return;
94
        }
95
96
        @fclose($this->handle);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
97
98
        $this->handle = null;
99
    }
100
}