Issues (3627)

SmsBundle/Broadcast/BroadcastExecutioner.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2019 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\SmsBundle\Broadcast;
13
14
use Mautic\CampaignBundle\Executioner\ContactFinder\Limiter\ContactLimiter;
15
use Mautic\ChannelBundle\Event\ChannelBroadcastEvent;
16
use Mautic\SmsBundle\Entity\Sms;
17
use Mautic\SmsBundle\Model\SmsModel;
18
use Symfony\Component\Translation\TranslatorInterface;
19
20
class BroadcastExecutioner
21
{
22
    /**
23
     * @var SmsModel
24
     */
25
    private $smsModel;
26
27
    /**
28
     * @var ContactLimiter
29
     */
30
    private $contactLimiter;
31
32
    /**
33
     * @var BroadcastQuery
34
     */
35
    private $broadcastQuery;
36
37
    /**
38
     * @var BroadcastResult
39
     */
40
    private $result;
41
42
    /**
43
     * @var TranslatorInterface
44
     */
45
    private $translator;
46
47
    /**
48
     * BroadcastExecutioner constructor.
49
     */
50
    public function __construct(SmsModel $smsModel, BroadcastQuery $broadcastQuery, TranslatorInterface $translator)
51
    {
52
        $this->smsModel       = $smsModel;
53
        $this->broadcastQuery = $broadcastQuery;
54
        $this->translator     = $translator;
55
    }
56
57
    public function execute(ChannelBroadcastEvent $event)
58
    {
59
        // Get list of published broadcasts or broadcast if there is only a single ID
60
        $smses = $this->smsModel->getRepository()->getPublishedBroadcasts($event->getId());
61
        while (false !== ($next = $smses->next())) {
62
            $sms                  = reset($next);
63
            $this->contactLimiter = new ContactLimiter($event->getBatch(), null, $event->getMinContactIdFilter(), $event->getMaxContactIdFilter(), [], null, null, $event->getLimit());
64
            $this->result         = new BroadcastResult();
65
            try {
66
                $this->send($sms);
67
            } catch (\Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
68
            }
69
70
            $event->setResults(
71
                sprintf('%s: %s', $this->translator->trans('mautic.sms.sms'), $sms->getName()),
72
                $this->result->getSentCount(),
73
                $this->result->getFailedCount()
74
            );
75
        }
76
    }
77
78
    /**
79
     * @throws LimitQuotaException
80
     * @throws \Mautic\CampaignBundle\Executioner\Exception\NoContactsFoundException
81
     */
82
    private function send(Sms $sms)
83
    {
84
        $contacts = $this->broadcastQuery->getPendingContacts($sms, $this->contactLimiter);
85
        while (!empty($contacts)) {
86
            foreach ($contacts as $contact) {
87
                $contactId  = $contact['id'];
88
                $results    = $this->smsModel->sendSms($sms, $contactId, [
89
                    'channel'=> [
90
                        'sms', $sms->getId(),
91
                    ],
92
                    'listId'=> $contact['listId'],
93
                ]);
94
                $this->result->process($results);
95
            }
96
97
            $this->contactLimiter->setBatchMinContactId($contactId + 1);
98
99
            if ($this->contactLimiter->hasCampaignLimit()) {
100
                $this->contactLimiter->reduceCampaignLimitRemaining(count($results));
101
            }
102
103
            // Next batch
104
            $contacts = $this->broadcastQuery->getPendingContacts($sms, $this->contactLimiter);
105
        }
106
    }
107
}
108