Passed
Push — master ( 966e68...f8b583 )
by Christian
15:57 queued 04:21
created

AccountProfileController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 179
rs 10
c 0
b 0
f 0
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 7 1
A saveEmail() 0 17 3
A profileOverview() 0 10 1
A savePassword() 0 15 2
A deleteProfile() 0 20 5
A saveProfile() 0 15 3
A __construct() 0 14 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Controller;
4
5
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
6
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractChangeCustomerProfileRoute;
7
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractChangeEmailRoute;
8
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractChangePasswordRoute;
9
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractDeleteCustomerRoute;
10
use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
11
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
12
use Shopware\Core\Framework\Feature;
13
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
14
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
15
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
16
use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
17
use Shopware\Core\System\SalesChannel\SalesChannelContext;
18
use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoader;
19
use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoader;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\Routing\Annotation\Route;
23
24
/**
25
 * @RouteScope(scopes={"storefront"})
26
 */
27
class AccountProfileController extends StorefrontController
28
{
29
    /**
30
     * @var AccountOverviewPageLoader
31
     */
32
    private $overviewPageLoader;
33
34
    /**
35
     * @var AccountProfilePageLoader
36
     */
37
    private $profilePageLoader;
38
39
    /**
40
     * @var AbstractChangeCustomerProfileRoute
41
     */
42
    private $changeCustomerProfileRoute;
43
44
    /**
45
     * @var AbstractChangePasswordRoute
46
     */
47
    private $changePasswordRoute;
48
49
    /**
50
     * @var AbstractChangeEmailRoute
51
     */
52
    private $changeEmailRoute;
53
54
    /**
55
     * @var AbstractDeleteCustomerRoute
56
     */
57
    private $deleteCustomerRoute;
58
59
    public function __construct(
60
        AccountOverviewPageLoader $overviewPageLoader,
61
        AccountProfilePageLoader $profilePageLoader,
62
        AbstractChangeCustomerProfileRoute $changeCustomerProfileRoute,
63
        AbstractChangePasswordRoute $changePasswordRoute,
64
        AbstractChangeEmailRoute $changeEmailRoute,
65
        AbstractDeleteCustomerRoute $deleteCustomerRoute
66
    ) {
67
        $this->overviewPageLoader = $overviewPageLoader;
68
        $this->profilePageLoader = $profilePageLoader;
69
        $this->changeCustomerProfileRoute = $changeCustomerProfileRoute;
70
        $this->changePasswordRoute = $changePasswordRoute;
71
        $this->changeEmailRoute = $changeEmailRoute;
72
        $this->deleteCustomerRoute = $deleteCustomerRoute;
73
    }
74
75
    /**
76
     * @Route("/account", name="frontend.account.home.page", methods={"GET"})
77
     *
78
     * @throws CustomerNotLoggedInException
79
     * @throws CategoryNotFoundException
80
     * @throws InconsistentCriteriaIdsException
81
     * @throws MissingRequestParameterException
82
     */
83
    public function index(Request $request, SalesChannelContext $context): Response
84
    {
85
        $this->denyAccessUnlessLoggedIn();
86
87
        $page = $this->overviewPageLoader->load($request, $context);
88
89
        return $this->renderStorefront('@Storefront/storefront/page/account/index.html.twig', ['page' => $page]);
90
    }
91
92
    /**
93
     * @Route("/account/profile", name="frontend.account.profile.page", methods={"GET"})
94
     *
95
     * @throws CustomerNotLoggedInException
96
     * @throws CategoryNotFoundException
97
     * @throws InconsistentCriteriaIdsException
98
     * @throws MissingRequestParameterException
99
     */
100
    public function profileOverview(Request $request, SalesChannelContext $context): Response
101
    {
102
        $this->denyAccessUnlessLoggedIn();
103
104
        $page = $this->profilePageLoader->load($request, $context);
105
106
        return $this->renderStorefront('@Storefront/storefront/page/account/profile/index.html.twig', [
107
            'page' => $page,
108
            'passwordFormViolation' => $request->get('passwordFormViolation'),
109
            'emailFormViolation' => $request->get('emailFormViolation'),
110
        ]);
111
    }
112
113
    /**
114
     * @Route("/account/profile", name="frontend.account.profile.save", methods={"POST"})
115
     *
116
     * @throws CustomerNotLoggedInException
117
     */
118
    public function saveProfile(RequestDataBag $data, SalesChannelContext $context): Response
119
    {
120
        $this->denyAccessUnlessLoggedIn();
121
122
        try {
123
            $this->changeCustomerProfileRoute->change($data, $context);
124
125
            $this->addFlash('success', $this->trans('account.profileUpdateSuccess'));
126
        } catch (ConstraintViolationException $formViolations) {
127
            return $this->forwardToRoute('frontend.account.profile.page', ['formViolations' => $formViolations]);
128
        } catch (\Exception $exception) {
129
            $this->addFlash('danger', $this->trans('error.message-default'));
130
        }
131
132
        return $this->redirectToRoute('frontend.account.profile.page');
133
    }
134
135
    /**
136
     * @Route("/account/profile/email", name="frontend.account.profile.email.save", methods={"POST"})
137
     *
138
     * @throws CustomerNotLoggedInException
139
     */
140
    public function saveEmail(RequestDataBag $data, SalesChannelContext $context): Response
141
    {
142
        $this->denyAccessUnlessLoggedIn();
143
144
        try {
145
            $this->changeEmailRoute->change($data->get('email')->toRequestDataBag(), $context);
146
147
            $this->addFlash('success', $this->trans('account.emailChangeSuccess'));
148
        } catch (ConstraintViolationException $formViolations) {
149
            $this->addFlash('danger', $this->trans('account.emailChangeNoSuccess'));
150
151
            return $this->forwardToRoute('frontend.account.profile.page', ['formViolations' => $formViolations, 'emailFormViolation' => true]);
152
        } catch (\Exception $exception) {
153
            $this->addFlash('danger', $this->trans('error.message-default'));
154
        }
155
156
        return $this->redirectToRoute('frontend.account.profile.page');
157
    }
158
159
    /**
160
     * @Route("/account/profile/password", name="frontend.account.profile.password.save", methods={"POST"})
161
     *
162
     * @throws CustomerNotLoggedInException
163
     */
164
    public function savePassword(RequestDataBag $data, SalesChannelContext $context): Response
165
    {
166
        $this->denyAccessUnlessLoggedIn();
167
168
        try {
169
            $this->changePasswordRoute->change($data->get('password')->toRequestDataBag(), $context);
170
171
            $this->addFlash('success', $this->trans('account.passwordChangeSuccess'));
172
        } catch (ConstraintViolationException $formViolations) {
173
            $this->addFlash('danger', $this->trans('account.passwordChangeNoSuccess'));
174
175
            return $this->forwardToRoute('frontend.account.profile.page', ['formViolations' => $formViolations, 'passwordFormViolation' => true]);
176
        }
177
178
        return $this->redirectToRoute('frontend.account.profile.page');
179
    }
180
181
    /**
182
     * @Route("/account/profile/delete", name="frontend.account.profile.delete", methods={"POST"})
183
     *
184
     * @throws CustomerNotLoggedInException
185
     */
186
    public function deleteProfile(Request $request, SalesChannelContext $context): Response
187
    {
188
        if (!Feature::isActive('FEATURE_NEXT_10077')) {
189
            return $this->redirectToRoute('frontend.home.page');
190
        }
191
192
        $this->denyAccessUnlessLoggedIn();
193
194
        try {
195
            $this->deleteCustomerRoute->delete($context);
196
            $this->addFlash('success', $this->trans('account.profileDeleteSuccessAlert'));
197
        } catch (\Exception $exception) {
198
            $this->addFlash('danger', $this->trans('error.message-default'));
199
        }
200
201
        if ($request->get('redirectTo') || $request->get('forwardTo')) {
202
            return $this->createActionResponse($request);
203
        }
204
205
        return $this->redirectToRoute('frontend.home.page');
206
    }
207
}
208