Completed
Pull Request — master (#4)
by
unknown
04:43
created

SendinblueDriver::toArray()   A

Complexity

Conditions 1
Paths 1

Size

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

122
        /** @scrutinizer ignore-call */ 
123
        $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...
123
        return ['messageId' => $response->getMessageId()];
124
    }
125
126
    /**
127
     * @return array
128
     */
129
    public function toArray(): array
130
    {
131
        return [
132
            'body' => array_merge($this->body, [
133
                'Messages' => [
134
                    $this->message,
135
                ],
136
            ])
137
        ];
138
    }
139
}