Passed
Pull Request — master (#5)
by
unknown
03:40
created

MailgunDriver::setSubject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

108
            $response = $this->client->/** @scrutinizer ignore-call */ messages()->send(

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...
109
                $this->domain,
110
                $this->message
111
            );
112
113
            return [
114
                'id' => $response->getId(),
115
                'message' => $response->getMessage(),
116
            ];
117
        } catch (HttpClientException $exception) {
118
            throw SendError::responseError('mailgun', 0, $exception);
119
        }
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    public function toArray(): array
126
    {
127
        return [
128
            'body' => array_merge($this->body, [
129
                'Messages' => [
130
                    $this->message,
131
                ],
132
            ])
133
        ];
134
    }
135
}