Passed
Push — master ( ec6c9c...99973f )
by Yannick
09:59 queued 01:34
created

PageController::topbarVisibility()   D

Complexity

Conditions 10
Paths 512

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 21
nc 512
nop 2
dl 0
loc 30
rs 4.1777
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chamilo\CoreBundle\Controller;
6
7
use Chamilo\CoreBundle\Helpers\AccessUrlHelper;
8
use Chamilo\CoreBundle\Repository\PageRepository;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\Routing\Attribute\Route;
14
use Symfony\Component\Security\Http\Attribute\IsGranted;
15
16
#[Route('/pages')]
17
class PageController extends AbstractController
18
{
19
    public function __construct(
20
        private readonly AccessUrlHelper $accessUrlHelper
21
    ) {}
22
23
    /**
24
     * Public endpoint consumed by the top bar (not logged-in).
25
     * It returns which entries should be visible for current URL and locale.
26
     *
27
     * GET /pages/_topbar-visibility?locale=es_spanish
28
     */
29
    #[Route('/_topbar-visibility', name: 'public_topbar_visibility', methods: ['GET'])]
30
    public function topbarVisibility(Request $request, PageRepository $pageRepo): JsonResponse
31
    {
32
        $accessUrl = $this->accessUrlHelper->getCurrent();
33
        $locale    = trim((string) $request->query->get('locale', ''));
34
35
        // We first try exact locale, then fallback to the 2-letter prefix.
36
        $prefix = $locale !== '' ? substr($locale, 0, 2) : '';
37
38
        $homeExact   = $locale !== '' ? $pageRepo->countByCategoryAndLocale($accessUrl, 'index', $locale) : 0;
39
        $homePrefix  = $prefix !== '' ? $pageRepo->countByCategoryAndLocalePrefix($accessUrl, 'index', $prefix) : 0;
40
        $home        = ($homeExact + $homePrefix) > 0;
41
42
        $faqExact    = $locale !== '' ? $pageRepo->countByCategoryAndLocale($accessUrl, 'faq', $locale) : 0;
43
        $faqPrefix   = $prefix !== '' ? $pageRepo->countByCategoryAndLocalePrefix($accessUrl, 'faq', $prefix) : 0;
44
        $faq         = ($faqExact + $faqPrefix) > 0;
45
46
        $demoExact   = $locale !== '' ? $pageRepo->countByCategoryAndLocale($accessUrl, 'demo', $locale) : 0;
47
        $demoPrefix  = $prefix !== '' ? $pageRepo->countByCategoryAndLocalePrefix($accessUrl, 'demo', $prefix) : 0;
48
        $demo        = ($demoExact + $demoPrefix) > 0;
49
50
        $contactExact  = $locale !== '' ? $pageRepo->countByCategoryAndLocale($accessUrl, 'contact', $locale) : 0;
51
        $contactPrefix = $prefix !== '' ? $pageRepo->countByCategoryAndLocalePrefix($accessUrl, 'contact', $prefix) : 0;
52
        $contact       = ($contactExact + $contactPrefix) > 0;
53
54
        return $this->json([
55
            'home'    => $home,
56
            'faq'     => $faq,
57
            'demo'    => $demo,
58
            'contact' => $contact,
59
        ]);
60
    }
61
62
    #[Route('/{slug}', name: 'public_page_show', requirements: ['slug' => '^[^_].+'], methods: ['GET'])]
63
    public function show(string $slug, PageRepository $pageRepo): Response
64
    {
65
        $accessUrl = $this->accessUrlHelper->getCurrent();
66
67
        $page = $pageRepo->findOneBy([
68
            'slug' => $slug,
69
            'enabled' => true,
70
            'url' => $accessUrl,
71
        ]);
72
73
        if (!$page) {
74
            throw $this->createNotFoundException('Page not found or not available for this access URL');
75
        }
76
77
        return $this->render('@ChamiloCore/Page/show.html.twig', [
78
            'page' => $page,
79
        ]);
80
    }
81
82
    #[Route('/{id}/preview', name: 'admin_page_preview', methods: ['GET'])]
83
    #[IsGranted('ROLE_ADMIN')]
84
    public function preview(int $id, PageRepository $pageRepo): Response
85
    {
86
        $page = $pageRepo->find($id);
87
88
        if (!$page) {
89
            throw $this->createNotFoundException('Page not found');
90
        }
91
92
        return $this->render('@ChamiloCore/Page/preview.html.twig', [
93
            'page' => $page,
94
        ]);
95
    }
96
}
97