1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Frontend\Modules\Mailmotor\Actions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Frontend\Core\Engine\Base\Block as FrontendBaseBlock; |
7
|
|
|
use Frontend\Core\Engine\Navigation as FrontendNavigation; |
8
|
|
|
use Frontend\Core\Language\Locale; |
9
|
|
|
use Frontend\Modules\Mailmotor\Domain\Subscription\Command\Unsubscription; |
10
|
|
|
use Frontend\Modules\Mailmotor\Domain\Subscription\Event\NotImplementedUnsubscribedEvent; |
11
|
|
|
use Frontend\Modules\Mailmotor\Domain\Subscription\UnsubscribeType; |
12
|
|
|
use MailMotor\Bundle\MailMotorBundle\Exception\NotImplementedException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This is the Unsubscription-action for Mailmotor |
16
|
|
|
*/ |
17
|
|
|
class Unsubscribe extends FrontendBaseBlock |
18
|
|
|
{ |
19
|
|
|
public function execute(): void |
20
|
|
|
{ |
21
|
|
|
parent::execute(); |
22
|
|
|
|
23
|
|
|
$form = $this->createForm( |
24
|
|
|
UnsubscribeType::class, |
25
|
|
|
new Unsubscription(Locale::frontendLanguage(), $this->getEmail()) |
|
|
|
|
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
$form->handleRequest($this->getRequest()); |
29
|
|
|
|
30
|
|
|
if (!$form->isSubmitted() || !$form->isValid()) { |
31
|
|
|
$this->template->assign('form', $form->createView()); |
32
|
|
|
|
33
|
|
|
if ($form->isSubmitted()) { |
34
|
|
|
$this->template->assign('mailmotorUnsubscribeHasFormError', true); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->loadTemplate(); |
38
|
|
|
$this->parse(); |
39
|
|
|
|
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @var Unsubscription $unsubscription */ |
44
|
|
|
$unsubscription = $form->getData(); |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
// The command bus will handle the unsubscription |
48
|
|
|
$this->get('command_bus')->handle($unsubscription); |
49
|
|
|
} catch (NotImplementedException $e) { |
50
|
|
|
// fallback for when no mail-engine is chosen in the Backend |
51
|
|
|
$this->get('event_dispatcher')->dispatch( |
52
|
|
|
NotImplementedUnsubscribedEvent::EVENT_NAME, |
53
|
|
|
new NotImplementedUnsubscribedEvent($unsubscription) |
54
|
|
|
); |
55
|
|
|
} catch (Exception $exception) { |
56
|
|
|
$reason = json_decode($exception->getMessage()); |
57
|
|
|
// check if the error is one from mailchimp |
58
|
|
|
if ($reason === false) { |
59
|
|
|
throw $exception; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->template->assign('mailmotorUnsubscribeHasFormError', true); |
63
|
|
|
$this->template->assign('form', $form->createView()); |
64
|
|
|
|
65
|
|
|
$this->loadTemplate(); |
66
|
|
|
$this->parse(); |
67
|
|
|
|
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->redirect( |
72
|
|
|
FrontendNavigation::getUrlForBlock('Mailmotor', 'Unsubscribe') |
73
|
|
|
. '?unsubscribed=true' |
74
|
|
|
. '#mailmotorUnsubscribeForm' |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getEmail(): ?string |
79
|
|
|
{ |
80
|
|
|
return $this->getRequest()->request->get('email'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function parse(): void |
84
|
|
|
{ |
85
|
|
|
if ($this->url->getParameter('unsubscribed') === 'true') { |
86
|
|
|
$this->template->assign('mailmotorUnsubscribeIsSuccess', true); |
87
|
|
|
$this->template->assign('mailmotorUnsubscribeHideForm', true); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|