AdminProfileController::profile()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 28
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 40
ccs 0
cts 35
cp 0
crap 30
rs 9.1608
1
<?php
2
3
namespace App\Controller\Admin;
4
5
use App\Repository\UserRepository;
6
use Psr\Log\LoggerInterface;
7
use Ramsey\Uuid\Uuid;
8
use App\Entity\User;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Messenger\MessageBusInterface;
13
use Symfony\Component\Routing\Annotation\Route;
14
use Symfony\Component\Form\FormError;
15
use Symfony\Component\HttpFoundation\RedirectResponse;
16
use App\DTO\ProfileDTO;
17
use App\Form\Type\UserProfileType;
18
use App\Command\UpdateUserProfileCommand;
19
20
class AdminProfileController extends AbstractController
21
{
22
    /**
23
     * @var MessageBusInterface
24
     */
25
    private $commandBus;
26
    /**
27
     * @var LoggerInterface
28
     */
29
    private $logger;
30
    /**
31
     * @var UserRepository
32
     */
33
    private $repository;
34
35
    /**
36
     * @param MessageBusInterface $commandBus
37
     * @param LoggerInterface $logger
38
     * @param UserRepository $repository
39
     */
40
    public function __construct(
41
        MessageBusInterface $commandBus,
42
        LoggerInterface $logger,
43
        UserRepository $repository
44
    )
45
    {
46
        $this->commandBus = $commandBus;
47
        $this->logger = $logger;
48
        $this->repository = $repository;
49
    }
50
51
    /**
52
     * @Route("/admin/profile", name="admin_profile", methods={"GET","POST"})
53
     * 
54
     * @param Request $rawRequest
55
     * @return Response|RedirectResponse
56
     */
57
    public function profile(Request $rawRequest): Response
58
    {
59
        /** @var User */
60
        $user = $this->getUser();
61
62
        if (empty($user)) {
63
            return $this->redirectToRoute('logout');
64
        }
65
66
        $profileDTO = new ProfileDTO(
67
            $user->getFirstName(),
68
            $user->getLastName(),
69
            $user->getEmail(),
70
            null
71
        );
72
        $form = $this->createForm(\App\Form\Type\UserProfileType::class, $profileDTO);
73
        $form->handleRequest($rawRequest);
74
75
        try {
76
            if ($form->isSubmitted() && $form->isValid()) {
77
                $profileDTO = $form->getData();
78
                $command = new UpdateUserProfileCommand(
79
                    $user->getId(),
80
                    $profileDTO->getFirstName(),
81
                    $profileDTO->getLastName(),
82
                    $profileDTO->getEmail(),
83
                    $profileDTO->getPlainPassword()
84
                );
85
                $this->commandBus->dispatch($command);
86
                $this->addFlash('success','Your changes were saved!');
87
                return $this->redirectToRoute('admin_profile');
88
            }
89
        } catch (\Exception $e) {
90
            $this->addFlash('danger','Error while saving changes');
91
            $error = new FormError("There is an error: ".$e->getMessage());
92
            $form->addError($error);
93
        }
94
95
        return $this->render('admin/profile.html.twig', [
96
            'form' => $form->createView()
97
        ]);
98
    }
99
}
100