MenuController::checkUrl()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 23
ccs 0
cts 16
cp 0
rs 9.7
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 20
1
<?php
2
3
namespace Fi\CoreBundle\Controller;
4
5
/**
6
 * Menu controller.
7
 */
8
class MenuController extends FiCoreController
9
{
10
11 13
    protected function initGestionePermessi()
12
    {
13 13
        $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
14
15 13
        return $gestionepermessi;
16
    }
17
18 13
    public function generamenuAction()
19
    {
20 13
        $router = $this->get('router')->match('/')['_route'];
21 13
        $rispostahome = array();
22 13
        $rispostahome[] = array('percorso' => $this->getUrlObject($this->container->getParameter('appname'), $router, ''),
23 13
            'nome' => 'Home',
24 13
            'target' => '',
25
        );
26
27 13
        $em = $this->get('doctrine')->getManager();
28
        /* @var $qb \Doctrine\ORM\QueryBuilder */
29 13
        $qb = $em->createQueryBuilder();
30 13
        $qb->select(array('a'));
31 13
        $qb->from('FiCoreBundle:MenuApplicazione', 'a');
32 13
        $qb->where('a.attivo = :attivo and (a.padre is null or a.padre = 0)');
33 13
        $qb->setParameter('attivo', true);
34 13
        $qb->orderBy('a.padre', 'ASC');
35 13
        $qb->orderBy('a.ordine', 'ASC');
36 13
        $menu = $qb->getQuery()->useQueryCache(true)->useResultCache(true, null, 'MenuApplicazione')->getResult();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\AbstractQuery::useResultCache() has been deprecated: 2.7 Use {@see enableResultCache} and {@see disableResultCache} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

36
        $menu = /** @scrutinizer ignore-deprecated */ $qb->getQuery()->useQueryCache(true)->useResultCache(true, null, 'MenuApplicazione')->getResult();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

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