MandrillAdapter::sendEmail()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 12
rs 9.9666
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 3.0.0
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 MandrillAdapter
24
 * @package Quantum\Libraries\Mailer
25
 */
26
class MandrillAdapter implements MailerInterface
27
{
28
    use MailerTrait;
29
30
    /**
31
     * @var string
32
     */
33
    public $name = 'Mandrill';
34
35
    /**
36
     * @var HttpClient
37
     */
38
    protected $httpClient;
39
40
    /**
41
     * @var string
42
     */
43
    private $apiUrl = 'https://mandrillapp.com/api/1.0/messages/send.json';
44
45
    /**
46
     * @var array
47
     */
48
    private $data = [];
49
50
    /**
51
     * MandrillAdapter constructor
52
     * @param array $params
53
     */
54
    public function __construct(array $params)
55
    {
56
        $this->httpClient = new HttpClient();
57
58
        $this->data['key'] = $params['api_key'];
59
    }
60
61
    /**
62
     * Prepares the data
63
     */
64
    private function prepare()
0 ignored issues
show
Unused Code introduced by
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...
65
    {
66
        $message = [];
67
68
        $message['from_email'] = $this->from['email'];
69
70
        if ($this->from['name']) {
71
            $message['from_name'] = $this->from['name'];
72
        }
73
74
        $message['to'] = $this->addresses;
75
76
        if ($this->subject) {
77
            $message['subject'] = $this->subject;
78
        }
79
80
        if ($this->message) {
81
            if ($this->templatePath) {
82
                $body = $this->createFromTemplate();
83
            } else {
84
                $body = is_array($this->message) ? implode('', $this->message) : $this->message;
85
            }
86
87
            $message['html'] = trim(str_replace("\n", '', $body));
88
        }
89
90
        $this->data['message'] = $message;
91
    }
92
93
    /**
94
     * @return bool
95
     */
96
    private function sendEmail(): bool
0 ignored issues
show
Unused Code introduced by
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...
97
    {
98
        try {
99
            $this->httpClient
100
                ->createRequest($this->apiUrl)
101
                ->setMethod('POST')
102
                ->setData(json_encode($this->data))
103
                ->start();
104
105
            return true;
106
        } catch (Exception $e) {
107
            return false;
108
        }
109
    }
110
}
111