Passed
Push — develop ( f339c9...ca27f8 )
by Andrea
52:52
created

MenuExtension::generamenu()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 61
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 6.5625

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 40
c 3
b 0
f 0
nc 32
nop 1
dl 0
loc 61
ccs 30
cts 40
cp 0.75
crap 6.5625
rs 8.6577

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
    protected $em;
18
    protected $urlgenerator;
19
    protected $user;
20
21 25
    public function __construct(ObjectManager $em, UrlGeneratorInterface $urlgenerator, TokenStorageInterface $user, $rootpath)
22
    {
23 25
        $this->em = $em;
24 25
        $this->urlgenerator = $urlgenerator;
25 25
        $this->user = $user;
26 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...
27 25
    }
28
29
    public function getFunctions()
30
    {
31
        return [
32
            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

32
            /** @scrutinizer ignore-deprecated */ new Twig_SimpleFunction('generamenu', [$this, 'generamenu'], [
Loading history...
33
                'needs_environment' => true,
34
                'is_safe' => ['html'],
35
                ]),
36
        ];
37
    }
38
39 12
    public function generamenu(Twig_Environment $environment)
40
    {
41 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

41
        $router = $this->urlgenerator->/** @scrutinizer ignore-call */ match('/')['_route'];
Loading history...
42 12
        $rispostahome = array();
43 12
        $rispostahome[] = array('percorso' => $this->getUrlObject('', $router, ''),
44 12
            'nome' => 'Home',
45 12
            'target' => '_self',
46
        );
47
48 12
        $em = $this->em;
49
        /* @var $qb QueryBuilder */
50 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

50
        /** @scrutinizer ignore-call */ 
51
        $qb = $em->createQueryBuilder();
Loading history...
51 12
        $qb->select(array('a'));
52 12
        $qb->from('BiCoreBundle:Menuapplicazione', 'a');
53 12
        $qb->where('a.attivo = :attivo and (a.padre is null or a.padre = 0)');
54 12
        $qb->setParameter('attivo', true);
55 12
        $qb->orderBy('a.padre', 'ASC');
56 12
        $qb->orderBy('a.ordine', 'ASC');
57 12
        $menu = $qb->getQuery()->getResult();
58
59 12
        $risposta = array_merge($rispostahome, $this->getMenu($menu));
60
61 12
        $pathmanualemkdocs = $this->rootpath.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'manuale/index.html';
62 12
        $pathmanuale = $this->rootpath.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'manuale.pdf';
63 12
        $username = '';
64 12
        $urlLogout = '';
65
66 12
        if (file_exists($pathmanualemkdocs)) {
67
            $risposta[] = array(
68
                'percorso' => $this->getUrlObject('Manuale', $pathmanualemkdocs, '_blank'),
69
                'nome' => 'Manuale', 'target' => '_blank', );
70
        }
71
72 12
        if (file_exists($pathmanuale)) {
73
            $risposta[] = array(
74
                'percorso' => $this->getUrlObject('Manuale', $pathmanuale, '_blank'),
75
                'nome' => 'Manuale (Pdf)', 'target' => '_blank', );
76
        }
77
78 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

78
        if ('ssocdf' === $this->user->getToken()->/** @scrutinizer ignore-call */ getProviderKey()) {
Loading history...
79
            $username = $this->user->getToken()->getUser()->getUsername();
80
            $urlLogout = $this->urlgenerator->generate('fi_autenticazione_signout');
81
        }
82
83 12
        if ('ssolineacomune' === $this->user->getToken()->getProviderKey()) {
84
            $username = $this->user->getToken()->getUser()->getUsername();
85
            $urlLogout = $this->urlgenerator->generate('fi_Lineacomuneauth_signout');
86
        }
87
88 12
        if ('main' === $this->user->getToken()->getProviderKey()) {
89 12
            $username = $this->user->getToken()->getUser()->getUsername();
90 12
            $urlLogout = $this->urlgenerator->generate('fos_user_security_logout');
91
        }
92
93 12
        $risposta[] = array('percorso' => $this->getUrlObject($username, '', ''), 'nome' => $username, 'target' => '',
94
            'sottolivello' => array(
95 12
                array('percorso' => $urlLogout, 'nome' => 'Logout', 'target' => '_self'),
96
            ),
97
        );
98
99 12
        return $environment->render('BiCoreBundle:Menu:menu.html.twig', array('risposta' => $risposta));
100
    }
101
102 12
    protected function getMenu($menu)
103
    {
104 12
        $risposta = array();
105 12
        $em = $this->em;
106
107 12
        foreach ($menu as $item) {
108 12
            $visualizzare = true;
109
110 12
            if ($item->isAutorizzazionerichiesta()) {
111 12
                $permessi = new PermessiManager($this->em, $this->user);
112 12
                $visualizzare = $permessi->canRead($item->getTag());
113
            }
114
115 12
            if ($visualizzare) {
116 12
                $qb = $em->createQueryBuilder();
117 12
                $qb->select(array('a'));
118 12
                $qb->from('BiCoreBundle:Menuapplicazione', 'a');
119 12
                $qb->where('a.padre = :padre_id');
120 12
                $qb->andWhere('a.attivo = :attivo');
121 12
                $qb->orderBy('a.padre', 'ASC');
122 12
                $qb->orderBy('a.ordine', 'ASC');
123 12
                $qb->setParameter('padre_id', $item->getId());
124 12
                $qb->setParameter('attivo', true);
125 12
                $submenu = $qb->getQuery()->getResult();
126
127 12
                $sottomenutabelle = $this->getSubMenu($submenu);
128
129 12
                $percorso = $this->getUrlObject($item->getNome(), $item->getPercorso(), $item->getTarget());
130 12
                $risposta[] = array(
131 12
                    'percorso' => $percorso,
132 12
                    'nome' => $item->getNome(),
133 12
                    'sottolivello' => $sottomenutabelle,
134 12
                    'target' => $item->getTarget(),
135 12
                    'notifiche' => $item->hasNotifiche(),
136 12
                    'tag' => $item->getTag(),
137 12
                    'percorsonotifiche' => $this->getUrlObject($item->getNome(), $item->getPercorsonotifiche(), ''),
138
                );
139 12
                unset($submenu);
140 12
                unset($sottomenutabelle);
141
            }
142
        }
143
144 12
        return $risposta;
145
    }
146
147 12
    protected function getSubMenu($submenu)
148
    {
149 12
        $sottomenutabelle = array();
150 12
        foreach ($submenu as $subitem) {
151 12
            $visualizzare = true;
152 12
            if ($subitem->isAutorizzazionerichiesta()) {
153
                $permessi = new PermessiManager($this->em, $this->user);
154
                $visualizzare = $permessi->canRead($subitem->getTag());
155
            }
156
157 12
            if ($visualizzare) {
158 12
                $vettoresottomenu = $this->getMenu(array($subitem));
159 12
                $sottomenu = $vettoresottomenu[0];
160
161 12
                if (isset($sottomenu['sottolivello']) && count($sottomenu['sottolivello']) > 0) {
162 11
                    $sottolivellomenu = array('sottolivello' => $sottomenu['sottolivello']);
163 11
                    $menuobj = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
164 11
                    $sottomenutabelle[] = array_merge($menuobj, $sottolivellomenu);
165
                } else {
166 12
                    $sottomenutabelle[] = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
167
                }
168
            }
169
        }
170
171 12
        return $sottomenutabelle;
172
    }
173
174 12
    protected function getUrlObject($nome, $percorso, $target)
175
    {
176 12
        if ($this->routeExists($percorso)) {
177 12
            $percorso = $this->urlgenerator->generate($percorso);
178
        } else {
179 12
            $percorso = '#';
180
        }
181 12
        if (!$target) {
182 12
            $target = '_self';
183
        }
184
185 12
        return array('percorso' => $percorso, 'nome' => $nome, 'target' => $target);
186
    }
187
188 12
    protected function routeExists($name)
189
    {
190 12
        $router = $this->urlgenerator;
191
192 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

192
        if ((null === $router->/** @scrutinizer ignore-call */ getRouteCollection()->get($name)) ? false : true) {
Loading history...
193 12
            return true;
194
        } else {
195 12
            return false;
196
        }
197
    }
198
199
    protected function urlExists($name)
200
    {
201
        if ($this->checkUrl($name, false)) {
202
            return true;
203
        } else {
204
            if ($this->checkUrl($name, true)) {
205
                return true;
206
            } else {
207
                return false;
208
            }
209
        }
210
    }
211
212
    protected function checkUrl($name, $proxy)
213
    {
214
        $ch = curl_init($name);
215
216
        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

216
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $name);
Loading history...
217
        if ($proxy) {
218
            curl_setopt($ch, CURLOPT_PROXY, $proxy);
219
        } else {
220
            curl_setopt($ch, CURLOPT_PROXY, null);
221
        }
222
        curl_setopt($ch, CURLOPT_NOBODY, true);
223
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
224
        curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout in seconds
225
        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

225
        curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
226
        $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

226
        $retcode = curl_getinfo(/** @scrutinizer ignore-type */ $ch, CURLINFO_HTTP_CODE);
Loading history...
227
        if (200 === $retcode || 401 === $retcode) {
228
            $exist = true;
229
        } else {
230
            $exist = false;
231
        }
232
        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

232
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
233
234
        return $exist;
235
    }
236
}
237