Completed
Push — master ( 28ddb0...754cfb )
by Alex
03:13
created

PageController::indexAction()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 3 Features 1
Metric Value
c 8
b 3
f 1
dl 0
loc 32
rs 6.7272
cc 7
eloc 18
nc 5
nop 3
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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
19
class PageController extends AbstractCmsController
20
{
21
    /**
22
     * @var Request
23
     */
24
    protected $request;
25
26
    /**
27
     * @Route("/{slugs}", name="orbitale_cms_page", requirements={"slugs": "([a-zA-Z0-9_-]+\/?)*"}, defaults={"slugs": ""})
28
     *
29
     * @param Request     $request
30
     * @param string      $slugs
31
     * @param string|null $_locale
32
     *
33
     * @return Response
34
     */
35
    public function indexAction(Request $request, $slugs = '', $_locale = null)
1 ignored issue
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
36
    {
37
        if (preg_match('~/$~', $slugs)) {
38
            return $this->redirect($this->generateUrl('orbitale_cms_page', array('slugs' => rtrim($slugs, '/'))));
39
        }
40
41
        $this->request = $request;
42
        $this->request->setLocale($_locale ?: $this->request->getLocale());
43
44
        $slugsArray = preg_split('~/~', $slugs, -1, PREG_SPLIT_NO_EMPTY);
45
46
        $pages = $this->getPages($slugsArray);
47
48
        $currentPage = $this->getCurrentPage($pages, $slugsArray);
49
50
        if ($currentPage->isHomepage() && strlen($slugs)) {
51
            $params = array('slugs' => '');
52
            if ($currentPage->getLocale()) {
53
                // Force locale if the Page has one
54
                $params['_locale'] = $currentPage->getLocale();
55
            }
56
            return $this->redirect($this->generateUrl('orbitale_cms_page', $params));
57
        }
58
59
        return $currentPage instanceof Response
60
            ? $currentPage
61
            : $this->render('OrbitaleCmsBundle:Front:index.html.twig', array(
62
                'pages' => $pages,
63
                'page' => $currentPage,
64
            ))
65
        ;
66
    }
67
68
    /**
69
     * Retrieves the page list based on slugs.
70
     * Also checks the hierarchy of the different pages.
71
     *
72
     * @param array $slugsArray
73
     *
74
     * @return Page[]
75
     */
76
    protected function getPages(array $slugsArray = array())
77
    {
78
        /** @var Page[] $pages */
79
        $pages = $this->get('orbitale_cms.page_repository')
80
            ->findFrontPages($slugsArray, $this->request->getHost(), $this->request->getLocale())
81
        ;
82
83
        if (!count($pages) || (count($slugsArray) && count($pages) !== count($slugsArray))) {
84
            if (count($slugsArray)) {
85
                $msg = 'Page not found';
86
            } else {
87
                $msg = 'No homepage has been configured. Please check your existing pages or create a homepage in your application.';
88
            }
89
            throw $this->createNotFoundException($msg);
90
        }
91
92
        return $pages;
93
    }
94
95
    /**
96
     * Retrieves the current page based on page list and entered slugs.
97
     *
98
     * @param Page[] $pages
99
     * @param array  $slugsArray
100
     *
101
     * @return Page
102
     */
103
    protected function getCurrentPage(array $pages, array $slugsArray)
104
    {
105
        if (count($pages) === count($slugsArray)) {
106
            $currentPage = $this->getFinalTreeElement($slugsArray, $pages);
107
        } else {
108
            $currentPage = current($pages);
109
        }
110
111
        return $currentPage;
112
    }
113
}
114