PageController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 91
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 33 6
B getPages() 0 15 5
A getCurrentPage() 0 10 2
1
<?php
2
3
/*
4
* This file is part of the OrbitaleCmsBundle package.
5
*
6
* (c) Alexandre Rock Ancelet <[email protected]>
7
*
8
* For the full copyright and license information, please view the LICENSE
9
* file that was distributed with this source code.
10
*/
11
12
namespace Orbitale\Bundle\CmsBundle\Controller;
13
14
use Orbitale\Bundle\CmsBundle\Entity\Page;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
18
class PageController extends AbstractCmsController
19
{
20
    /**
21
     * @var Request
22
     */
23
    protected $request;
24
25
    /**
26
     * @param Request     $request
27
     * @param string      $slugs
28
     * @param string|null $_locale
29
     *
30
     * @return Response
31
     */
32
    public function indexAction(Request $request, string $slugs = '', string $_locale = null): Response
33
    {
34
        if (preg_match('~/$~', $slugs)) {
35
            return $this->redirect($this->generateUrl('orbitale_cms_page', ['slugs' => rtrim($slugs, '/')]));
36
        }
37
38
        $this->request = $request;
39
        $this->request->setLocale($_locale ?: $this->request->getLocale());
40
41
        $slugsArray = preg_split('~/~', $slugs, -1, PREG_SPLIT_NO_EMPTY);
42
43
        $pages = $this->getPages($slugsArray);
44
45
        $currentPage = $this->getCurrentPage($pages, $slugsArray);
46
47
        // If we have slugs and the current page is homepage,
48
        //  we redirect to homepage for "better" url and SEO management.
49
        // Example: if "/home" is a homepage, "/home" url is redirected to "/".
50
        if ($slugs && $currentPage->isHomepage()) {
51
            $params = ['slugs' => ''];
52
            if ($currentPage->getLocale()) {
53
                // Force locale if the Page has one
54
                $params['_locale'] = $currentPage->getLocale();
55
            }
56
57
            return $this->redirect($this->generateUrl('orbitale_cms_page', $params));
58
        }
59
60
        return $this->render('@OrbitaleCms/Front/index.html.twig', [
61
            'pages' => $pages,
62
            'page'  => $currentPage,
63
        ]);
64
    }
65
66
    /**
67
     * Retrieves the page list based on slugs.
68
     * Also checks the hierarchy of the different pages.
69
     *
70
     * @param array $slugsArray
71
     *
72
     * @return Page[]
73
     */
74
    protected function getPages(array $slugsArray = [])
75
    {
76
        /** @var Page[] $pages */
77
        $pages = $this->get('orbitale_cms.page_repository')
78
            ->findFrontPages($slugsArray, $this->request->getHost(), $this->request->getLocale())
79
        ;
80
81
        if (!count($pages) || (count($slugsArray) && count($pages) !== count($slugsArray))) {
82
            throw $this->createNotFoundException(count($slugsArray)
83
                ? 'Page not found'
84
                : 'No homepage has been configured. Please check your existing pages or create a homepage in your application.');
85
        }
86
87
        return $pages;
88
    }
89
90
    /**
91
     * Retrieves the current page based on page list and entered slugs.
92
     *
93
     * @param Page[] $pages
94
     * @param array  $slugsArray
95
     *
96
     * @return Page
97
     */
98
    protected function getCurrentPage(array $pages, array $slugsArray): Page
99
    {
100
        if (count($pages) === count($slugsArray)) {
101
            $currentPage = $this->getFinalTreeElement($slugsArray, $pages);
102
        } else {
103
            $currentPage = current($pages);
104
        }
105
106
        return $currentPage;
107
    }
108
}
109