Completed
Push — master ( 1bc138...210f5b )
by Arman
18s queued 15s
created

SendinblueAdapter::sendEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 17
rs 9.8333
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.0
13
 */
14
15
namespace Quantum\Libraries\Mailer\Adapters;
16
17
use Quantum\Libraries\Mailer\MailerInterface;
18
use Quantum\Libraries\Curl\HttpClient;
19
use Quantum\Libraries\Mailer\MailTrap;
20
use Exception;
21
22
/**
23
 * class SendinblueAdapter
24
 * @package Quantum\Libraries\Mailer
25
 */
26
class SendinblueAdapter implements MailerInterface
27
{
28
29
    use MailerAdapterTrait;
0 ignored issues
show
Bug introduced by
The trait Quantum\Libraries\Mailer...ters\MailerAdapterTrait requires the property $mailer which is not provided by Quantum\Libraries\Mailer...pters\SendinblueAdapter.
Loading history...
30
31
    /**
32
     * @var string
33
     */
34
    public $name = 'Sendinblue';
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.sendinblue.com/v3/smtp/email';
50
51
    /**
52
     * @var array
53
     */
54
    private $data = [];
55
56
    /**
57
     * @var SendinblueAdapter|null
58
     */
59
    private static $instance = null;
60
61
    /**
62
     * SendinblueAdapter constructor
63
     * @param array $params
64
     */
65
    private function __construct(array $params)
66
    {
67
        $this->httpClient = new HttpClient();
68
69
        $this->apiKey = $params['api_key'];
70
    }
71
72
    /**
73
     * Get Instance
74
     * @param array $params
75
     * @return SendinblueAdapter
76
     */
77
    public static function getInstance(array $params): SendinblueAdapter
78
    {
79
        if (self::$instance === null) {
80
            self::$instance = new self($params);
81
        }
82
83
        return self::$instance;
84
    }
85
86
    /**
87
     * Prepares the data
88
     */
89
    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...
90
    {
91
        $this->data['sender'] = $this->from;
92
        $this->data['to'] = $this->addresses;
93
94
        if ($this->subject) {
95
            $this->data['subject'] = $this->subject;
96
        }
97
98
        if ($this->message) {
99
            if ($this->templatePath) {
100
                $body = $this->createFromTemplate();
101
            } else {
102
                $body = is_array($this->message) ? implode($this->message) : $this->message;
103
            }
104
105
            $this->data['htmlContent'] = trim(str_replace("\n", "", $body));
106
        }
107
    }
108
109
    /**
110
     * @return bool
111
     */
112
    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...
113
    {
114
        try {
115
            $this->httpClient
116
                ->createRequest($this->apiUrl)
117
                ->setMethod('POST')
118
                ->setHeaders([
119
                    'Accept' => 'application/json',
120
                    'Content-type' => 'application/json',
121
                    'api-key' => $this->apiKey
122
                ])
123
                ->setData(json_encode($this->data))
124
                ->start();
125
126
            return true;
127
        } catch (Exception $e) {
128
            return false;
129
        }
130
    }
131
132
    /**
133
     * @return bool
134
     * @throws Exception
135
     */
136
    private function saveEmail(): bool
0 ignored issues
show
Unused Code introduced by
The method saveEmail() 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...
137
    {
138
        return MailTrap::getInstance()->saveMessage($this->getMessageId(), $this->getMessageContent());
139
    }
140
}
141