Merge pull request #17736 from civicrm/5.27
[civicrm-core.git] / ext / flexmailer / src / Listener / Abdicator.php
CommitLineData
bdf67e28
SL
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11namespace Civi\FlexMailer\Listener;
12
13use Civi\FlexMailer\Event\RunEvent;
14
15/**
16 * Class Abdicator
17 * @package Civi\FlexMailer\Listener
18 *
19 * FlexMailer is in incubation -- it's a heavily reorganized version
20 * of the old MailingJob::deliver*() functions. It hasn't been tested as
21 * thoroughly and may not have perfect parity.
22 *
23 * During incubation, we want to mostly step-aside -- for traditional
24 * mailings, simply continue using the old system.
25 */
26class Abdicator {
27
28 /**
29 * @param \CRM_Mailing_BAO_Mailing $mailing
30 * @return bool
31 */
32 public static function isFlexmailPreferred($mailing) {
33 if ($mailing->sms_provider_id) {
34 return FALSE;
35 }
36
37 // Use FlexMailer for new-style email blasts (with custom `template_type`).
38 if ($mailing->template_type && $mailing->template_type !== 'traditional') {
39 return TRUE;
40 }
41
42 switch (\Civi::settings()->get('flexmailer_traditional')) {
43 case 'auto':
44 // Transitional support for old hidden setting "experimentalFlexMailerEngine" (bool)
45 // TODO: Remove this. Maybe after Q4 2019.
46 // TODO: Change this to default to flexmailer
47 return (bool) \Civi::settings()->get('experimentalFlexMailerEngine');
48
49 case 'bao':
50 return FALSE;
51
52 case 'flexmailer':
53 return TRUE;
54
55 default:
56 throw new \RuntimeException("Unrecognized value for setting 'flexmailer_traditional'");
57 }
58 }
59
60 /**
61 * Abdicate; defer to the old system during delivery.
62 *
63 * @param \Civi\FlexMailer\Event\RunEvent $e
64 */
65 public function onRun(RunEvent $e) {
66 if (self::isFlexmailPreferred($e->getMailing())) {
67 // OK, we'll continue running.
68 return;
69 }
70
71 // Nope, we'll abdicate.
72 $e->stopPropagation();
73 $isDelivered = $e->getJob()->deliver(
74 $e->context['deprecatedMessageMailer'],
75 $e->context['deprecatedTestParams']
76 );
77 $e->setCompleted($isDelivered);
78 }
79
80 /**
81 * Abdicate; defer to the old system when checking completeness.
82 *
83 * @param \Civi\FlexMailer\Event\CheckSendableEvent $e
84 */
85 public function onCheckSendable($e) {
86 if (self::isFlexmailPreferred($e->getMailing())) {
87 // OK, we'll continue running.
88 return;
89 }
90
91 $e->stopPropagation();
92 $errors = \CRM_Mailing_BAO_Mailing::checkSendable($e->getMailing());
93 if (is_array($errors)) {
94 foreach ($errors as $key => $message) {
95 $e->setError($key, $message);;
96 }
97 }
98 }
99
100}