Completed
Push — master ( 845351...636e27 )
by Gaël
13:17
created

MailjetDriver::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

133
        /** @scrutinizer ignore-call */ 
134
        $response = $this->client->post(Resources::$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...
134
            'body' => [
135
                'Messages' => [
136
                    $this->message,
137
                ],
138
            ],
139
        ]);
140
141
        if ($response->success() === false) {
142
            throw SendError::responseError('mailjet');
143
        }
144
145
        return $response->getData();
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    public function toArray(): array
152
    {
153
        return [
154
            'body' => [
155
                'Messages' => [
156
                    $this->message,
157
                ],
158
            ],
159
        ];
160
    }
161
}
162