Completed
Push — master ( b8d88d...61d6a1 )
by Gaël
03:56 queued 11s
created

SendinblueDriver::setFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
namespace DansMaCulotte\MailTemplate\Drivers;
4
5
6
use DansMaCulotte\MailTemplate\Exceptions\InvalidConfiguration;
7
use DansMaCulotte\MailTemplate\Exceptions\SendError;
8
use GuzzleHttp\Client;
9
use SendinBlue\Client\Api\SMTPApi;
10
use SendinBlue\Client\ApiException;
11
use SendinBlue\Client\Configuration;
12
use SendinBlue\Client\Model\SendSmtpEmail;
13
use SendinBlue\Client\Model\SendSmtpEmailSender;
14
use SendinBlue\Client\Model\SendSmtpEmailTo;
15
16
class SendinblueDriver implements Driver
17
{
18
19
    /** @var SMTPApi|null  */
20
    public $client = null;
21
22
    /** @var array */
23
    public $body = [];
24
25
    /** @var array */
26
    public $message = [];
27
28
    /**
29
     *
30
     *
31
     * Driver constructor.
32
     * @param array $config
33
     * @throws InvalidConfiguration
34
     */
35
    public function __construct(array $config)
36
    {
37
        if (!isset($config['key'])) {
38
            throw InvalidConfiguration::invalidCredential('sendinblue', 'key');
39
        }
40
41
        $config = Configuration::getDefaultConfiguration()->setApiKey('api-key', $config['key']);
42
        $this->client = new SMTPApi(new Client(), $config, null);
43
    }
44
45
    /**
46
     * @param string $name
47
     * @param string $email
48
     * @return Driver
49
     */
50
    public function setFrom(string $name, string $email): Driver
51
    {
52
        $this->message['sender'] = new SendSmtpEmailSender([
53
            'name' => $name,
54
            'email' => $email
55
        ]);
56
57
        return $this;
58
    }
59
60
    /**
61
     * @param string $template
62
     * @return Driver
63
     */
64
    public function setTemplate($template): Driver
65
    {
66
        $this->message['templateId'] = $template;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param string $subject
73
     * @return Driver
74
     */
75
    public function setSubject(string $subject): Driver
76
    {
77
        $this->message['subject'] = $subject;
78
79
        return $this;
80
    }
81
82
    /**
83
     * @param string $name
84
     * @param string $email
85
     * @return Driver
86
     */
87
    public function setRecipient(string $name, string $email): Driver
88
    {
89
        $this->message['to'] = [new SendSmtpEmailTo([
90
            'name' => $name,
91
            'email' => $email
92
        ])];
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param array $variables
99
     * @return Driver
100
     */
101
    public function setVariables(array $variables): Driver
102
    {
103
104
        $params = [];
105
        foreach ($variables as $variableKey => $variableValue) {
106
            $params[$variableKey] = $variableValue;
107
        }
108
109
        $this->message['params'] = $params;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @param string $language
116
     * @return Driver
117
     */
118
    public function setLanguage(string $language): Driver
119
    {
120
        return $this;
121
    }
122
123
    /**
124
     * @return array
125
     * @throws SendError
126
     */
127
    public function send(): array
128
    {
129
        $email = new SendSmtpEmail($this->message);
130
131
        try {
132
            $response = $this->client->sendTransacEmail($email);
0 ignored issues
show
Bug introduced by
The method sendTransacEmail() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
            /** @scrutinizer ignore-call */ 
133
            $response = $this->client->sendTransacEmail($email);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
            return ['messageId' => $response->getMessageId()];
134
        } catch (ApiException $exception) {
135
            throw SendError::responseError('sendinblue', 0, $exception);
136
        }
137
    }
138
139
    /**
140
     * @return array
141
     */
142
    public function toArray(): array
143
    {
144
        return [
145
            'body' => array_merge($this->body, [
146
                'Messages' => [
147
                    $this->message,
148
                ],
149
            ])
150
        ];
151
    }
152
}