Issues (9)

src/Drivers/SendgridDriver.php (1 issue)

Labels
Severity
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
10
class SendgridDriver implements Driver
11
{
12
13
    /** @var SendGrid|null  */
14
    public $client = null;
15
16
    /** @var SendGrid\Mail\Mail */
17
    public $message;
18
19
    /**
20
     * Driver constructor.
21
     * @param array $config
22
     * @throws InvalidConfiguration
23
     */
24
    public function __construct(array $config)
25
    {
26
        if (!isset($config['key'])) {
27
            throw InvalidConfiguration::invalidCredential('sendgrid', 'key');
28
        }
29
30
        $this->client = new SendGrid($config['key']);
31
        $this->message = new Mail();
32
    }
33
34
    /**
35
     * @return Driver
36
     */
37
    public function make(): Driver
38
    {
39
        $this->message = new Mail();
40
41
        return $this;
42
    }
43
44
    /**
45
     * @param string $name
46
     * @param string $email
47
     * @return Driver
48
     * @throws SendGrid\Mail\TypeException
49
     */
50
    public function setFrom(string $name, string $email): Driver
51
    {
52
        $this->message->setFrom($email, $name);
53
54
        return $this;
55
    }
56
57
    /**
58
     * @param string $templateId
59
     * @return Driver
60
     */
61
    public function setTemplate($templateId): Driver
62
    {
63
        $this->message->setTemplateId($templateId);
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param string $subject
70
     * @return Driver
71
     */
72
    public function setSubject(string $subject): Driver
73
    {
74
        $this->message->setSubject($subject);
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param string $name
81
     * @param string $email
82
     * @return Driver
83
     */
84
    public function setRecipient(string $name, string $email): Driver
85
    {
86
        $this->message->addTo($email, $name);
87
88
        return $this;
89
    }
90
91
    /**
92
     * @param string $name
93
     * @param string $email
94
     * @return Driver
95
     */
96
    public function setBcc(string $name, string $email): Driver
97
    {
98
        $this->message->addBcc($email, $name);
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 $key => $value) {
110
            $this->message->addSubstitution($key, $value);
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
        return $this;
123
    }
124
125
    /**
126
     * @return array
127
     * @throws SendError
128
     */
129
    public function send(): array
130
    {
131
        try {
132
            $response = $this->client->send($this->message);
0 ignored issues
show
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

132
            /** @scrutinizer ignore-call */ 
133
            $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...
133
            return $response->headers();
134
        } catch (\Exception $exception) {
135
            throw SendError::responseError('sendgrid', 0, $exception);
136
        }
137
    }
138
139
    /**
140
     * @return array
141
     */
142
    public function toArray(): array
143
    {
144
        return [
145
            'body' => [
146
                'message' => $this->message,
147
            ],
148
        ];
149
    }
150
}
151