Completed
Push — master ( 48e5b8...eb0757 )
by Tobias
20:40
created

BatchMessage   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 0
loc 112
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addRecipient() 0 19 6
B finalize() 0 27 7
A getMessageIds() 0 4 1
1
<?php
2
3
/*
4
 * Copyright (C) 2013 Mailgun
5
 *
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Mailgun\Message;
11
12
use Mailgun\Api\Message;
13
use Mailgun\Message\Exceptions\MissingRequiredParameter;
14
use Mailgun\Message\Exceptions\RuntimeException;
15
use Mailgun\Message\Exceptions\TooManyRecipients;
16
17
/**
18
 * This class is used for batch sending. See the official documentation (link below)
19
 * for usage instructions.
20
 *
21
 * @see https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Message/README.md
22
 */
23
class BatchMessage extends MessageBuilder
24
{
25
    /**
26
     * @var array
27
     */
28
    private $batchRecipientAttributes = [];
29
30
    /**
31
     * @var bool
32
     */
33
    private $autoSend = true;
34
35
    /**
36
     * @var array
37
     */
38
    private $messageIds = [];
39
40
    /**
41
     * @var string
42
     */
43
    private $domain;
44
45
    /**
46
     * @var Message
47
     */
48
    private $api;
49
50
    /**
51
     * @param Message $messageApi
52
     * @param string  $domain
53
     * @param bool    $autoSend
54
     */
55
    public function __construct(Message $messageApi, $domain, $autoSend)
56
    {
57
        $this->api = $messageApi;
58
        $this->domain = $domain;
59
        $this->autoSend = $autoSend;
60
    }
61
62
    /**
63
     * @param string $headerName
64
     * @param string $address
65
     * @param array  $variables  {
66
     *
67
     *     @var string $id
68
     *     @var string $full_name
69
     *     @var string $first
70
     *     @var string $last
71
     * }
72
     *
73
     * @throws MissingRequiredParameter
74
     * @throws TooManyRecipients
75
     */
76
    protected function addRecipient($headerName, $address, array $variables)
77
    {
78
        if (array_key_exists($headerName, $this->counters['recipients'])) {
79
            if ($this->counters['recipients'][$headerName] === self::RECIPIENT_COUNT_LIMIT) {
80
                if (false === $this->autoSend) {
81
                    throw TooManyRecipients::whenAutoSendDisabled();
82
                }
83
                $this->finalize();
84
            }
85
        }
86
87
        parent::addRecipient($headerName, $address, $variables);
88
89
        if (array_key_exists($headerName, $this->counters['recipients']) && !array_key_exists('id', $variables)) {
90
            $variables['id'] = $headerName.'_'.$this->counters['recipients'][$headerName];
91
        }
92
93
        $this->batchRecipientAttributes[(string) $address] = $variables;
94
    }
95
96
    /**
97
     * @throws MissingRequiredParameter
98
     */
99
    public function finalize()
100
    {
101
        $message = $this->message;
102
103
        if (empty($this->domain)) {
104
            throw new RuntimeException('You must call BatchMessage::setDomain before sending messages.');
105
        } elseif (empty($message['from'])) {
106
            throw MissingRequiredParameter::create('from');
107
        } elseif (empty($message['to'])) {
108
            throw MissingRequiredParameter::create('to');
109
        } elseif (empty($message['subject'])) {
110
            throw MissingRequiredParameter::create('subject');
111
        } elseif (empty($message['text']) && empty($message['html'])) {
112
            throw MissingRequiredParameter::create('text" or "html');
113
        } else {
114
            $message['recipient-variables'] = json_encode($this->batchRecipientAttributes);
115
            $response = $this->api->send($this->domain, $message);
116
117
            $this->batchRecipientAttributes = [];
118
            $this->counters['recipients']['to'] = 0;
119
            $this->counters['recipients']['cc'] = 0;
120
            $this->counters['recipients']['bcc'] = 0;
121
            unset($this->message['to']);
122
123
            $this->messageIds[] = $response->getId();
124
        }
125
    }
126
127
    /**
128
     * @return string[]
129
     */
130
    public function getMessageIds()
131
    {
132
        return $this->messageIds;
133
    }
134
}
135