Completed
Push — master ( 453b10...bec1da )
by Tobias
04:06
created

BatchMessage::addRecipient()   B

Complexity

Conditions 9
Paths 28

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 9.1796

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 20
cts 23
cp 0.8696
rs 8.0555
c 0
b 0
f 0
cc 9
nc 28
nop 3
crap 9.1796
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\Messages;
11
12
use Mailgun\Constants\Api;
13
use Mailgun\Constants\ExceptionMessages;
14
use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters;
15
use Mailgun\Messages\Exceptions\TooManyParameters;
16
17
/**
18
 * This class is used for batch sending. See the official documentation (link below)
19
 * for usage instructions.
20
 *
21
 * @deprecated Will be removed in 3.0. Use Mailgun\Message\BatchMessage
22
 * @see https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Messages/README.md
23
 */
24
class BatchMessage extends MessageBuilder
0 ignored issues
show
Deprecated Code introduced by
The class Mailgun\Messages\MessageBuilder has been deprecated with message: Will be removed in 3.0. Use Mailgun\Message\MessageBuilder

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
25
{
26
    /**
27
     * @var array
28
     */
29
    private $batchRecipientAttributes;
30
31
    /**
32
     * @var bool
33
     */
34
    private $autoSend;
35
36
    /**
37
     * @var \Mailgun\Connection\RestClient
38
     */
39
    private $restClient;
40
41
    /**
42
     * @var string
43
     */
44
    private $workingDomain;
45
46
    /**
47
     * @var array
48
     */
49
    private $messageIds = [];
50
51
    /**
52
     * @var string
53
     */
54
    private $endpointUrl;
55
56
    /**
57
     * @param \Mailgun\Connection\RestClient $restClient
58
     * @param string                         $workingDomain
59
     * @param bool                           $autoSend
60
     */
61 15
    public function __construct($restClient, $workingDomain, $autoSend)
62
    {
63 15
        $this->batchRecipientAttributes = [];
64 15
        $this->autoSend = $autoSend;
65 15
        $this->restClient = $restClient;
66 15
        $this->workingDomain = $workingDomain;
67 15
        $this->endpointUrl = $workingDomain.'/messages';
68 15
    }
69
70
    /**
71
     * @param string $headerName
72
     * @param string $address
73
     * @param array  $variables
74
     *
75
     * @throws MissingRequiredMIMEParameters
76
     * @throws TooManyParameters
77
     */
78 10
    protected function addRecipient($headerName, $address, $variables)
79
    {
80 10
        if (array_key_exists($headerName, $this->counters['recipients'])) {
81 10
            if ($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT) {
82 1
                if (false === $this->autoSend) {
83
                    throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS);
84
                }
85 1
                $this->sendMessage();
86 1
            }
87 10
        }
88
89 10
        $compiledAddress = $this->parseAddress($address, $variables);
90
91 10
        if (isset($this->message[$headerName])) {
92 2
            array_push($this->message[$headerName], $compiledAddress);
93 10
        } elseif ('h:reply-to' == $headerName) {
94
            $this->message[$headerName] = $compiledAddress;
95
        } else {
96 10
            $this->message[$headerName] = [$compiledAddress];
97
        }
98
99 10
        if (array_key_exists($headerName, $this->counters['recipients'])) {
100 10
            $this->counters['recipients'][$headerName] += 1;
101 10
            if (is_array($variables) && !array_key_exists('id', $variables)) {
102 10
                $variables['id'] = $this->counters['recipients'][$headerName];
103 10
            }
104 10
        }
105 10
        $this->batchRecipientAttributes["$address"] = $variables;
106 10
    }
107
108
    /**
109
     * @param array $message
110
     * @param array $files
111
     *
112
     * @throws MissingRequiredMIMEParameters
113
     */
114 7
    public function sendMessage($message = [], $files = [])
115
    {
116 7
        if (count($message) < 1) {
117 3
            $message = $this->message;
118 3
            $files = $this->files;
119 3
        }
120 7
        if (!array_key_exists('from', $message)) {
121 1
            throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
122 6
        } elseif (!array_key_exists('to', $message)) {
123 1
            throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
124 5
        } elseif (!array_key_exists('subject', $message)) {
125 1
            throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
126 4
        } elseif ((!array_key_exists('text', $message) && !array_key_exists('html', $message))) {
127 1
            throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
128
        } else {
129 3
            $message['recipient-variables'] = json_encode($this->batchRecipientAttributes);
130 3
            $response = $this->restClient->post($this->endpointUrl, $message, $files);
131 3
            $this->batchRecipientAttributes = [];
132 3
            $this->counters['recipients']['to'] = 0;
133 3
            $this->counters['recipients']['cc'] = 0;
134 3
            $this->counters['recipients']['bcc'] = 0;
135 3
            unset($this->message['to']);
136 3
            array_push($this->messageIds, $response->http_response_body->id);
137
        }
138 3
    }
139
140
    /**
141
     * @throws MissingRequiredMIMEParameters
142
     */
143 2
    public function finalize()
144
    {
145 2
        $this->sendMessage();
146 2
    }
147
148
    /**
149
     * @return string[]
150
     */
151 1
    public function getMessageIds()
152
    {
153 1
        return $this->messageIds;
154
    }
155
}
156