Passed
Push — master ( 17e906...d315bd )
by Andrea
40:07 queued 35:20
created

MenuController::initGestionePermessi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Fi\CoreBundle\Controller;
4
5
/**
6
 * Menu controller.
7
 */
8
class MenuController extends FiCoreController
9
{
10
11
    protected function initGestionePermessi()
12
    {
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
        return $gestionepermessi;
16
    }
17
18
    public function generamenuAction()
19
    {
20
        $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
        $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
        $rispostahome[] = array('percorso' => $this->getUrlObject($this->container->getParameter('appname'), $router, ''),
23
            'nome' => $this->container->getParameter('appname'),
24
            'target' => '',
25
        );
26
27
        $em = $this->get('doctrine')->getManager();
28
        /* @var $qb \Doctrine\ORM\QueryBuilder */
29
        $qb = $em->createQueryBuilder();
30
        $qb->select(array('a'));
31
        $qb->from('FiCoreBundle:MenuApplicazione', 'a');
32
        $qb->where('a.attivo = :attivo and (a.padre is null or a.padre = 0)');
33
        $qb->setParameter('attivo', true);
34
        $qb->orderBy('a.padre', 'ASC');
35
        $qb->orderBy('a.ordine', 'ASC');
36
        $menu = $qb->getQuery()->getResult();
37
38
        $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
        $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
        $pathmanuale = '/uploads/manuale.pdf';
41
        $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
        $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
        if (file_exists($webdir . $pathmanuale)) {
45
            $risposta[] = array('percorso' => $this->getUrlObject('Manuale', $pathmanuale, '_blank'), 'nome' => 'Manuale', 'target' => '_blank');
46
        }
47
48
        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
        if ($this->get('security.token_storage')->getToken()->getProviderKey() === 'main') {
54
            $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
            $urlLogout = $this->generateUrl('fos_user_security_logout');
56
        }
57
58
        $risposta[] = array('percorso' => $this->getUrlObject($username, '', ''), 'nome' => $username, 'target' => '',
59
            'sottolivello' => array(
60
                array('percorso' => $urlLogout, 'nome' => 'Logout', 'target' => ''),
61
            ),
62
        );
63
64
        return $this->render('FiCoreBundle:Menu:menu.html.twig', array('risposta' => $risposta));
65
    }
66
67
    protected function getMenu($menu)
68
    {
69
        $gestionepermessi = $this->initGestionePermessi();
70
71
        $risposta = array();
72
        $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
        foreach ($menu as $item) {
75
            $visualizzare = true;
76
77
            if ($item->isAutorizzazionerichiesta()) {
78
                $visualizzare = $gestionepermessi->sulmenu(array('modulo' => $item->getTag()));
79
            }
80
81
            if ($visualizzare) {
82
                $qb = $em->createQueryBuilder();
83
                $qb->select(array('a'));
84
                $qb->from('FiCoreBundle:MenuApplicazione', 'a');
85
                $qb->where('a.padre = :padre_id');
86
                $qb->andWhere('a.attivo = :attivo');
87
                $qb->orderBy('a.padre', 'ASC');
88
                $qb->orderBy('a.ordine', 'ASC');
89
                $qb->setParameter('padre_id', $item->getId());
90
                $qb->setParameter('attivo', true);
91
                $submenu = $qb->getQuery()->getResult();
92
93
                $sottomenutabelle = $this->getSubMenu($submenu);
94
95
                $risposta[] = array(
96
                    'percorso' => $this->getUrlObject($item->getNome(), $item->getPercorso(), $item->getTarget()),
97
                    'nome' => $item->getNome(),
98
                    'sottolivello' => $sottomenutabelle,
99
                    'target' => $item->getTarget(),
100
                    'notifiche' => $item->hasNotifiche(),
101
                    'tag' => $item->getTag(),
102
                    'percorsonotifiche' => $this->getUrlObject($item->getNome(), $item->getPercorsonotifiche(), ''),
103
                );
104
                unset($submenu);
105
                unset($sottomenutabelle);
106
            }
107
        }
108
109
        return $risposta;
110
    }
111
112
    protected function getSubMenu($submenu)
113
    {
114
        $gestionepermessi = $this->initGestionePermessi();
115
116
        $sottomenutabelle = array();
117
        foreach ($submenu as $subitem) {
118
            $visualizzare = true;
119
            if ($subitem->isAutorizzazionerichiesta()) {
120
                $visualizzare = $gestionepermessi->sulmenu(array('modulo' => $subitem->getTag()));
121
            }
122
123
            if ($visualizzare) {
124
                $vettoresottomenu = $this->getMenu(array($subitem));
125
                $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
                if (isset($sottomenu['sottolivello']) && count($sottomenu['sottolivello']) > 0) {
128
                    $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
                    $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
                    $sottomenutabelle[] = array_merge($menuobj, $sottolivellomenu);
131
                } else {
132
                    $sottomenutabelle[] = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
133
                }
134
            }
135
        }
136
137
        return $sottomenutabelle;
138
    }
139
140
    protected function getUrlObject($nome, $percorso, $target)
141
    {
142
        if ($this->routeExists($percorso)) {
143
            return array('percorso' => $this->generateUrl($percorso), 'nome' => $nome, 'target' => $target);
144
        } else {
145
            return array('percorso' => $percorso, 'nome' => $nome, 'target' => $target);
146
        }
147
    }
148
149
    protected function routeExists($name)
150
    {
151
        $router = $this->container->get('router');
152
153
        if ((null === $router->getRouteCollection()->get($name)) ? false : true) {
154
            return true;
155
        } else {
156
            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