Passed
Push — 2.x ( 4d98bd...e7390d )
by Terry
02:11
created

Smtp::sandbox()   B

Complexity

Conditions 10
Paths 51

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 10
eloc 29
c 3
b 0
f 0
nc 51
nop 2
dl 0
loc 51
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Panel\Sandbox;
14
15
use Shieldon\Messenger\Smtp as SmtpTest;
16
use function explode;
17
use function filter_var;
18
use function str_replace;
19
20
21
/**
22
 * The sandbox for SMTP.
23
 */
24
class Smtp
25
{
26
    /**
27
     * Invoker.
28
     *
29
     * @param array $args
30
     *
31
     * @return bool
32
     */
33
    public function __invoke(array $args): bool
34
    {
35
        return $this->sandbox($args[0], $args[1]);
36
    }
37
38
    /**
39
     * Test SMTP.
40
     *
41
     * @param array $getParams The GET params passed from tryMessenger method.
42
     * @param array $message   The message title and body.
43
     *
44
     * @return bool
45
     */
46
    private function sandbox($getParams, $message)
47
    {
48
        $params = [
49
            'type',
50
            'host',
51
            'user',
52
            'pass',
53
            'port',
54
            'sender',
55
            'recipients',
56
        ];
57
58
        foreach ($params as $param) {
59
            ${$param} = $getParams[$param] ?? '';
60
61
            if (empty(${$param})) {
62
                return false;
63
            }
64
        }
65
66
        if (!$this->checkHost($host)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $host seems to be never defined.
Loading history...
67
            return false;
68
        }
69
70
        if ('ssl' === $type || 'tls' === $type) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $type seems to be never defined.
Loading history...
71
            $host = $type . '://' . $host;
72
        }
73
74
        $recipients = str_replace("\r", '|', $recipients);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $recipients seems to be never defined.
Loading history...
75
        $recipients = str_replace("\n", '|', $recipients);
76
        $recipients = explode('|', $recipients);
77
78
        $messenger = new SmtpTest($user, $pass, $host, (int) $port);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $host does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $port seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $pass seems to be never defined.
Loading history...
79
80
        foreach ($recipients as $recipient) {
81
            if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
82
                $messenger->addRecipient($recipient);
83
            }
84
        }
85
86
        if (filter_var($sender, FILTER_VALIDATE_EMAIL)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sender seems to be never defined.
Loading history...
87
            $messenger->addSender($sender);
88
        }
89
90
        $messenger->setSubject($message['title']);
91
92
        if ($messenger->send($message['body'])) {
93
            return true;
94
        }
95
        
96
        return false;
97
    }
98
99
    /**
100
     * Check the SMTP host.
101
     *
102
     * @param string $host The IP address or server domain name.
103
     *
104
     * @return bool
105
     */
106
    private function checkHost(string $host): bool
107
    {
108
        if (
109
            !filter_var($host, FILTER_VALIDATE_IP) && 
110
            !filter_var($host, FILTER_VALIDATE_DOMAIN)
111
        ) {
112
            return false;
113
        }
114
        return true;
115
    }
116
}