Mailgun::sandbox()   B
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
c 2
b 0
f 0
dl 0
loc 33
ccs 18
cts 18
cp 1
rs 8.0555
cc 9
nc 5
nop 2
crap 9
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\Mailgun as MailgunTest;
26
use function explode;
27
use function filter_var;
28
use function str_replace;
29
30
/**
31
 * The sandbox for Sendgrid.
32
 */
33
class Mailgun
34
{
35
    /**
36
     * Invoker.
37
     *
38
     * @param array $args The arguments.
39
     *
40
     * @return bool
41
     */
42 1
    public function __invoke(array $args): bool
43
    {
44 1
        return $this->sandbox($args[0], $args[1]);
45
    }
46
47
    /**
48
     * Test Mailgun.
49
     *
50
     * @param array $getParams The GET params passed from tryMessenger method.
51
     * @param array $message   The message title and body.
52
     *
53
     * @return bool
54
     */
55 1
    private function sandbox($getParams, $message)
56
    {
57 1
        $apiKey = $getParams['apiKey'] ?? '';
58 1
        $domain = $getParams['domain'] ?? '';
59 1
        $sender = $getParams['sender'] ?? '';
60 1
        $recipients = $getParams['recipients'] ?? '';
61
62 1
        if (!empty($sender) && !empty($recipients) && !empty($apiKey) && !empty($domain)) {
63 1
            $recipients = str_replace("\r", '|', $recipients);
64 1
            $recipients = str_replace("\n", '|', $recipients);
65 1
            $recipients = explode('|', $recipients);
66
67 1
            $messenger = new MailgunTest($apiKey, $domain);
68
69 1
            foreach ($recipients as $recipient) {
70 1
                if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
71 1
                    $messenger->addRecipient($recipient);
72
                }
73
            }
74
75 1
            if (filter_var($sender, FILTER_VALIDATE_EMAIL)) {
76 1
                $messenger->addSender($sender);
77
            }
78
79 1
            $messenger->setSubject($message['title']);
80
81 1
            if ($messenger->send($message['body'])) {
82
                // @codeCoverageIgnoreStart
83
                return true;
84
                // @codeCoverageIgnoreEnd
85
            }
86
        }
87 1
        return false;
88
    }
89
}
90