Issues (3627)

Tests/Helper/Transport/BatchTransport.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2017 Mautic Contributors. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\EmailBundle\Tests\Helper\Transport;
13
14
use Mautic\EmailBundle\Swiftmailer\Transport\AbstractTokenArrayTransport;
15
16
class BatchTransport extends AbstractTokenArrayTransport implements \Swift_Transport
17
{
18
    private $fromAddresses = [];
19
    private $fromNames     = [];
20
    private $metadatas     = [];
21
    private $validate      = false;
22
    private $maxRecipients;
23
    private $numberToFail;
24
25
    /**
26
     * BatchTransport constructor.
27
     *
28
     * @param bool $validate
29
     */
30
    public function __construct($validate = false, $maxRecipients = 4, $numberToFail = 1)
31
    {
32
        $this->validate      = $validate;
33
        $this->maxRecipients = $maxRecipients;
34
        $this->numberToFail  = (int) $numberToFail;
35
    }
36
37
    /**
38
     * @param null $failedRecipients
39
     */
40
    public function send(\Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
41
    {
42
        $this->message         = $message;
43
        $from                  = $message->getFrom();
44
        $fromEmail             = key($from);
45
        $this->fromAddresses[] = $fromEmail;
46
        $this->fromNames[]     = $from[$fromEmail];
47
        $this->metadatas[]     = $this->getMetadata();
48
49
        $messageArray = $this->messageToArray();
50
51
        if ($this->validate && $this->numberToFail) {
52
            --$this->numberToFail;
53
54
            if (empty($messageArray['subject'])) {
55
                $this->throwException('Subject empty');
56
            }
57
58
            if (empty($messageArray['recipients']['to'])) {
59
                $this->throwException('To empty');
60
            }
61
        }
62
63
        return true;
64
    }
65
66
    /**
67
     * @return int
68
     */
69
    public function getMaxBatchLimit()
70
    {
71
        return $this->maxRecipients;
72
    }
73
74
    /**
75
     * @param int    $toBeAdded
76
     * @param string $type
77
     *
78
     * @return int
79
     */
80
    public function getBatchRecipientCount(\Swift_Message $message, $toBeAdded = 1, $type = 'to')
81
    {
82
        $to      = $message->getTo();
83
        $toCount = (is_array($to) || $to instanceof \Countable) ? count($to) : 0;
0 ignored issues
show
The condition is_array($to) is always true.
Loading history...
84
85
        return ('to' === $type) ? $toCount + $toBeAdded : $toCount;
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function getFromAddresses()
92
    {
93
        return $this->fromAddresses;
94
    }
95
96
    /**
97
     * return array.
98
     */
99
    public function getFromNames()
100
    {
101
        return $this->fromNames;
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    public function getMetadatas()
108
    {
109
        return $this->metadatas;
110
    }
111
112
    /**
113
     * @return bool
114
     */
115
    public function ping()
116
    {
117
        return true;
118
    }
119
}
120