Passed
Pull Request — master (#4)
by
unknown
04:34
created

SendinblueDriver::setLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

126
            /** @scrutinizer ignore-call */ 
127
            $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...
127
            return ['messageId' => $response->getMessageId()];
128
        } catch (ApiException $exception) {
129
            throw SendError::responseError('sendinblue', 0, $exception);
130
        }
131
    }
132
133
    /**
134
     * @return array
135
     */
136
    public function toArray(): array
137
    {
138
        return [
139
            'body' => array_merge($this->body, [
140
                'Messages' => [
141
                    $this->message,
142
                ],
143
            ])
144
        ];
145
    }
146
}