Passed
Push — master ( 09ce70...3c2452 )
by Andrea
32:18 queued 16s
created

MenuExtension::getSubMenu()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.0702

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 7
nop 1
dl 0
loc 25
ccs 14
cts 16
cp 0.875
crap 6.0702
rs 9.1111
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Twig\Extension;
4
5
use Cdf\BiCoreBundle\Service\Permessi\PermessiManager;
6
use Doctrine\ORM\EntityManagerInterface;
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 as TwigEnvironment;
12
use Twig\TwigFunction;
13
use function count;
14
15
class MenuExtension extends AbstractExtension
16
{
17
18
    protected $em;
19
    protected $urlgenerator;
20
    protected $user;
21
    protected $rootpath;
22
23 25
    public function __construct(EntityManagerInterface $em, UrlGeneratorInterface $urlgenerator, TokenStorageInterface $user, $rootpath)
24
    {
25 25
        $this->em = $em;
26 25
        $this->urlgenerator = $urlgenerator;
27 25
        $this->user = $user;
28 25
        $this->rootpath = $rootpath;
29 25
    }
30
31 1
    public function getFunctions()
32
    {
33
        return [
34 1
            new TwigFunction('generamenu', [$this, 'generamenu'], [
35 1
                'needs_environment' => true,
36
                'is_safe' => ['html'],
37
                    ]),
38
        ];
39
    }
40
41 11
    public function generamenu(TwigEnvironment $environment)
42
    {
43 11
        $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

43
        $router = $this->urlgenerator->/** @scrutinizer ignore-call */ match('/')['_route'];
Loading history...
44 11
        $rispostahome = array();
45 11
        $rispostahome[] = array('percorso' => $this->getUrlObject('', $router, ''),
46 11
            'nome' => 'Home',
47 11
            'target' => '_self',
48
        );
49
50 11
        $em = $this->em;
51
        /* @var $qb QueryBuilder */
52 11
        $qb = $em->createQueryBuilder();
53 11
        $qb->select(array('a'));
54 11
        $qb->from('BiCoreBundle:Menuapplicazione', 'a');
55 11
        $qb->where('a.attivo = :attivo and (a.padre is null or a.padre = 0)');
56 11
        $qb->setParameter('attivo', true);
57 11
        $qb->orderBy('a.padre', 'ASC');
58 11
        $qb->orderBy('a.ordine', 'ASC');
59 11
        $menu = $qb->getQuery()->getResult();
60
61 11
        $risposta = array_merge($rispostahome, $this->getMenu($menu));
62
63 11
        $username = '';
64 11
        $urlLogout = '';
65
        
66 11
        $this->generaManualeMkdocMenu($risposta);
67
        
68 11
        $this->generaManualePdfMenu($risposta);
69
        
70 11
        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

70
        if ('ssocdf' === $this->user->getToken()->/** @scrutinizer ignore-call */ getProviderKey()) {
Loading history...
71
            $username = $this->user->getToken()->getUser()->getUsername();
0 ignored issues
show
Bug introduced by
The method getUsername() does not exist on Stringable. It seems like you code against a sub-type of Stringable such as Symfony\Component\Securi...ernameNotFoundException or Symfony\Component\Securi...ernameNotFoundException. ( Ignorable by Annotation )

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

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

208
        if ((null === $router->/** @scrutinizer ignore-call */ getRouteCollection()->get($name)) ? false : true) {
Loading history...
209 11
            return true;
210
        } else {
211 11
            return false;
212
        }
213
    }
214
215
    protected function urlExists($name)
216
    {
217
        if ($this->checkUrl($name, false)) {
218
            return true;
219
        } else {
220
            if ($this->checkUrl($name, true)) {
221
                return true;
222
            } else {
223
                return false;
224
            }
225
        }
226
    }
227
228
    protected function checkUrl($name, $proxy)
229
    {
230
        $ch = curl_init($name);
231
232
        curl_setopt($ch, CURLOPT_URL, $name);
233
        if ($proxy) {
234
            curl_setopt($ch, CURLOPT_PROXY, $proxy);
235
        } else {
236
            curl_setopt($ch, CURLOPT_PROXY, null);
237
        }
238
        curl_setopt($ch, CURLOPT_NOBODY, true);
239
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
240
        curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout in seconds
241
        curl_exec($ch);
242
        $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
243
        if (200 === $retcode || 401 === $retcode) {
244
            $exist = true;
245
        } else {
246
            $exist = false;
247
        }
248
        curl_close($ch);
249
250
        return $exist;
251
    }
252
}
253