MandrillAdapter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 33
c 2
b 0
f 1
dl 0
loc 93
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sendEmail() 0 12 2
A __construct() 0 5 1
A prepare() 0 27 6
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 MandrillAdapter
24
 * @package Quantum\Libraries\Mailer
25
 */
26
class MandrillAdapter implements MailerInterface
27
{
28
29
    use MailerTrait;
0 ignored issues
show
Bug introduced by
The trait Quantum\Libraries\Mailer\Traits\MailerTrait requires the property $mailer which is not provided by Quantum\Libraries\Mailer\Adapters\MandrillAdapter.
Loading history...
30
31
    /**
32
     * @var string
33
     */
34
    public $name = 'Mandrill';
35
36
    /**
37
     * @var HttpClient
38
     */
39
    private $httpClient;
40
41
    /**
42
     * @var string
43
     */
44
    private $apiKey;
0 ignored issues
show
introduced by
The private property $apiKey is not used, and could be removed.
Loading history...
45
46
    /**
47
     * @var string
48
     */
49
    private $apiUrl = 'https://mandrillapp.com/api/1.0/messages/send.json';
50
51
    /**
52
     * @var array
53
     */
54
    private $data = [];
55
56
    /**
57
     * @var MandrillAdapter|null
58
     */
59
    private static $instance = null;
0 ignored issues
show
introduced by
The private property $instance is not used, and could be removed.
Loading history...
60
61
    /**
62
     * MandrillAdapter constructor
63
     * @param array $params
64
     */
65
    public function __construct(array $params)
66
    {
67
        $this->httpClient = new HttpClient();
68
69
        $this->data['key'] = $params['api_key'];
70
    }
71
72
    /**
73
     * Prepares the data
74
     */
75
    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...
76
    {
77
        $message = [];
78
79
        $message['from_email'] = $this->from['email'];
80
81
        if ($this->from['name']) {
82
            $message['from_name'] = $this->from['name'];
83
        }
84
85
        $message['to'] = $this->addresses;
86
87
        if ($this->subject) {
88
            $message['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
            $message['html'] = trim(str_replace("\n", "", $body));
99
        }
100
101
        $this->data['message'] = $message;
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    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...
108
    {
109
        try {
110
            $this->httpClient
111
                ->createRequest($this->apiUrl)
112
                ->setMethod('POST')
113
                ->setData(json_encode($this->data))
114
                ->start();
115
116
            return true;
117
        } catch (Exception $e) {
118
            return false;
119
        }
120
    }
121
}