Passed
Push — main ( 674e66...68245a )
by Axel
04:12
created

AccountController::deleteMyAccount()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 47
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 28
nc 8
nop 5
dl 0
loc 47
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\UsersBundle\Controller;
15
16
use Locale;
17
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Intl\Languages;
21
use Symfony\Component\Routing\Annotation\Route;
22
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
23
use Symfony\Component\Security\Http\Attribute\IsGranted;
24
use Symfony\Contracts\Translation\TranslatorInterface;
25
use Zikula\Bundle\CoreBundle\Api\ApiInterface\LocaleApiInterface;
26
use Zikula\UsersBundle\Api\ApiInterface\CurrentUserApiInterface;
0 ignored issues
show
Bug introduced by
The type Zikula\UsersBundle\Api\A...CurrentUserApiInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Zikula\UsersBundle\Entity\User;
28
use Zikula\UsersBundle\Event\DeleteUserFormPostCreatedEvent;
0 ignored issues
show
Bug introduced by
The type Zikula\UsersBundle\Event...serFormPostCreatedEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use Zikula\UsersBundle\Event\DeleteUserFormPostValidatedEvent;
0 ignored issues
show
Bug introduced by
The type Zikula\UsersBundle\Event...rFormPostValidatedEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use Zikula\UsersBundle\Form\Type\ChangeLanguageType;
31
use Zikula\UsersBundle\Helper\DeleteHelper;
32
use Zikula\UsersBundle\Repository\UserRepositoryInterface;
33
use Zikula\UsersBundle\UsersConstant;
34
35
#[Route('/account')]
36
#[IsGranted('ROLE_USER')]
37
class AccountController extends AbstractController
38
{
39
    public function __construct(
40
        private readonly TranslatorInterface $translator,
41
        private readonly bool $displayGraphics,
42
        private readonly bool $allowSelfDeletion
43
    ) {
44
    }
45
46
    /**
47
     * @throws AccessDeniedException Thrown if the user isn't logged in
48
     */
49
    #[Route('', name: 'zikulausersbundle_account_menu')]
50
    public function menu(): Response
51
    {
52
        return $this->redirect('/');
53
    }
54
55
    /**
56
     * @throws AccessDeniedException Thrown if the user isn't logged in
57
     */
58
    #[Route('/change-language', name: 'zikulausersbundle_account_changelanguage')]
59
    public function changeLanguage(
60
        Request $request,
61
        CurrentUserApiInterface $currentUserApi,
62
        UserRepositoryInterface $userRepository,
63
        LocaleApiInterface $localeApi
64
    ): Response {
65
        if (!$currentUserApi->isLoggedIn()) {
66
            throw new AccessDeniedException();
67
        }
68
        $form = $this->createForm(ChangeLanguageType::class, [
69
            'locale' => $currentUserApi->get('locale'),
70
        ]);
71
        $form->handleRequest($request);
72
        if ($form->isSubmitted()) {
73
            $locale = $this->getParameter('locale');
74
            if ($form->get('submit')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not exist on Symfony\Component\Form\FormInterface. It seems like you code against a sub-type of Symfony\Component\Form\FormInterface such as Symfony\Component\Form\SubmitButton. ( Ignorable by Annotation )

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

74
            if ($form->get('submit')->/** @scrutinizer ignore-call */ isClicked()) {
Loading history...
75
                $data = $form->getData();
76
                $locale = !empty($data['locale']) ? $data['locale'] : $locale;
77
                /** @var User $userEntity */
78
                $userEntity = $userRepository->find($currentUserApi->get('uid'));
79
                $userEntity->setLocale($locale);
80
                $userRepository->persistAndFlush($userEntity);
81
                if ($request->hasSession() && ($session = $request->getSession())) {
82
                    $session->set('_locale', $locale);
83
                }
84
                Locale::setDefault($locale);
0 ignored issues
show
Bug introduced by
It seems like $locale can also be of type array; however, parameter $locale of Locale::setDefault() does only seem to accept string, 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

84
                Locale::setDefault(/** @scrutinizer ignore-type */ $locale);
Loading history...
85
                $langText = Languages::getName($locale);
86
                $this->addFlash('success', $this->translator->trans('Language changed to %lang%', ['%lang%' => $langText], 'messages', $locale));
87
            } elseif ($form->get('cancel')->isClicked()) {
88
                $this->addFlash('status', 'Operation cancelled.');
89
            }
90
91
            return $this->redirectToRoute('zikulausersbundle_account_menu', ['_locale' => $locale]);
92
        }
93
94
        return $this->render('@ZikulaUsers/Account/changeLanguage.html.twig', [
95
            'form' => $form->createView(),
96
            'multilingual' => $localeApi->multilingual(),
97
        ]);
98
    }
99
}
100