Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| DefaultController | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Controller; |
| 6 | |
| 7 | use App\Repository\MaxfieldRepository; |
| 8 | use Symfony\Component\HttpFoundation\Request; |
| 9 | use Symfony\Component\HttpFoundation\Response; |
| 10 | use Symfony\Component\Routing\Attribute\Route; |
| 11 | use UnexpectedValueException; |
| 12 | |
| 13 | class DefaultController extends BaseController |
| 14 | { |
| 15 | public function __construct(private readonly MaxfieldRepository $maxfieldRepository) {} |
| 16 | |
| 17 | #[Route('/', name: 'default', methods: ['GET'])] |
| 18 | public function index(Request $request): Response |
| 19 | { |
| 20 | $user = $this->getUser(); |
| 21 | |
| 22 | $favourites = $user ? $user->getFavourites() : []; |
| 23 | $searchTerm = $request->query->get('q'); |
| 24 | $maxfields = $this->maxfieldRepository->search($searchTerm); |
| 25 | $template = 'index'; |
| 26 | |
| 27 | $partial = $request->query->get('partial'); |
| 28 | |
| 29 | if ($partial) { |
| 30 | if (in_array( |
| 31 | $partial, |
| 32 | ['searchPreview', 'favourites', 'contentList'], |
| 33 | true |
| 34 | ) |
| 35 | ) { |
| 36 | $template = '_' . $partial; |
| 37 | } else { |
| 38 | throw new UnexpectedValueException('Invalid partial'); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return $this->render( |
| 43 | sprintf('default/%s.html.twig', $template), |
| 44 | [ |
| 45 | 'maxfields' => $maxfields, |
| 46 | 'favourites' => $favourites, |
| 47 | 'searchTerm' => $searchTerm, |
| 48 | ] |
| 49 | ); |
| 50 | } |
| 51 | } |