Completed
Push — master ( 754cfb...ce2f80 )
by Alex
03:12
created

PageController::indexAction()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 3 Features 1
Metric Value
c 9
b 3
f 1
dl 0
loc 29
rs 8.439
cc 6
eloc 16
nc 4
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 $this->render('OrbitaleCmsBundle:Front:index.html.twig', array(
60
            'pages' => $pages,
61
            'page' => $currentPage,
62
        ));
63
    }
64
65
    /**
66
     * Retrieves the page list based on slugs.
67
     * Also checks the hierarchy of the different pages.
68
     *
69
     * @param array $slugsArray
70
     *
71
     * @return Page[]
72
     */
73
    protected function getPages(array $slugsArray = array())
74
    {
75
        /** @var Page[] $pages */
76
        $pages = $this->get('orbitale_cms.page_repository')
77
            ->findFrontPages($slugsArray, $this->request->getHost(), $this->request->getLocale())
78
        ;
79
80
        if (!count($pages) || (count($slugsArray) && count($pages) !== count($slugsArray))) {
81
            if (count($slugsArray)) {
82
                $msg = 'Page not found';
83
            } else {
84
                $msg = 'No homepage has been configured. Please check your existing pages or create a homepage in your application.';
85
            }
86
            throw $this->createNotFoundException($msg);
87
        }
88
89
        return $pages;
90
    }
91
92
    /**
93
     * Retrieves the current page based on page list and entered slugs.
94
     *
95
     * @param Page[] $pages
96
     * @param array  $slugsArray
97
     *
98
     * @return Page
99
     */
100
    protected function getCurrentPage(array $pages, array $slugsArray)
101
    {
102
        if (count($pages) === count($slugsArray)) {
103
            $currentPage = $this->getFinalTreeElement($slugsArray, $pages);
104
        } else {
105
            $currentPage = current($pages);
106
        }
107
108
        return $currentPage;
109
    }
110
}
111