Passed
Branch master (e828dd)
by giu
03:47
created

PagesController::display()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
c 1
b 0
f 0
nc 14
nop 1
dl 0
loc 25
ccs 14
cts 16
cp 0.875
crap 8.125
rs 8.4444
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13
 * @link      https://cakephp.org CakePHP(tm) Project
14
 * @since     0.2.9
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
16
 */
17
namespace App\Controller;
18
19
use Cake\Core\Configure;
20
use Cake\Http\Exception\ForbiddenException;
21
use Cake\Http\Exception\NotFoundException;
22
use Cake\Http\Response;
23
use Cake\View\Exception\MissingTemplateException;
24
25
/**
26
 * Static content controller
27
 *
28
 * This controller will render views from templates/Pages/
29
 *
30
 * @link https://book.cakephp.org/4/en/controllers/pages-controller.html
31
 */
32
class PagesController extends AppController
33
{
34
    /**
35
     * Displays a view
36
     *
37
     * @param string ...$path Path segments.
38
     * @return \Cake\Http\Response|null
39
     * @throws \Cake\Http\Exception\ForbiddenException When a directory traversal attempt.
40
     * @throws \Cake\View\Exception\MissingTemplateException When the view file could not
41
     *   be found and in debug mode.
42
     * @throws \Cake\Http\Exception\NotFoundException When the view file could not
43
     *   be found and not in debug mode.
44
     * @throws \Cake\View\Exception\MissingTemplateException In debug mode.
45
     */
46 4
    public function display(string ...$path): ?Response
47
    {
48 4
        if (!$path) {
49
            return $this->redirect('/');
50
        }
51 4
        if (in_array('..', $path, true) || in_array('.', $path, true)) {
52 1
            throw new ForbiddenException();
53
        }
54 3
        $page = $subpage = null;
55
56 3
        if (!empty($path[0])) {
57 3
            $page = $path[0];
58
        }
59 3
        if (!empty($path[1])) {
60
            $subpage = $path[1];
61
        }
62 3
        $this->set(compact('page', 'subpage'));
63
64
        try {
65 3
            return $this->render(implode('/', $path));
66 2
        } catch (MissingTemplateException $exception) {
67 2
            if (Configure::read('debug')) {
68 1
                throw $exception;
69
            }
70 1
            throw new NotFoundException();
71
        }
72
    }
73
}
74