Completed
Push — master ( 43794e...d93ca7 )
by Gaël
17s queued 12s
created

SendgridDriver::setVariables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace DansMaCulotte\MailTemplate\Drivers;
4
5
use DansMaCulotte\MailTemplate\Exceptions\InvalidConfiguration;
6
use DansMaCulotte\MailTemplate\Exceptions\SendError;
7
use SendGrid;
8
use SendGrid\Mail\Mail;
9
use SendGrid\Mail\Substitution;
10
11
class SendgridDriver implements Driver
12
{
13
14
    /** @var SendGrid|null  */
15
    public $client = null;
16
17
    /** @var array */
18
    public $body = [];
19
20
    /** @var SendGrid\Mail\Mail */
21
    public $message;
22
23
    /**
24
     * Driver constructor.
25
     * @param array $config
26
     * @throws InvalidConfiguration
27
     */
28
    public function __construct(array $config)
29
    {
30
        if (!isset($config['key'])) {
31
            throw InvalidConfiguration::invalidCredential('sendgrid', 'key');
32
        }
33
34
        $this->client = new SendGrid($config['key']);
35
        $this->message = new Mail();
36
    }
37
38
    /**
39
     * @param string $name
40
     * @param string $email
41
     * @return Driver
42
     * @throws SendGrid\Mail\TypeException
43
     */
44
    public function setFrom(string $name, string $email): Driver
45
    {
46
        $this->message->setFrom($email, $name);
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param string $templateId
53
     * @return Driver
54
     */
55
    public function setTemplate(string $templateId): Driver
56
    {
57
        $this->message->setTemplateId($templateId);
58
59
        return $this;
60
    }
61
62
    /**
63
     * @param string $subject
64
     * @return Driver
65
     */
66
    public function setSubject(string $subject): Driver
67
    {
68
        $this->message->setSubject($subject);
69
70
        return $this;
71
    }
72
73
    /**
74
     * @param string $name
75
     * @param string $email
76
     * @return Driver
77
     */
78
    public function setRecipient(string $name, string $email): Driver
79
    {
80
        $this->message->addTo($email, $name);
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param array $variables
87
     * @return Driver
88
     */
89
    public function setVariables(array $variables): Driver
90
    {
91
        foreach ($variables as $key => $value) {
92
            $this->message->addSubstitution($key, $value);
93
        }
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param string $language
100
     * @return Driver
101
     */
102
    public function setLanguage(string $language): Driver
103
    {
104
        return $this;
105
    }
106
107
    /**
108
     * @return array
109
     * @throws SendError
110
     */
111
    public function send(): array
112
    {
113
        try {
114
            $response = $this->client->send($this->message);
0 ignored issues
show
Bug introduced by
The method send() 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

114
            /** @scrutinizer ignore-call */ 
115
            $response = $this->client->send($this->message);

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...
115
            return $response->headers();
116
        } catch (\Exception $exception) {
117
            throw SendError::responseError('sendgrid', 0, $exception);
118
        }
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    public function toArray(): array
125
    {
126
        return [
127
            'body' => array_merge($this->body, [
128
                'message' => $this->message,
129
            ]),
130
        ];
131
    }
132
}