Completed
Push — master ( 1f5bd4...54aa0a )
by David
07:08 queued 03:18
created

BatchMessage::addRecipient()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 13
cts 14
cp 0.9286
rs 8.9617
c 0
b 0
f 0
cc 6
nc 7
nop 3
crap 6.0131
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 14
    public function __construct(Message $messageApi, $domain, $autoSend)
56
    {
57 14
        $this->api = $messageApi;
58 14
        $this->domain = $domain;
59 14
        $this->autoSend = $autoSend;
60 14
    }
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
     * @return BatchMessage
77
     */
78 14
    protected function addRecipient($headerName, $address, array $variables)
79
    {
80 14
        if (array_key_exists($headerName, $this->counters['recipients'])) {
81 13
            if ($this->counters['recipients'][$headerName] === self::RECIPIENT_COUNT_LIMIT) {
82 1
                if (false === $this->autoSend) {
83
                    throw TooManyRecipients::whenAutoSendDisabled();
84
                }
85 1
                $this->finalize();
86 1
            }
87 13
        }
88
89 14
        parent::addRecipient($headerName, $address, $variables);
90
91 14
        if (array_key_exists($headerName, $this->counters['recipients']) && !array_key_exists('id', $variables)) {
92 13
            $variables['id'] = $headerName.'_'.$this->counters['recipients'][$headerName];
93 13
        }
94
95 14
        $this->batchRecipientAttributes[(string) $address] = $variables;
96
97 14
        return $this;
98
    }
99
100
    /**
101
     * @throws MissingRequiredParameter
102
     */
103 7
    public function finalize()
104
    {
105 7
        $message = $this->message;
106
107 7
        if (empty($this->domain)) {
108
            throw new RuntimeException('You must call BatchMessage::setDomain before sending messages.');
109 7
        } elseif (empty($message['from'])) {
110 1
            throw MissingRequiredParameter::create('from');
111 6
        } elseif (empty($message['to'])) {
112 1
            throw MissingRequiredParameter::create('to');
113 5
        } elseif (empty($message['subject'])) {
114 1
            throw MissingRequiredParameter::create('subject');
115 4
        } elseif (empty($message['text']) && empty($message['html'])) {
116 1
            throw MissingRequiredParameter::create('text" or "html');
117
        } else {
118 3
            $message['recipient-variables'] = json_encode($this->batchRecipientAttributes);
119 3
            $response = $this->api->send($this->domain, $message);
120
121 3
            $this->batchRecipientAttributes = [];
122 3
            $this->counters['recipients']['to'] = 0;
123 3
            $this->counters['recipients']['cc'] = 0;
124 3
            $this->counters['recipients']['bcc'] = 0;
125 3
            unset($this->message['to']);
126
127 3
            $this->messageIds[] = $response->getId();
128
        }
129 3
    }
130
131
    /**
132
     * @return string[]
133
     */
134 1
    public function getMessageIds()
135
    {
136 1
        return $this->messageIds;
137
    }
138
}
139