Issues (213)

src/Libraries/Mailer/Adapters/MailgunAdapter.php (2 issues)

Severity
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Libraries\Mailer\Adapters;
16
17
use Quantum\Libraries\Mailer\Contracts\MailerInterface;
18
use Quantum\Libraries\Mailer\Traits\MailerTrait;
19
use Quantum\Libraries\HttpClient\HttpClient;
20
use Exception;
21
22
/**
23
 * class MailgunAdapter
24
 * @package Quantum\Libraries\Mailer
25
 */
26
class MailgunAdapter implements MailerInterface
27
{
28
29
    use MailerTrait;
30
31
    /**
32
     * @var string
33
     */
34
    public $name = 'Mailgun';
35
36
    /**
37
     * @var HttpClient
38
     */
39
    private $httpClient;
40
41
    /**
42
     * @var string
43
     */
44
    private $apiKey;
45
46
    /**
47
     * @var string
48
     */
49
    private $apiUrl = 'https://api.mailgun.net/v3/';
50
51
    /**
52
     * @var array
53
     */
54
    private $data = [];
55
56
    /**
57
     * @var MailgunAdapter|null
58
     */
59
    private static $instance = null;
60
61
    /**
62
     * MailgunAdapter constructor
63
     * @param array $params
64
     */
65
    public function __construct(array $params)
66
    {
67
        $this->httpClient = new HttpClient();
68
69
        $this->apiKey = $params['api_key'];
70
        $this->apiUrl .= $params['domain'] . '/messages';
71
    }
72
73
    /**
74
     * Prepares the data
75
     */
76
    private function prepare()
0 ignored issues
show
The method prepare() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
77
    {
78
        $this->data['from'] = $this->from['name'] . " " . $this->from['email'];
79
80
        $to = [];
81
        foreach ($this->addresses as $address) {
82
            $to[] = $address['email'];
83
        }
84
85
        $this->data['to'] = implode(',', $to);
86
87
        if ($this->subject) {
88
            $this->data['subject'] = $this->subject;
89
        }
90
91
        if ($this->message) {
92
            if ($this->templatePath) {
93
                $body = $this->createFromTemplate();
94
            } else {
95
                $body = is_array($this->message) ? implode($this->message) : $this->message;
96
            }
97
98
            $this->data['html'] = trim(str_replace("\n", "", $body));
99
        }
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    private function sendEmail(): bool
0 ignored issues
show
The method sendEmail() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
106
    {
107
        try {
108
            $this->httpClient
109
                ->createRequest($this->apiUrl)
110
                ->setMethod('POST')
111
                ->setHeaders([
112
                    'Authorization' => 'Basic ' . base64_encode('api:' . $this->apiKey),
113
                    'Content-Type' => 'application/x-www-form-urlencoded'
114
                ])
115
                ->setData(json_encode($this->data))
116
                ->start();
117
118
            return true;
119
        } catch (Exception $e) {
120
            return false;
121
        }
122
    }
123
}