1 | <?php |
||
2 | |||
3 | namespace Backend\Modules\Mailmotor\Actions; |
||
4 | |||
5 | use Backend\Core\Engine\Base\ActionIndex; |
||
6 | use Backend\Core\Engine\Model; |
||
7 | use Backend\Core\Language\Language; |
||
8 | use Backend\Modules\Mailmotor\Domain\Settings\Command\SaveSettings; |
||
9 | use Backend\Modules\Mailmotor\Domain\Settings\Event\SettingsSavedEvent; |
||
10 | |||
11 | /** |
||
12 | * This tests the api |
||
13 | */ |
||
14 | final class Ping extends ActionIndex |
||
15 | { |
||
16 | public function execute(): void |
||
17 | { |
||
18 | parent::execute(); |
||
19 | |||
20 | $this->checkToken(); |
||
21 | |||
22 | // Successful API connection |
||
23 | if ($this->ping()) { |
||
24 | $this->redirect($this->getBackLink(['report' => 'successful-mail-engine-api-connection'])); |
||
25 | } |
||
26 | |||
27 | $this->resetMailEngine(); |
||
28 | $this->redirect($this->getBackLink(['error' => 'wrong-mail-engine-credentials'])); |
||
29 | } |
||
30 | |||
31 | private function ping(): bool |
||
32 | { |
||
33 | $gateway = $this->getContainer()->get('mailmotor.factory.public')->getSubscriberGateway(); |
||
34 | |||
35 | // don't try to ping if you aren't using a service like mailchimp or campaign monitor |
||
36 | if (!$gateway->ping($this->getContainer()->getParameter('mailmotor.list_id'))) { |
||
37 | return false; |
||
38 | } |
||
39 | |||
40 | $settings = $this->getContainer()->get('fork.settings'); |
||
41 | foreach (Language::getActiveLanguages() as $language) { |
||
42 | $languageListId = $settings->get('Mailmotor', 'list_id_' . $language); |
||
43 | |||
44 | // If there isn't a specific list for the language we don't need to check it |
||
45 | if ($languageListId === null) { |
||
46 | continue; |
||
47 | } |
||
48 | |||
49 | if (!$gateway->ping($languageListId)) { |
||
50 | return false; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | return true; |
||
55 | } |
||
56 | |||
57 | private function getBackLink(array $parameters = []): string |
||
58 | { |
||
59 | return Model::createUrlForAction( |
||
60 | 'Settings', |
||
61 | null, |
||
62 | null, |
||
63 | $parameters |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | private function resetMailEngine(): void |
||
68 | { |
||
69 | $saveSettings = new SaveSettings($this->get('fork.settings')); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
70 | $saveSettings->mailEngine = 'not_implemented'; |
||
71 | |||
72 | $this->get('command_bus')->handle($saveSettings); |
||
73 | |||
74 | $this->get('event_dispatcher')->dispatch( |
||
75 | SettingsSavedEvent::EVENT_NAME, |
||
76 | new SettingsSavedEvent($saveSettings) |
||
77 | ); |
||
78 | } |
||
79 | } |
||
80 |