Completed
Push — master ( 0de7db...d3a1aa )
by Andrea
24:21 queued 03:31
created

MenuController::getMenu()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 44
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 4.0032

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 32
cts 34
cp 0.9412
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 31
nc 5
nop 1
crap 4.0032
1
<?php
2
3
namespace Fi\CoreBundle\Controller;
4
5
/**
6
 * Menu controller.
7
 */
8
class MenuController extends MenuApplicazioneController
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
23 1
        $risposta[] = array('percorso' => $this->getUrlObject($this->container->getParameter('appname'), $router, ''),
0 ignored issues
show
Coding Style Comprehensibility introduced by
$risposta was never initialized. Although not strictly required by PHP, it is generally a good practice to add $risposta = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
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($risposta, $this->getMenu($menu));
40 1
        $webdir = $this->get('kernel')->getRootDir() . '/../web';
41 1
        $pathmanuale = '/uploads/manuale.pdf';
42
43 1
        if (file_exists($webdir . $pathmanuale)) {
44
            $risposta[] = array('percorso' => $this->getUrlObject('Manuale', $pathmanuale, '_blank'), 'nome' => 'Manuale', 'target' => '_blank');
45
        }
46
47 1
        if ($this->get('security.token_storage')->getToken()->getProviderKey() === 'secured_area') {
48
            $username = $this->getUser()->getUsername();
49
            $urlLogout = $this->generateUrl('fi_autenticazione_signout');
50
        }
51
52 1
        if ($this->get('security.token_storage')->getToken()->getProviderKey() === 'main') {
53 1
            $username = $this->get('security.token_storage')->getToken()->getUser()->getUsername();
54 1
            $urlLogout = $this->generateUrl('fos_user_security_logout');
55 1
        }
56
57 1
        $risposta[] = array('percorso' => $this->getUrlObject($username, '', ''), 'nome' => $username, 'target' => '',
0 ignored issues
show
Bug introduced by
The variable $username does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
58
            'sottolivello' => array(
59 1
                array('percorso' => $urlLogout, 'nome' => 'Logout', 'target' => ''),
0 ignored issues
show
Bug introduced by
The variable $urlLogout does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
60 1
            ),
61
        );
62
63 1
        return $this->render('FiCoreBundle:Menu:menu.html.twig', array('risposta' => $risposta));
64
    }
65
66 1
    protected function getMenu($menu)
67
    {
68 1
        $gestionepermessi = $this->initGestionePermessi();
69
70 1
        $risposta = array();
71 1
        $em = $this->get('doctrine')->getManager();
72
73 1
        foreach ($menu as $item) {
74 1
            $visualizzare = true;
75
76 1
            if ($item->isAutorizzazionerichiesta()) {
77
                $visualizzare = $gestionepermessi->sulmenu(array('modulo' => $item->getTag()));
78
            }
79
80 1
            if ($visualizzare) {
81 1
                $qb = $em->createQueryBuilder();
82 1
                $qb->select(array('a'));
83 1
                $qb->from('FiCoreBundle:MenuApplicazione', 'a');
84 1
                $qb->where('a.padre = :padre_id');
85 1
                $qb->andWhere('a.attivo = :attivo');
86 1
                $qb->orderBy('a.padre', 'ASC');
87 1
                $qb->orderBy('a.ordine', 'ASC');
88 1
                $qb->setParameter('padre_id', $item->getId());
89 1
                $qb->setParameter('attivo', true);
90 1
                $submenu = $qb->getQuery()->getResult();
91
92 1
                $sottomenutabelle = $this->getSubMenu($submenu);
93
94 1
                $risposta[] = array(
95 1
                    'percorso' => $this->getUrlObject($item->getNome(), $item->getPercorso(), $item->getTarget()),
96 1
                    'nome' => $item->getNome(),
97 1
                    'sottolivello' => $sottomenutabelle,
98 1
                    'target' => $item->getTarget(),
99 1
                    'notifiche' => $item->hasNotifiche(),
100 1
                    'tag' => $item->getTag(),
101 1
                    'percorsonotifiche' => $this->getUrlObject($item->getNome(), $item->getPercorsonotifiche(), ''),
102
                );
103 1
                unset($submenu);
104 1
                unset($sottomenutabelle);
105 1
            }
106 1
        }
107
108 1
        return $risposta;
109
    }
110
111 1
    protected function getSubMenu($submenu)
112
    {
113 1
        $gestionepermessi = $this->initGestionePermessi();
114
115 1
        $sottomenutabelle = array();
116 1
        foreach ($submenu as $subitem) {
117 1
            $visualizzare = true;
118 1
            if ($subitem->isAutorizzazionerichiesta()) {
119
                $visualizzare = $gestionepermessi->sulmenu(array('modulo' => $subitem->getTag()));
120
            }
121
122 1
            if ($visualizzare) {
123 1
                $vettoresottomenu = $this->getMenu(array($subitem));
124 1
                $sottomenu = $vettoresottomenu[0];
125
126 1
                if (isset($sottomenu['sottolivello']) && count($sottomenu['sottolivello']) > 0) {
127 1
                    $sottolivellomenu = array('sottolivello' => $sottomenu['sottolivello']);
128 1
                    $menuobj = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
129 1
                    $sottomenutabelle[] = array_merge($menuobj, $sottolivellomenu);
130 1
                } else {
131 1
                    $sottomenutabelle[] = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
132
                }
133 1
            }
134 1
        }
135
136 1
        return $sottomenutabelle;
137
    }
138
139 1
    protected function getUrlObject($nome, $percorso, $target)
140
    {
141 1
        if ($this->routeExists($percorso)) {
142 1
            return array('percorso' => $this->generateUrl($percorso), 'nome' => $nome, 'target' => $target);
143
        } else {
144 1
            return array('percorso' => $percorso, 'nome' => $nome, 'target' => $target);
145
        }
146
    }
147
148 1
    protected function routeExists($name)
149
    {
150 1
        $router = $this->container->get('router');
151
152 1
        if ((null === $router->getRouteCollection()->get($name)) ? false : true) {
153 1
            return true;
154
        } else {
155 1
            return false;
156
        }
157
    }
158
159
    protected function urlExists($name)
160
    {
161
        if ($this->checkUrl($name, false)) {
162
            return true;
163
        } else {
164
            if ($this->checkUrl($name, true)) {
165
                return true;
166
            } else {
167
                return false;
168
            }
169
        }
170
    }
171
172
    protected function checkUrl($name, $proxy)
173
    {
174
        $ch = curl_init($name);
175
176
        curl_setopt($ch, CURLOPT_URL, $name);
177
        if ($proxy) {
178
            curl_setopt($ch, CURLOPT_PROXY, 'proxyhttp.comune.intranet:8080');
179
        } else {
180
            curl_setopt($ch, CURLOPT_PROXY, null);
181
        }
182
        curl_setopt($ch, CURLOPT_NOBODY, true);
183
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
184
        curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout in seconds
185
        curl_exec($ch);
186
        $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
187
        if ($retcode === 200 || $retcode === 401) {
188
            $exist = true;
189
        } else {
190
            $exist = false;
191
        }
192
        curl_close($ch);
193
194
        return $exist;
195
    }
196
}
197