SendTemplate::compose()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 20
ccs 0
cts 13
cp 0
crap 2
rs 9.7333
1
<?php
2
3
namespace Slides\Connector\Auth\Clients\Mandrill\Requests;
4
5
use Illuminate\Support\Arr;
6
use Slides\Connector\Auth\Clients\AbstractRequest;
7
8
/**
9
 * Class SendTemplate
10
 *
11
 * @property string $template
12
 * @property array $recipients
13
 * @property array $variables
14
 * @property array $from
15
 * @property string $subject
16
 * @property array|null $tags
17
 *
18
 * @package Slides\Connector\Auth\Clients\Mandrill\Requests
19
 */
20
class SendTemplate extends AbstractRequest
21
{
22
    /**
23
     * HTTP method
24
     *
25
     * @var string
26
     */
27
    protected $method = 'post';
28
29
    /**
30
     * Request URI
31
     *
32
     * @var string
33
     */
34
    protected $uri = 'messages/send-template.json';
35
36
    /**
37
     * Validate a response
38
     *
39
     * @return bool
40
     */
41
    public function success(): bool
42
    {
43
        return $this->client->getResponse()->getStatusCode() === 200;
44
    }
45
46
    /**
47
     * Compose a request
48
     *
49
     * @return void
50
     */
51
    public function compose()
52
    {
53
        $optional = [
54
            'from_email' => Arr::get($this->from, 'email'),
55
            'from_name' => Arr::get($this->from, 'name'),
56
            'subject' => $this->subject,
57
            'tags' => $this->tags
58
        ];
59
60
        $this->body([
61
            'template_name' => $this->template,
62
            'template_content' => [],
63
            'message' => array_merge([
64
                'preserve_recipients' => false,
65
                'track_opens' => true,
66
                'track_clicks' => true,
67
                'to' => $this->recipients,
68
                'merge_vars' => $this->variables,
69
                'merge_language' => 'handlebars'
70
            ], array_filter($optional))
71
        ]);
72
    }
73
}