Passed
Push — master ( 74531a...944a70 )
by Andrea
17:56
created

MenuController   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Test Coverage

Coverage 73.6%

Importance

Changes 0
Metric Value
dl 0
loc 188
ccs 92
cts 125
cp 0.736
rs 10
c 0
b 0
f 0
wmc 27

8 Methods

Rating   Name   Duplication   Size   Complexity  
B getSubMenu() 0 26 6
B checkUrl() 0 23 4
B getMenu() 0 43 4
A initGestionePermessi() 0 5 1
A routeExists() 0 8 3
A getUrlObject() 0 6 2
B generamenuAction() 0 47 4
A urlExists() 0 9 3
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");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ficorebundle.gestionepermessi does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
14
15 13
        return $gestionepermessi;
16
    }
17
18 13
    public function generamenuAction()
19
    {
20 13
        $router = $this->get('router')->match('/')['_route'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
21 13
        $rispostahome = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
22 13
        $rispostahome[] = array('percorso' => $this->getUrlObject($this->container->getParameter('appname'), $router, ''),
23 13
            'nome' => $this->container->getParameter('appname'),
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()->getResult();
37
38 13
        $risposta = array_merge($rispostahome, $this->getMenu($menu));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
39 13
        $webdir = $this->get('kernel')->getRootDir() . '/../web';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
40 13
        $pathmanuale = '/uploads/manuale.pdf';
41 13
        $username = "";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
42 13
        $urlLogout = "";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
55 13
            $urlLogout = $this->generateUrl('fos_user_security_logout');
56 13
        }
57
58 13
        $risposta[] = array('percorso' => $this->getUrlObject($username, '', ''), 'nome' => $username, 'target' => '',
59
            'sottolivello' => array(
60 13
                array('percorso' => $urlLogout, 'nome' => 'Logout', 'target' => ''),
61 13
            ),
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();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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()->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 13
            }
107 13
        }
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];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
126
127 13
                if (isset($sottomenu['sottolivello']) && count($sottomenu['sottolivello']) > 0) {
128 13
                    $sottolivellomenu = array('sottolivello' => $sottomenu['sottolivello']);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
129 13
                    $menuobj = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
130 13
                    $sottomenutabelle[] = array_merge($menuobj, $sottolivellomenu);
131 13
                } else {
132 13
                    $sottomenutabelle[] = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
133
                }
134 13
            }
135 13
        }
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