Passed
Push — 2.x ( 92ec38...c080f0 )
by Terry
02:07
created

Smtp::sandbox()   C

Complexity

Conditions 14
Paths 11

Size

Total Lines 54
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 34
c 1
b 0
f 0
nc 11
nop 2
dl 0
loc 54
rs 6.2666

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 as Messenger;
16
17
/**
18
 * The sandbox for SMTP.
19
 */
20
class Smtp
21
{
22
    /**
23
     * Invoker.
24
     *
25
     * @param array $args
26
     *
27
     * @return bool
28
     */
29
    public function __invoke(array $args): bool
30
    {
31
        return $this->sandbox($args[0], $args[1]);
32
    }
33
34
    /**
35
     * Test SMTP.
36
     *
37
     * @param array $getParams The GET params passed from tryMessenger method.
38
     * @param array $message   The message title and body.
39
     *
40
     * @return bool
41
     */
42
    private function sandbox($getParams, $message)
43
    {
44
        $type = $getParams['type'] ?? '';
45
        $host = $getParams['host'] ?? '';
46
        $user = $getParams['user'] ?? '';
47
        $pass = $getParams['pass'] ?? '';
48
        $port = $getParams['port'] ?? '';
49
        $data = [];
50
51
        $sender = $getParams['sender'] ?? '';
52
        $recipients = $getParams['recipients'] ?? '';
53
54
        if (
55
            (
56
                !filter_var($host, FILTER_VALIDATE_IP) && 
57
                !filter_var($host, FILTER_VALIDATE_DOMAIN)
58
            ) || 
59
            !is_numeric($port) || 
60
            empty($user) || 
61
            empty($pass) 
62
        ) {
63
            $data['result']['message'] = 'Invalid fields.';
64
            $output = json_encode($data);
65
            return $this->respondJson($output);
0 ignored issues
show
Bug introduced by
The method respondJson() does not exist on Shieldon\Firewall\Panel\Sandbox\Smtp. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            return $this->/** @scrutinizer ignore-call */ respondJson($output);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        }
67
68
        if ('ssl' === $type || 'tls' === $type) {
69
            $host = $type . '://' . $host;
70
        }
71
72
        if (!empty($sender) && $recipients) {
73
            $recipients = str_replace("\r", '|', $recipients);
74
            $recipients = str_replace("\n", '|', $recipients);
75
            $recipients = explode('|', $recipients);
76
77
            $messenger = new Messenger\Smtp($user, $pass, $host, (int) $port);
78
79
            foreach($recipients as $recipient) {
80
                if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
81
                    $messenger->addRecipient($recipient);
82
                }
83
            }
84
85
            if (filter_var($sender, FILTER_VALIDATE_EMAIL)) {
86
                $messenger->addSender($sender);
87
            }
88
89
            $messenger->setSubject($message['title']);
90
91
            if ($messenger->send($message['body'])) {
92
                return true;
93
            }
94
        }
95
        return false;
96
    }
97
}