Completed
Push — master ( 73ebab...d9bece )
by Andrea
63:43 queued 61:37
created

MenuController::getMenu()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 44
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 4.0005

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 30
cts 31
cp 0.9677
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 31
nc 5
nop 1
crap 4.0005
1
<?php
2
3
namespace Fi\CoreBundle\Controller;
4
5
/**
6
 * Menu controller.
7
 */
8
class MenuController extends FiCoreController
9
{
10
11 1
    protected function initGestionePermessi()
12
    {
13 1
        $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
14
15 1
        return $gestionepermessi;
16
    }
17
18 1
    public function generamenuAction()
19
    {
20 1
        $router = $this->get('router')->match('/')['_route'];
21
        //$homeurl = $this->generateUrl($router, array(), true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
22 1
        $rispostahome = array();
23 1
        $rispostahome[] = array('percorso' => $this->getUrlObject($this->container->getParameter('appname'), $router, ''),
24 1
            'nome' => $this->container->getParameter('appname'),
25 1
            'target' => '',
26
        );
27
28 1
        $em = $this->get('doctrine')->getManager();
29
        /* @var $qb \Doctrine\ORM\QueryBuilder */
30 1
        $qb = $em->createQueryBuilder();
31 1
        $qb->select(array('a'));
32 1
        $qb->from('FiCoreBundle:MenuApplicazione', 'a');
33 1
        $qb->where('a.attivo = :attivo and (a.padre is null or a.padre = 0)');
34 1
        $qb->setParameter('attivo', true);
35 1
        $qb->orderBy('a.padre', 'ASC');
36 1
        $qb->orderBy('a.ordine', 'ASC');
37 1
        $menu = $qb->getQuery()->getResult();
38
39 1
        $risposta = array_merge($rispostahome, $this->getMenu($menu));
40 1
        $webdir = $this->get('kernel')->getRootDir() . '/../web';
41 1
        $pathmanuale = '/uploads/manuale.pdf';
42 1
        $username = "";
43 1
        $urlLogout = "";
44
45 1
        if (file_exists($webdir . $pathmanuale)) {
46
            $risposta[] = array('percorso' => $this->getUrlObject('Manuale', $pathmanuale, '_blank'), 'nome' => 'Manuale', 'target' => '_blank');
47
        }
48
49 1
        if ($this->get('security.token_storage')->getToken()->getProviderKey() === 'secured_area') {
50
            $username = $this->getUser()->getUsername();
51
            $urlLogout = $this->generateUrl('fi_autenticazione_signout');
52
        }
53
54 1
        if ($this->get('security.token_storage')->getToken()->getProviderKey() === 'main') {
55 1
            $username = $this->get('security.token_storage')->getToken()->getUser()->getUsername();
56 1
            $urlLogout = $this->generateUrl('fos_user_security_logout');
57
        }
58
59 1
        $risposta[] = array('percorso' => $this->getUrlObject($username, '', ''), 'nome' => $username, 'target' => '',
60
            'sottolivello' => array(
61 1
                array('percorso' => $urlLogout, 'nome' => 'Logout', 'target' => ''),
62
            ),
63
        );
64
65 1
        return $this->render('FiCoreBundle:Menu:menu.html.twig', array('risposta' => $risposta));
66
    }
67
68 1
    protected function getMenu($menu)
69
    {
70 1
        $gestionepermessi = $this->initGestionePermessi();
71
72 1
        $risposta = array();
73 1
        $em = $this->get('doctrine')->getManager();
74
75 1
        foreach ($menu as $item) {
76 1
            $visualizzare = true;
77
78 1
            if ($item->isAutorizzazionerichiesta()) {
79
                $visualizzare = $gestionepermessi->sulmenu(array('modulo' => $item->getTag()));
80
            }
81
82 1
            if ($visualizzare) {
83 1
                $qb = $em->createQueryBuilder();
84 1
                $qb->select(array('a'));
85 1
                $qb->from('FiCoreBundle:MenuApplicazione', 'a');
86 1
                $qb->where('a.padre = :padre_id');
87 1
                $qb->andWhere('a.attivo = :attivo');
88 1
                $qb->orderBy('a.padre', 'ASC');
89 1
                $qb->orderBy('a.ordine', 'ASC');
90 1
                $qb->setParameter('padre_id', $item->getId());
91 1
                $qb->setParameter('attivo', true);
92 1
                $submenu = $qb->getQuery()->getResult();
93
94 1
                $sottomenutabelle = $this->getSubMenu($submenu);
95
96 1
                $risposta[] = array(
97 1
                    'percorso' => $this->getUrlObject($item->getNome(), $item->getPercorso(), $item->getTarget()),
98 1
                    'nome' => $item->getNome(),
99 1
                    'sottolivello' => $sottomenutabelle,
100 1
                    'target' => $item->getTarget(),
101 1
                    'notifiche' => $item->hasNotifiche(),
102 1
                    'tag' => $item->getTag(),
103 1
                    'percorsonotifiche' => $this->getUrlObject($item->getNome(), $item->getPercorsonotifiche(), ''),
104
                );
105 1
                unset($submenu);
106 1
                unset($sottomenutabelle);
107
            }
108
        }
109
110 1
        return $risposta;
111
    }
112
113 1
    protected function getSubMenu($submenu)
114
    {
115 1
        $gestionepermessi = $this->initGestionePermessi();
116
117 1
        $sottomenutabelle = array();
118 1
        foreach ($submenu as $subitem) {
119 1
            $visualizzare = true;
120 1
            if ($subitem->isAutorizzazionerichiesta()) {
121
                $visualizzare = $gestionepermessi->sulmenu(array('modulo' => $subitem->getTag()));
122
            }
123
124 1
            if ($visualizzare) {
125 1
                $vettoresottomenu = $this->getMenu(array($subitem));
126 1
                $sottomenu = $vettoresottomenu[0];
127
128 1
                if (isset($sottomenu['sottolivello']) && count($sottomenu['sottolivello']) > 0) {
129 1
                    $sottolivellomenu = array('sottolivello' => $sottomenu['sottolivello']);
130 1
                    $menuobj = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
131 1
                    $sottomenutabelle[] = array_merge($menuobj, $sottolivellomenu);
132
                } else {
133 1
                    $sottomenutabelle[] = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
134
                }
135
            }
136
        }
137
138 1
        return $sottomenutabelle;
139
    }
140
141 1
    protected function getUrlObject($nome, $percorso, $target)
142
    {
143 1
        if ($this->routeExists($percorso)) {
144 1
            return array('percorso' => $this->generateUrl($percorso), 'nome' => $nome, 'target' => $target);
145
        } else {
146 1
            return array('percorso' => $percorso, 'nome' => $nome, 'target' => $target);
147
        }
148
    }
149
150 1
    protected function routeExists($name)
151
    {
152 1
        $router = $this->container->get('router');
153
154 1
        if ((null === $router->getRouteCollection()->get($name)) ? false : true) {
155 1
            return true;
156
        } else {
157 1
            return false;
158
        }
159
    }
160
161
    protected function urlExists($name)
162
    {
163
        if ($this->checkUrl($name, false)) {
164
            return true;
165
        } else {
166
            if ($this->checkUrl($name, true)) {
167
                return true;
168
            } else {
169
                return false;
170
            }
171
        }
172
    }
173
174
    protected function checkUrl($name, $proxy)
175
    {
176
        $ch = curl_init($name);
177
178
        curl_setopt($ch, CURLOPT_URL, $name);
179
        if ($proxy) {
180
            curl_setopt($ch, CURLOPT_PROXY, 'proxyhttp.comune.intranet:8080');
181
        } else {
182
            curl_setopt($ch, CURLOPT_PROXY, null);
183
        }
184
        curl_setopt($ch, CURLOPT_NOBODY, true);
185
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
186
        curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout in seconds
187
        curl_exec($ch);
188
        $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
189
        if ($retcode === 200 || $retcode === 401) {
190
            $exist = true;
191
        } else {
192
            $exist = false;
193
        }
194
        curl_close($ch);
195
196
        return $exist;
197
    }
198
}
199