Commit | Line | Data |
---|---|---|
e3f6a1e9 TO |
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 | ||
12 | namespace Civi\Api4\Action\MailSettings; | |
13 | ||
14 | use Civi\Api4\Generic\BasicBatchAction; | |
15 | ||
16 | class TestConnection extends BasicBatchAction { | |
17 | ||
18 | public function __construct($entityName, $actionName) { | |
19 | parent::__construct($entityName, $actionName, ['id', 'name']); | |
20 | } | |
21 | ||
22 | /** | |
23 | * @param array $item | |
24 | * @return array | |
25 | */ | |
26 | protected function doTask($item) { | |
27 | try { | |
28 | $mailStore = \CRM_Mailing_MailStore::getStore($item['name']); | |
29 | } | |
30 | catch (\Throwable $t) { | |
31 | \Civi::log()->warning('MailSettings: Failed to establish test connection', [ | |
32 | 'exception' => $t, | |
33 | ]); | |
34 | ||
35 | return [ | |
36 | 'title' => ts("Failed to connect"), | |
37 | 'details' => $t->getMessage() . "\n" . ts('(See log for more details.)'), | |
38 | 'error' => TRUE, | |
39 | ]; | |
40 | } | |
41 | ||
42 | if (empty($mailStore)) { | |
43 | return [ | |
44 | 'title' => ts("Failed to connect"), | |
45 | 'details' => ts('The mail service was not instantiated.'), | |
46 | 'error' => TRUE, | |
47 | ]; | |
48 | } | |
49 | ||
50 | $limitTestCount = 5; | |
51 | try { | |
52 | $msgs = $mailStore->fetchNext($limitTestCount); | |
53 | } | |
54 | catch (\Throwable $t) { | |
55 | \Civi::log()->warning('MailSettings: Failed to read test message', [ | |
56 | 'exception' => $t, | |
57 | ]); | |
58 | return [ | |
59 | 'title' => ts('Failed to read test message'), | |
60 | 'details' => $t->getMessage() . "\n" . ts('(See log for more details.)'), | |
61 | 'error' => TRUE, | |
62 | ]; | |
63 | } | |
64 | ||
65 | if (count($msgs) === 0) { | |
66 | return [ | |
67 | 'title' => ts('Connection succeeded.'), | |
68 | 'details' => ts('No new messages found.'), | |
69 | 'error' => FALSE, | |
70 | ]; | |
71 | } | |
72 | else { | |
73 | return [ | |
74 | 'title' => ts('Connection succeeded.'), | |
75 | 'details' => ts('Found at least %1 new messages.', [ | |
76 | 1 => count($msgs), | |
77 | ]), | |
78 | 'error' => FALSE, | |
79 | ]; | |
80 | } | |
81 | } | |
82 | ||
83 | } |