Merge pull request #21612 from civicrm/5.42
[civicrm-core.git] / ext / flexmailer / src / Validator.php
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 */
11 namespace Civi\FlexMailer;
12
13 use Civi\FlexMailer\Event\CheckSendableEvent;
14 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
16 /**
17 * Class Validator
18 * @package Civi\FlexMailer
19 *
20 * The *validator* determines whether a mailing is completely specified
21 * (sendable). If not, delivery should be blocked.
22 */
23 class Validator {
24
25 const EVENT_CHECK_SENDABLE = 'civi.flexmailer.checkSendable';
26
27 /**
28 * @param \CRM_Mailing_DAO_Mailing $mailing
29 * The mailing which may or may not be sendable.
30 * @return array
31 * List of error messages.
32 */
33 public static function createAndRun($mailing) {
34 $validator = new \Civi\FlexMailer\Validator();
35 return $validator->run(array(
36 'mailing' => $mailing,
37 'attachments' => \CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id),
38 ));
39 }
40
41 /**
42 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
43 */
44 private $dispatcher;
45
46 /**
47 * FlexMailer constructor.
48 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
49 */
50 public function __construct(EventDispatcherInterface $dispatcher = NULL) {
51 $this->dispatcher = $dispatcher ? $dispatcher : \Civi::service('dispatcher');
52 }
53
54 /**
55 * @param array $context
56 * An array which must define options:
57 * - mailing: \CRM_Mailing_BAO_Mailing
58 * - attachments: array
59 * @return array
60 * List of error messages.
61 * Ex: array('subject' => 'The Subject field is blank').
62 * Example keys: 'subject', 'name', 'from_name', 'from_email', 'body', 'body_html:unsubscribeUrl'.
63 */
64 public function run($context) {
65 $checkSendable = new CheckSendableEvent($context);
66 $this->dispatcher->dispatch(static::EVENT_CHECK_SENDABLE, $checkSendable);
67 return $checkSendable->getErrors();
68 }
69
70 }