Passed
Push — master ( ce968c...4448bb )
by Andrea
50:49 queued 13s
created

MenuExtension::generamenu()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 51
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 4.0312

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 4
eloc 32
c 5
b 0
f 0
nc 8
nop 1
dl 0
loc 51
ccs 28
cts 32
cp 0.875
crap 4.0312
rs 9.408

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Cdf\BiCoreBundle\Twig\Extension;
4
5
use Cdf\BiCoreBundle\Service\Permessi\PermessiManager;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\ORM\QueryBuilder;
8
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
9
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
10
use Twig\Extension\AbstractExtension;
11
use Twig_Environment;
12
use Twig_SimpleFunction;
13
use function count;
14
15
class MenuExtension extends AbstractExtension
16
{
17
18
    protected $em;
19
    protected $urlgenerator;
20
    protected $user;
21
22 25
    public function __construct(ObjectManager $em, UrlGeneratorInterface $urlgenerator, TokenStorageInterface $user, $rootpath)
23
    {
24 25
        $this->em = $em;
25 25
        $this->urlgenerator = $urlgenerator;
26 25
        $this->user = $user;
27 25
        $this->rootpath = $rootpath;
0 ignored issues
show
Bug Best Practice introduced by
The property rootpath does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28 25
    }
29
30
    public function getFunctions()
31
    {
32
        return [
33
            new Twig_SimpleFunction('generamenu', [$this, 'generamenu'], [
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

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

33
            /** @scrutinizer ignore-deprecated */ new Twig_SimpleFunction('generamenu', [$this, 'generamenu'], [
Loading history...
34
                'needs_environment' => true,
35
                'is_safe' => ['html'],
36
                    ]),
37
        ];
38
    }
39
40 12
    public function generamenu(Twig_Environment $environment)
41
    {
42 12
        $router = $this->urlgenerator->match('/')['_route'];
0 ignored issues
show
Bug introduced by
The method match() does not exist on Symfony\Component\Routin...r\UrlGeneratorInterface. It seems like you code against a sub-type of Symfony\Component\Routin...r\UrlGeneratorInterface such as Symfony\Component\Routing\RouterInterface. ( Ignorable by Annotation )

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

42
        $router = $this->urlgenerator->/** @scrutinizer ignore-call */ match('/')['_route'];
Loading history...
43 12
        $rispostahome = array();
44 12
        $rispostahome[] = array('percorso' => $this->getUrlObject('', $router, ''),
45 12
            'nome' => 'Home',
46 12
            'target' => '_self',
47
        );
48
49 12
        $em = $this->em;
50
        /* @var $qb QueryBuilder */
51 12
        $qb = $em->createQueryBuilder();
0 ignored issues
show
Bug introduced by
The method createQueryBuilder() does not exist on Doctrine\Common\Persistence\ObjectManager. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Common\Persistence\ObjectManagerDecorator. Are you sure you never get one of those? ( Ignorable by Annotation )

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

51
        /** @scrutinizer ignore-call */ 
52
        $qb = $em->createQueryBuilder();
Loading history...
52 12
        $qb->select(array('a'));
53 12
        $qb->from('BiCoreBundle:Menuapplicazione', 'a');
54 12
        $qb->where('a.attivo = :attivo and (a.padre is null or a.padre = 0)');
55 12
        $qb->setParameter('attivo', true);
56 12
        $qb->orderBy('a.padre', 'ASC');
57 12
        $qb->orderBy('a.ordine', 'ASC');
58 12
        $menu = $qb->getQuery()->getResult();
59
60 12
        $risposta = array_merge($rispostahome, $this->getMenu($menu));
61
62 12
        $username = '';
63 12
        $urlLogout = '';
64
        
65 12
        $this->generaManualeMkdocMenu($risposta);
66
        
67 12
        $this->generaManualePdfMenu($risposta);
68
        
69 12
        if ('ssocdf' === $this->user->getToken()->getProviderKey()) {
0 ignored issues
show
Bug introduced by
The method getProviderKey() does not exist on Symfony\Component\Securi...on\Token\TokenInterface. It seems like you code against a sub-type of Symfony\Component\Securi...on\Token\TokenInterface such as Symfony\Component\Securi...uthenticationGuardToken or Symfony\Component\Securi...n\Token\RememberMeToken or Symfony\Component\Securi...n\UsernamePasswordToken or Symfony\Component\Securi...uthenticationGuardToken or Symfony\Component\Securi...n\PreAuthenticatedToken. ( Ignorable by Annotation )

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

69
        if ('ssocdf' === $this->user->getToken()->/** @scrutinizer ignore-call */ getProviderKey()) {
Loading history...
70
            $username = $this->user->getToken()->getUser()->getUsername();
71
            $urlLogout = $this->urlgenerator->generate('fi_autenticazione_signout');
72
        }
73
74 12
        if ('ssolineacomune' === $this->user->getToken()->getProviderKey()) {
75
            $username = $this->user->getToken()->getUser()->getUsername();
76
            $urlLogout = $this->urlgenerator->generate('fi_Lineacomuneauth_signout');
77
        }
78
79 12
        if ('main' === $this->user->getToken()->getProviderKey()) {
80 12
            $username = $this->user->getToken()->getUser()->getUsername();
81 12
            $urlLogout = $this->urlgenerator->generate('fos_user_security_logout');
82
        }
83
84 12
        $risposta[] = array('percorso' => $this->getUrlObject($username, '', ''), 'nome' => $username, 'target' => '',
85
            'sottolivello' => array(
86 12
                array('percorso' => $urlLogout, 'nome' => 'Logout', 'target' => '_self'),
87
            ),
88
        );
89
90 12
        return $environment->render('BiCoreBundle:Menu:menu.html.twig', array('risposta' => $risposta));
91
    }
92
93 12
    protected function generaManualePdfMenu(&$risposta)
94
    {
95 12
        $pathmanuale = $this->rootpath . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'manuale.pdf';
96 12
        if (file_exists($pathmanuale)) {
97
            $risposta[] = array(
98
                'percorso' => $this->getUrlObject('Manuale', $pathmanuale, '_blank'),
99
                'nome' => 'Manuale (Pdf)', 'target' => '_blank',);
100
        }
101 12
    }
102
103 12
    protected function generaManualeMkdocMenu(&$risposta)
104
    {
105 12
        $mkdocsfile = 'manuale' . DIRECTORY_SEPARATOR . 'index.html';
106 12
        $pathmanualemkdocs = $this->rootpath . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . $mkdocsfile;
107 12
        if (file_exists($pathmanualemkdocs)) {
108
            $risposta[] = array(
109
                'percorso' => array("percorso" => $this->urlgenerator->generate("homepage") . $mkdocsfile),
110
                'nome' => 'Manuale', 'target' => '_blank',);
111
        }
112 12
    }
113
114 12
    protected function getMenu($menu)
115
    {
116 12
        $risposta = array();
117 12
        $em = $this->em;
118
119 12
        foreach ($menu as $item) {
120 12
            $visualizzare = true;
121
122 12
            if ($item->isAutorizzazionerichiesta()) {
123 12
                $permessi = new PermessiManager($this->em, $this->user);
124 12
                $visualizzare = $permessi->canRead($item->getTag());
125
            }
126
127 12
            if ($visualizzare) {
128 12
                $qb = $em->createQueryBuilder();
129 12
                $qb->select(array('a'));
130 12
                $qb->from('BiCoreBundle:Menuapplicazione', 'a');
131 12
                $qb->where('a.padre = :padre_id');
132 12
                $qb->andWhere('a.attivo = :attivo');
133 12
                $qb->orderBy('a.padre', 'ASC');
134 12
                $qb->orderBy('a.ordine', 'ASC');
135 12
                $qb->setParameter('padre_id', $item->getId());
136 12
                $qb->setParameter('attivo', true);
137 12
                $submenu = $qb->getQuery()->getResult();
138
139 12
                $sottomenutabelle = $this->getSubMenu($submenu);
140
141 12
                $percorso = $this->getUrlObject($item->getNome(), $item->getPercorso(), $item->getTarget());
142 12
                $risposta[] = array(
143 12
                    'percorso' => $percorso,
144 12
                    'nome' => $item->getNome(),
145 12
                    'sottolivello' => $sottomenutabelle,
146 12
                    'target' => $item->getTarget(),
147 12
                    'notifiche' => $item->hasNotifiche(),
148 12
                    'tag' => $item->getTag(),
149 12
                    'percorsonotifiche' => $this->getUrlObject($item->getNome(), $item->getPercorsonotifiche(), ''),
150
                );
151 12
                unset($submenu);
152 12
                unset($sottomenutabelle);
153
            }
154
        }
155
156 12
        return $risposta;
157
    }
158
159 12
    protected function getSubMenu($submenu)
160
    {
161 12
        $sottomenutabelle = array();
162 12
        foreach ($submenu as $subitem) {
163 12
            $visualizzare = true;
164 12
            if ($subitem->isAutorizzazionerichiesta()) {
165
                $permessi = new PermessiManager($this->em, $this->user);
166
                $visualizzare = $permessi->canRead($subitem->getTag());
167
            }
168
169 12
            if ($visualizzare) {
170 12
                $vettoresottomenu = $this->getMenu(array($subitem));
171 12
                $sottomenu = $vettoresottomenu[0];
172
173 12
                if (isset($sottomenu['sottolivello']) && count($sottomenu['sottolivello']) > 0) {
174 11
                    $sottolivellomenu = array('sottolivello' => $sottomenu['sottolivello']);
175 11
                    $menuobj = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
176 11
                    $sottomenutabelle[] = array_merge($menuobj, $sottolivellomenu);
177
                } else {
178 12
                    $sottomenutabelle[] = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
179
                }
180
            }
181
        }
182
183 12
        return $sottomenutabelle;
184
    }
185
186 12
    protected function getUrlObject($nome, $percorso, $target)
187
    {
188 12
        if ($this->routeExists($percorso)) {
189 12
            $percorso = $this->urlgenerator->generate($percorso);
190
        } else {
191 12
            $percorso = '#';
192
        }
193 12
        if (!$target) {
194 12
            $target = '_self';
195
        }
196
197 12
        return array('percorso' => $percorso, 'nome' => $nome, 'target' => $target);
198
    }
199
200 12
    protected function routeExists($name)
201
    {
202 12
        $router = $this->urlgenerator;
203
204 12
        if ((null === $router->getRouteCollection()->get($name)) ? false : true) {
0 ignored issues
show
Bug introduced by
The method getRouteCollection() does not exist on Symfony\Component\Routin...r\UrlGeneratorInterface. It seems like you code against a sub-type of Symfony\Component\Routin...r\UrlGeneratorInterface such as Symfony\Component\Routing\RouterInterface. ( Ignorable by Annotation )

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

204
        if ((null === $router->/** @scrutinizer ignore-call */ getRouteCollection()->get($name)) ? false : true) {
Loading history...
205 12
            return true;
206
        } else {
207 12
            return false;
208
        }
209
    }
210
211
    protected function urlExists($name)
212
    {
213
        if ($this->checkUrl($name, false)) {
214
            return true;
215
        } else {
216
            if ($this->checkUrl($name, true)) {
217
                return true;
218
            } else {
219
                return false;
220
            }
221
        }
222
    }
223
224
    protected function checkUrl($name, $proxy)
225
    {
226
        $ch = curl_init($name);
227
228
        curl_setopt($ch, CURLOPT_URL, $name);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

228
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $name);
Loading history...
229
        if ($proxy) {
230
            curl_setopt($ch, CURLOPT_PROXY, $proxy);
231
        } else {
232
            curl_setopt($ch, CURLOPT_PROXY, null);
233
        }
234
        curl_setopt($ch, CURLOPT_NOBODY, true);
235
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
236
        curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout in seconds
237
        curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

237
        curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
238
        $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_getinfo() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

238
        $retcode = curl_getinfo(/** @scrutinizer ignore-type */ $ch, CURLINFO_HTTP_CODE);
Loading history...
239
        if (200 === $retcode || 401 === $retcode) {
240
            $exist = true;
241
        } else {
242
            $exist = false;
243
        }
244
        curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

244
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
245
246
        return $exist;
247
    }
248
}
249