Completed
Push — master ( 93d030...ae88a9 )
by Julito
40:51
created

NavBuilder::profileMenu()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 72
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 43
nc 2
nop 2
dl 0
loc 72
rs 9.102
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Menu;
5
6
use Chamilo\PageBundle\Entity\Page;
7
use Chamilo\PageBundle\Entity\Site;
8
use Chamilo\UserBundle\Entity\User;
9
use Knp\Menu\FactoryInterface;
10
use Knp\Menu\ItemInterface;
11
use Symfony\Component\DependencyInjection\ContainerAware;
12
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
13
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
14
use Symfony\Component\Routing\RouterInterface;
15
16
/**
17
 * Class NavBuilder
18
 * @package Chamilo\CoreBundle\Menu
19
 */
20
class NavBuilder implements ContainerAwareInterface
21
{
22
    use ContainerAwareTrait;
23
24
    /**
25
     * @param array  $itemOptions The options given to the created menuItem
26
     * @param string $currentUri  The current URI
27
     *
28
     * @return \Knp\Menu\ItemInterface
29
     */
30
    public function createCategoryMenu(array $itemOptions = array(), $currentUri = null)
31
    {
32
        $factory = $this->container->get('knp_menu.factory');
33
        $menu = $factory->createItem('categories', $itemOptions);
34
35
        $this->buildCategoryMenu($menu, $itemOptions, $currentUri);
36
37
        return $menu;
38
    }
39
40
    /**
41
     * @param \Knp\Menu\ItemInterface $menu        The item to fill with $routes
42
     * @param array                   $options     The item options
43
     * @param string                  $currentUri  The current URI
44
     */
45
    public function buildCategoryMenu(ItemInterface $menu, array $options = array(), $currentUri = null)
46
    {
47
        //$categories = $this->categoryManager->getCategoryTree();
48
49
        //$this->fillMenu($menu, $categories, $options, $currentUri);
50
        $menu->addChild('home', array('route' => 'home'));
51
    }
52
53
    /**
54
     * Top menu left
55
     * @param FactoryInterface $factory
56
     * @param array $options
57
     * @return \Knp\Menu\ItemInterface
58
     */
59
    public function leftMenu(FactoryInterface $factory, array $options)
60
    {
61
        $checker = $this->container->get('security.authorization_checker');
62
        $translator = $this->container->get('translator');
63
64
        $menu = $factory->createItem('root');
65
        $menu->setChildrenAttribute('class', 'nav navbar-nav');
66
67
        $menu->addChild(
68
            $translator->trans('Home'),
69
            array('route' => 'home')
70
        );
71
72
        if ($checker->isGranted('IS_AUTHENTICATED_FULLY')) {
73
            $menu->addChild(
74
                $translator->trans('MyCourses'),
75
                array('route' => 'userportal')
76
            );
77
78
            $menu->addChild(
79
                $translator->trans('Calendar'),
80
                array(
81
                    'route' => 'main',
82
                    'routeParameters' => array(
83
                        'name' => 'calendar/agenda_js.php',
84
                    ),
85
                )
86
            );
87
88
            $menu->addChild(
89
                $translator->trans('Reporting'),
90
                array(
91
                    'route' => 'main',
92
                    'routeParameters' => array(
93
                        'name' => 'mySpace/index.php',
94
                    ),
95
                )
96
            );
97
98
            $menu->addChild(
99
                $translator->trans('Social'),
100
                array(
101
                    'route' => 'main',
102
                    'routeParameters' => array(
103
                        'name' => 'social/home.php',
104
                    ),
105
                )
106
            );
107
108
            if ($checker->isGranted('ROLE_ADMIN')) {
109
                $menu->addChild(
110
                    $translator->trans('Dashboard'),
111
                    array(
112
                        'route' => 'main',
113
                        'routeParameters' => array(
114
                            'name' => 'dashboard/index.php',
115
                        ),
116
                    )
117
                );
118
119
                $menu->addChild(
120
                    $translator->trans('Administration'),
121
                    array(
122
                        'route' => 'administration',
123
                    )
124
                );
125
            }
126
        }
127
128
        $categories = $this->container->get('faq.entity.category_repository')->retrieveActive();
129
        if ($categories) {
130
            $faq = $menu->addChild(
131
                'FAQ',
132
                [
133
                    'route' => 'faq_index',
134
                ]
135
            );
136
            /** @var Category $category */
137
            foreach ($categories as $category) {
138
                 $faq->addChild(
139
                    $category->getHeadline(),
140
                    array(
141
                        'route' => 'faq',
142
                        'routeParameters' => array(
143
                            'categorySlug' => $category->getSlug(),
144
                            'questionSlug' => '',
145
                        ),
146
                    )
147
                )->setAttribute('divider_append', true);
148
            }
149
        }
150
151
        // Getting site information
152
        $site = $this->container->get('sonata.page.site.selector');
153
        $host = $site->getRequestContext()->getHost();
154
        $siteManager = $this->container->get('sonata.page.manager.site');
155
        /** @var Site $site */
156
        $site = $siteManager->findOneBy(array(
157
            'host'    => array($host, 'localhost'),
158
            'enabled' => true,
159
        ));
160
161
        if ($site) {
162
            $pageManager = $this->container->get('sonata.page.manager.page');
163
164
            // Parents only of homepage
165
            $criteria = ['site' => $site, 'enabled' => true, 'parent' => 1];
166
            $pages = $pageManager->findBy($criteria);
167
168
            //$pages = $pageManager->loadPages($site);
169
            /** @var Page $page */
170
            foreach ($pages as $page) {
171
                /*if ($page->getRouteName() !== 'page_slug') {
172
                    continue;
173
                }*/
174
175
                // Avoid home
176
                if ($page->getUrl() === '/') {
177
                    continue;
178
                }
179
180
                if (!$page->isCms()) {
181
                    continue;
182
                }
183
184
                $subMenu = $menu->addChild(
185
                    $page->getName(),
186
                    [
187
                        'route' => $page->getRouteName(),
188
                        'routeParameters' => [
189
                            'path' => $page->getUrl(),
190
                        ],
191
                    ]
192
                );
193
194
                /** @var Page $child */
195
                foreach ($page->getChildren() as $child) {
196
                    $subMenu->addChild(
197
                        $child->getName(),
198
                        array(
199
                            'route' => $page->getRouteName(),
200
                            'routeParameters' => array(
201
                                'path' => $child->getUrl(),
202
                            ),
203
                        )
204
                    )->setAttribute('divider_append', true);
205
                }
206
            }
207
        }
208
209
        return $menu;
210
    }
211
}
212