Unsubscribe   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 71
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 56 7
A getEmail() 0 3 1
A parse() 0 5 2
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())
0 ignored issues
show
Bug introduced by
It seems like Frontend\Core\Language\Locale::frontendLanguage() can also be of type null; however, parameter $locale of Frontend\Modules\Mailmot...cription::__construct() does only seem to accept Frontend\Core\Language\Locale, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
            new Unsubscription(/** @scrutinizer ignore-type */ Locale::frontendLanguage(), $this->getEmail())
Loading history...
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