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

Mailgun   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 3 1
B sandbox() 0 31 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
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 Sendgrid.
19
 */
20
class Mailgun
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 Mailgun.
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
        $apiKey = $getParams['apiKey'] ?? '';
45
        $domain = $getParams['domain'] ?? '';
46
        $sender = $getParams['sender'] ?? '';
47
        $recipients = $getParams['recipients'] ?? '';
48
49
        if (!empty($sender) && !empty($recipients) && !empty($apiKey) && !empty($domain)) {
50
            $recipients = str_replace("\r", '|', $recipients);
51
            $recipients = str_replace("\n", '|', $recipients);
52
            $recipients = explode('|', $recipients);
53
54
            $messenger = new Messenger\Mailgun($apiKey, $domain);
55
56
            foreach($recipients as $recipient) {
57
                if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
58
                    $messenger->addRecipient($recipient);
59
                }
60
            }
61
62
            if (filter_var($sender, FILTER_VALIDATE_EMAIL)) {
63
                $messenger->addSender($sender);
64
            }
65
66
            $messenger->setSubject($message['title']);
67
68
            if ($messenger->send($message['body'])) {
69
                return true;
70
            }
71
        }
72
        return false;
73
    }
74
}