CategoryController::indexAction()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 27
nc 2
nop 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 Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
class CategoryController extends AbstractCmsController
18
{
19
    /**
20
     * @param string  $slugs
21
     * @param Request $request
22
     *
23
     * @return Response
24
     */
25
    public function indexAction(string $slugs = '', Request $request): Response
26
    {
27
        if (preg_match('#/$#', $slugs)) {
28
            return $this->redirect($this->generateUrl('orbitale_cms_category', ['slugs' => rtrim($slugs, '/')]));
29
        }
30
31
        $slugsArray = preg_split('~/~', $slugs, -1, PREG_SPLIT_NO_EMPTY);
32
33
        $categories = $this->get('orbitale_cms.category_repository')->findFrontCategories($slugsArray);
34
35
        $category = $this->getFinalTreeElement($slugsArray, $categories);
36
37
        $validOrderFields = ['createdAt', 'title', 'content'];
38
39
        $limit   = $request->query->get('limit', 10);
40
        $page    = $request->query->get('page', 1);
41
        $orderBy = $request->query->get('order_by', current($validOrderFields));
42
        $order   = $request->query->get('order', 'asc');
43
44
        $pages = $this->get('orbitale_cms.page_repository')->findByCategory(
45
            $category,
46
            $order,
47
            $orderBy,
48
            $page,
49
            $limit
50
        )
51
        ;
52
53
        return $this->render('@OrbitaleCms/Front/category.html.twig', [
54
            'category'   => $category,
55
            'categories' => $categories,
56
            'pages'      => $pages,
57
            'pagesCount' => count($pages),
58
            'filters'    => [
59
                'page'    => $page,
60
                'limit'   => $limit,
61
                'orderBy' => $orderBy,
62
                'order'   => $order,
63
            ],
64
        ]);
65
    }
66
}
67