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

SendgridAdapter::saveEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 SendgridAdapter
24
 * @package Quantum\Libraries\Mailer
25
 */
26
class SendgridAdapter implements MailerInterface
27
{
28
    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\Adapters\SendgridAdapter.
Loading history...
29
30
    /**
31
     * @var string
32
     */
33
    public $name = 'Sendgrid';
34
35
    /**
36
     * @var HttpClient
37
     */
38
    private $httpClient;
39
40
    /**
41
     * @var string
42
     */
43
    private $apiKey;
44
45
    /**
46
     * @var string
47
     */
48
    private $apiUrl = 'https://api.sendgrid.com/v3/mail/send';
49
50
    /**
51
     * @var array
52
     */
53
    private $data = [];
54
55
    /**
56
     * @var SendgridAdapter|null
57
     */
58
    private static $instance = null;
59
60
    /**
61
     * SendgridAdapter constructor
62
     * @param array $params
63
     */
64
    private function __construct(array $params)
65
    {
66
        $this->httpClient = new HttpClient();
67
68
        $this->apiKey = $params['api_key'];
69
    }
70
71
    /**
72
     * Get Instance
73
     * @param array $params
74
     * @return SendgridAdapter
75
     */
76
    public static function getInstance(array $params): SendgridAdapter
77
    {
78
        if (self::$instance === null) {
79
            self::$instance = new self($params);
80
        }
81
82
        return self::$instance;
83
    }
84
85
    /**
86
     * Prepares the data
87
     */
88
    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...
89
    {
90
        $this->data['from'] = $this->from;
91
92
        $this->data['personalizations'] = [
93
            ['to' => $this->addresses]
94
        ];
95
96
        if ($this->subject) {
97
            $this->data['subject'] = $this->subject;
98
        }
99
100
        if ($this->message) {
101
            if ($this->templatePath) {
102
                $body = $this->createFromTemplate();
103
            } else {
104
                $body = is_array($this->message) ? implode($this->message) : $this->message;
105
            }
106
107
            $this->data['content'] = [
108
                [
109
                    'type' => 'text/html',
110
                    'value' => trim(str_replace("\n", "", $body))
111
                ]
112
            ];
113
        }
114
    }
115
116
    /**
117
     * @return bool
118
     */
119
    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...
120
    {
121
        try {
122
            $this->httpClient
123
                ->createRequest($this->apiUrl)
124
                ->setMethod('POST')
125
                ->setHeaders([
126
                    'Authorization' => 'Bearer ' . $this->apiKey,
127
                    'Content-Type' => 'application/json'
128
                ])
129
                ->setData(json_encode($this->data))
130
                ->start();
131
132
            return true;
133
        } catch (Exception $e) {
134
            return false;
135
        }
136
    }
137
138
    /**
139
     * @return bool
140
     * @throws Exception
141
     */
142
    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...
143
    {
144
        return MailTrap::getInstance()->saveMessage($this->getMessageId(), $this->getMessageContent());
145
    }
146
}
147