Passed
Pull Request — master (#134)
by Arman
03:44
created

SendgridAdapter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 40
c 1
b 0
f 1
dl 0
loc 119
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A sendEmail() 0 16 2
A prepare() 0 24 5
A getInstance() 0 7 2
A saveEmail() 0 3 1
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|null
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'] = [
91
            'email' => $this->from['email']
92
        ];
93
94
        $this->data['personalizations'] = [
95
            ['to' => $this->addresses]
96
        ];
97
98
        if ($this->subject) {
99
            $this->data['subject'] = $this->subject;
100
        }
101
102
        if ($this->message) {
103
            if ($this->templatePath) {
104
                $body = $this->createFromTemplate();
105
            } else {
106
                $body = is_array($this->message) ? implode($this->message) : $this->message;
107
            }
108
109
            $this->data['content'] = [
110
                'type' => 'text/html',
111
                'value' => trim(str_replace("\n", "", $body))
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