Merge pull request #22859 from civicrm/5.47
[civicrm-core.git] / ext / flexmailer / src / Validator.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;
12
13use Civi\FlexMailer\Event\CheckSendableEvent;
14use 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 */
23class Validator {
24
25 const EVENT_CHECK_SENDABLE = 'civi.flexmailer.checkSendable';
26
27 /**
7e0fcd9d 28 * @param array $params
bdf67e28
SL
29 * The mailing which may or may not be sendable.
30 * @return array
31 * List of error messages.
32 */
7e0fcd9d
EM
33 public static function createAndRun(array $params): array {
34 $mailing = new \CRM_Mailing_BAO_Mailing();
35 $mailing->id = $params['id'] ?? NULL;
36 if ($mailing->id) {
37 $mailing->find(TRUE);
38 }
39 $mailing->copyValues($params);
40
41 return (new Validator())->run(array(
bdf67e28
SL
42 'mailing' => $mailing,
43 'attachments' => \CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id),
44 ));
45 }
46
47 /**
48 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
49 */
50 private $dispatcher;
51
52 /**
53 * FlexMailer constructor.
54 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
55 */
56 public function __construct(EventDispatcherInterface $dispatcher = NULL) {
57 $this->dispatcher = $dispatcher ? $dispatcher : \Civi::service('dispatcher');
58 }
59
60 /**
61 * @param array $context
62 * An array which must define options:
63 * - mailing: \CRM_Mailing_BAO_Mailing
64 * - attachments: array
65 * @return array
66 * List of error messages.
67 * Ex: array('subject' => 'The Subject field is blank').
68 * Example keys: 'subject', 'name', 'from_name', 'from_email', 'body', 'body_html:unsubscribeUrl'.
69 */
70 public function run($context) {
71 $checkSendable = new CheckSendableEvent($context);
72 $this->dispatcher->dispatch(static::EVENT_CHECK_SENDABLE, $checkSendable);
73 return $checkSendable->getErrors();
74 }
75
76}