Lines
100.00%
20 / 20
Functions and Methods
100.00%
1 / 1
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ProfileController | 100.00% | 20 / 20 | 100.00% | 1 / 1 | 5 | 100.00% | 1 / 1 | |||
| __invoke | 100.00% | 20 / 20 | 100.00% | 1 / 1 | 5 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace App\Controller; | |
| 4 | ||
| 5 | use App\Enum\ProfilePart; | |
| 6 | use App\Enum\UserRole; | |
| 7 | use App\Form\ProfileVisibilityType; | |
| 8 | use App\Security\ApiTokenStore; | |
| 9 | use Doctrine\ORM\EntityManagerInterface; | |
| 10 | use Symfony\Component\HttpFoundation\Request; | |
| 11 | use Symfony\Component\HttpFoundation\Response; | |
| 12 | use Symfony\Component\Routing\Attribute\Route; | |
| 13 | use Symfony\Component\Security\Http\Attribute\IsGranted; | |
| 14 | ||
| 15 | #[Route('/profile', name: 'app_profile', methods: ['GET', 'POST'])] | |
| 16 | #[IsGranted(UserRole::USER->value)] | |
| 17 | class ProfileController extends BaseController | |
| 18 | { | |
| 19 | public function __invoke(Request $request, ApiTokenStore $apiTokenStore, EntityManagerInterface $entityManager): Response | |
| 20 | { | |
| 21 | $user = $this->getUser(); | |
| 22 | $form = $this->createForm(ProfileVisibilityType::class, ['visibleParts' => $user->getVisibleParts()]); | |
| 23 | $form->handleRequest($request); | |
| 24 | ||
| 25 | if ($form->isSubmitted() && $form->isValid()) { | |
| 26 | /** @var list<ProfilePart> $parts */ | |
| 27 | $parts = $form->get('visibleParts')->getData(); | |
| 28 | $user->chooseVisibleParts($parts, new \DateTimeImmutable()); | |
| 29 | $entityManager->flush(); | |
| 30 | ||
| 31 | $this->addFlash('success', 'profile.visibility.saved'); | |
| 32 | ||
| 33 | return $this->redirectToRoute('app_profile', [], Response::HTTP_SEE_OTHER); | |
| 34 | } | |
| 35 | ||
| 36 | $partHelp = []; | |
| 37 | foreach (ProfilePart::cases() as $part) { | |
| 38 | $partHelp[$part->value] = $part->help(); | |
| 39 | } | |
| 40 | ||
| 41 | return $this->render('default/profile.html.twig', [ | |
| 42 | 'visibilityForm' => $form, | |
| 43 | 'partHelp' => $partHelp, | |
| 44 | 'agentPageVisible' => $user->isVisible(ProfilePart::AGENT), | |
| 45 | 'apiToken' => $this->isGranted(UserRole::ADMIN->value) | |
| 46 | ? $apiTokenStore->get($request->getSession(), $user) | |
| 47 | : null, | |
| 48 | ]); | |
| 49 | } | |
| 50 | } |