NativePhpMail::sandbox()   B
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 33
ccs 15
cts 15
cp 1
rs 8.4444
cc 8
nc 5
nop 2
crap 8
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
 * php version 7.1.0
11
 *
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Panel\Sandbox;
24
25
use Shieldon\Messenger\Mail as MailTest;
26
use function explode;
27
use function filter_var;
28
use function function_exists;
29
use function str_replace;
30
31
/**
32
 * The sandbox for PHP native mail.
33
 */
34
class NativePhpMail
35
{
36
    /**
37
     * Invoker.
38
     *
39
     * @param array $args The arguments.
40
     *
41
     * @return bool
42
     */
43 1
    public function __invoke(array $args): bool
44
    {
45 1
        return $this->sandbox($args[0], $args[1]);
46
    }
47
48
    /**
49
     * Test PHP native mail.
50
     *
51
     * @param array $getParams The GET params passed from tryMessenger method.
52
     * @param array $message   The message title and body.
53
     *
54
     * @return bool
55
     */
56 1
    private function sandbox($getParams, $message)
57
    {
58 1
        $sender = $getParams['sender'] ?? '';
59 1
        $recipients = $getParams['recipients'] ?? '';
60
61 1
        if (!empty($sender) && !empty($recipients)) {
62 1
            $recipients = str_replace("\r", '|', $recipients);
63 1
            $recipients = str_replace("\n", '|', $recipients);
64 1
            $recipients = explode('|', $recipients);
65
66 1
            $messenger = new MailTest();
67
68 1
            foreach ($recipients as $recipient) {
69 1
                if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
70 1
                    $messenger->addRecipient($recipient);
71
                }
72
            }
73
74 1
            if (filter_var($sender, FILTER_VALIDATE_EMAIL)) {
75 1
                $messenger->addSender($sender);
76
            }
77
78 1
            $messenger->setSubject($message['title']);
79
80 1
            if (!defined('PHP_UNIT_TEST') && $messenger->send($message['body'])) {
81
                // @codeCoverageIgnoreStart
82
                return true;
83
                // @codeCoverageIgnoreEnd
84
            }
85
        }
86
87
        // @codeCoverageIgnoreStart
88
        return false;
89
        // @codeCoverageIgnoreEnd
90
    }
91
}
92