Passed
Push — master ( f21ca4...caf834 )
by
unknown
26:17 queued 17:12
created

PageController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chamilo\CoreBundle\Controller;
6
7
use Chamilo\CoreBundle\Repository\PageRepository;
8
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\Attribute\Route;
12
use Symfony\Component\Security\Http\Attribute\IsGranted;
13
14
#[Route('/pages')]
15
class PageController extends AbstractController
16
{
17
    public function __construct(
18
        private readonly AccessUrlHelper $accessUrlHelper
19
    ) {}
20
21
    #[Route('/{slug}', name: 'public_page_show', methods: ['GET'])]
22
    public function show(string $slug, PageRepository $pageRepo): Response
23
    {
24
        $accessUrl = $this->accessUrlHelper->getCurrent();
25
26
        $page = $pageRepo->findOneBy([
27
            'slug' => $slug,
28
            'enabled' => true,
29
            'url' => $accessUrl,
30
        ]);
31
32
        if (!$page) {
33
            throw $this->createNotFoundException('Page not found or not available for this access URL');
34
        }
35
36
        return $this->render('@ChamiloCore/Page/show.html.twig', [
37
            'page' => $page,
38
        ]);
39
    }
40
41
    #[Route('/{id}/preview', name: 'admin_page_preview', methods: ['GET'])]
42
    #[IsGranted('ROLE_ADMIN')]
43
    public function preview(int $id, PageRepository $pageRepo): Response
44
    {
45
        $page = $pageRepo->find($id);
46
47
        if (!$page) {
48
            throw $this->createNotFoundException('Page not found');
49
        }
50
51
        return $this->render('@ChamiloCore/Page/preview.html.twig', [
52
            'page' => $page,
53
        ]);
54
    }
55
}
56