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

SendgridDriver::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 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 array $variables
93
     * @return Driver
94
     */
95
    public function setVariables(array $variables): Driver
96
    {
97
        foreach ($variables as $key => $value) {
98
            $this->message->addSubstitution($key, $value);
99
        }
100
101
        return $this;
102
    }
103
104
    /**
105
     * @param string $language
106
     * @return Driver
107
     */
108
    public function setLanguage(string $language): Driver
109
    {
110
        return $this;
111
    }
112
113
    /**
114
     * @return array
115
     * @throws SendError
116
     */
117
    public function send(): array
118
    {
119
        try {
120
            $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

120
            /** @scrutinizer ignore-call */ 
121
            $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...
121
            return $response->headers();
122
        } catch (\Exception $exception) {
123
            throw SendError::responseError('sendgrid', 0, $exception);
124
        }
125
    }
126
127
    /**
128
     * @return array
129
     */
130
    public function toArray(): array
131
    {
132
        return [
133
            'body' => [
134
                'message' => $this->message,
135
            ],
136
        ];
137
    }
138
}
139