Completed
Push — master ( aa9f24...ead1f2 )
by Andrea
20:48 queued 17:21
created

MenuExtension::routeExists()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Twig\Extension;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
7
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
8
9
class MenuExtension extends \Twig_Extension
10
{
11
    protected $em;
12
    protected $urlgenerator;
13
    protected $user;
14
15 26
    public function __construct(ObjectManager $em, UrlGeneratorInterface $urlgenerator, TokenStorageInterface $user, $rootpath)
16
    {
17 26
        $this->em = $em;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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...
18 26
        $this->urlgenerator = $urlgenerator;
19 26
        $this->user = $user;
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...
20 26
        $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...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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 26
    }
22
23 1
    public function getFunctions()
24
    {
25
        return [
26 1
            new \Twig_SimpleFunction('generamenu', [$this, 'generamenu'], [
27 1
                'needs_environment' => true,
28
                'is_safe' => ['html'],
29
                ]),
30
        ];
31
    }
32
33 12
    public function generamenu(\Twig_Environment $environment)
34
    {
35 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

35
        $router = $this->urlgenerator->/** @scrutinizer ignore-call */ match('/')['_route'];
Loading history...
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...
36 12
        $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...
37 12
        $rispostahome[] = array('percorso' => $this->getUrlObject('', $router, ''),
38 12
            'nome' => 'Home',
39 12
            'target' => '_self',
40
        );
41
42 12
        $em = $this->em;
43
        /* @var $qb \Doctrine\ORM\QueryBuilder */
44 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

44
        /** @scrutinizer ignore-call */ 
45
        $qb = $em->createQueryBuilder();
Loading history...
45 12
        $qb->select(array('a'));
46 12
        $qb->from('BiCoreBundle:Menuapplicazione', 'a');
47 12
        $qb->where('a.attivo = :attivo and (a.padre is null or a.padre = 0)');
48 12
        $qb->setParameter('attivo', true);
49 12
        $qb->orderBy('a.padre', 'ASC');
50 12
        $qb->orderBy('a.ordine', 'ASC');
51 12
        $menu = $qb->getQuery()->getResult();
52
53 12
        $risposta = array_merge($rispostahome, $this->getMenu($menu));
54
55 12
        $pathmanuale = $this->rootpath.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'manuale.pdf';
56 12
        $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...
57 12
        $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...
58
59 12
        if (file_exists($pathmanuale)) {
60
            $risposta[] = array(
61
                'percorso' => $this->getUrlObject('Manuale', $pathmanuale, '_blank'),
62
                'nome' => 'Manuale', 'target' => '_blank', );
63
        }
64
65 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

65
        if ('ssocdf' === $this->user->getToken()->/** @scrutinizer ignore-call */ getProviderKey()) {
Loading history...
66
            $username = $this->user->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...
67
            $urlLogout = $this->urlgenerator->generate('fi_autenticazione_signout');
68
        }
69
70 12
        if ('main' === $this->user->getToken()->getProviderKey()) {
71 12
            $username = $this->user->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...
72 12
            $urlLogout = $this->urlgenerator->generate('fos_user_security_logout');
73
        }
74
75 12
        $risposta[] = array('percorso' => $this->getUrlObject($username, '', ''), 'nome' => $username, 'target' => '',
76
            'sottolivello' => array(
77 12
                array('percorso' => $urlLogout, 'nome' => 'Logout', 'target' => '_self'),
78
            ),
79
        );
80
81 12
        return $environment->render('BiCoreBundle:Menu:menu.html.twig', array('risposta' => $risposta));
82
    }
83
84 12
    protected function getMenu($menu)
85
    {
86 12
        $risposta = array();
87 12
        $em = $this->em;
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...
88
89 12
        foreach ($menu as $item) {
90 12
            $visualizzare = true;
91
92 12
            if ($item->isAutorizzazionerichiesta()) {
93 12
                $permessi = new \Cdf\BiCoreBundle\Service\Permessi\PermessiManager($this->em, $this->user);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
94 12
                $visualizzare = $permessi->canRead($item->getTag());
95
            }
96
97 12
            if ($visualizzare) {
98 12
                $qb = $em->createQueryBuilder();
99 12
                $qb->select(array('a'));
100 12
                $qb->from('BiCoreBundle:Menuapplicazione', 'a');
101 12
                $qb->where('a.padre = :padre_id');
102 12
                $qb->andWhere('a.attivo = :attivo');
103 12
                $qb->orderBy('a.padre', 'ASC');
104 12
                $qb->orderBy('a.ordine', 'ASC');
105 12
                $qb->setParameter('padre_id', $item->getId());
106 12
                $qb->setParameter('attivo', true);
107 12
                $submenu = $qb->getQuery()->getResult();
108
109 12
                $sottomenutabelle = $this->getSubMenu($submenu);
110
111 12
                $percorso = $this->getUrlObject($item->getNome(), $item->getPercorso(), $item->getTarget());
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...
112 12
                $risposta[] = array(
113 12
                    'percorso' => $percorso,
114 12
                    'nome' => $item->getNome(),
115 12
                    'sottolivello' => $sottomenutabelle,
116 12
                    'target' => $item->getTarget(),
117 12
                    'notifiche' => $item->hasNotifiche(),
118 12
                    'tag' => $item->getTag(),
119 12
                    'percorsonotifiche' => $this->getUrlObject($item->getNome(), $item->getPercorsonotifiche(), ''),
120
                );
121 12
                unset($submenu);
122 12
                unset($sottomenutabelle);
123
            }
124
        }
125
126 12
        return $risposta;
127
    }
128
129 12
    protected function getSubMenu($submenu)
130
    {
131 12
        $sottomenutabelle = array();
132 12
        foreach ($submenu as $subitem) {
133 12
            $visualizzare = true;
134 12
            if ($subitem->isAutorizzazionerichiesta()) {
135
                $permessi = new \Cdf\BiCoreBundle\Service\Permessi\PermessiManager($this->em, $this->user);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
136
                $visualizzare = $permessi->canRead($subitem->getTag());
137
            }
138
139 12
            if ($visualizzare) {
140 12
                $vettoresottomenu = $this->getMenu(array($subitem));
141 12
                $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...
142
143 12
                if (isset($sottomenu['sottolivello']) && count($sottomenu['sottolivello']) > 0) {
144 11
                    $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...
145 11
                    $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...
146 11
                    $sottomenutabelle[] = array_merge($menuobj, $sottolivellomenu);
147
                } else {
148 12
                    $sottomenutabelle[] = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
149
                }
150
            }
151
        }
152
153 12
        return $sottomenutabelle;
154
    }
155
156 12
    protected function getUrlObject($nome, $percorso, $target)
157
    {
158 12
        if ($this->routeExists($percorso)) {
159 12
            $percorso = $this->urlgenerator->generate($percorso);
160
        } else {
161 12
            $percorso = '#';
162
        }
163 12
        if (!$target) {
164 12
            $target = '_self';
165
        }
166
167 12
        return array('percorso' => $percorso, 'nome' => $nome, 'target' => $target);
168
    }
169
170 12
    protected function routeExists($name)
171
    {
172 12
        $router = $this->urlgenerator;
173
174 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

174
        if ((null === $router->/** @scrutinizer ignore-call */ getRouteCollection()->get($name)) ? false : true) {
Loading history...
175 12
            return true;
176
        } else {
177 12
            return false;
178
        }
179
    }
180
181
    protected function urlExists($name)
182
    {
183
        if ($this->checkUrl($name, false)) {
184
            return true;
185
        } else {
186
            if ($this->checkUrl($name, true)) {
187
                return true;
188
            } else {
189
                return false;
190
            }
191
        }
192
    }
193
194
    protected function checkUrl($name, $proxy)
195
    {
196
        $ch = curl_init($name);
197
198
        curl_setopt($ch, CURLOPT_URL, $name);
199
        if ($proxy) {
200
            curl_setopt($ch, CURLOPT_PROXY, $proxy);
201
        } else {
202
            curl_setopt($ch, CURLOPT_PROXY, null);
203
        }
204
        curl_setopt($ch, CURLOPT_NOBODY, true);
205
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
206
        curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout in seconds
207
        curl_exec($ch);
208
        $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
209
        if (200 === $retcode || 401 === $retcode) {
210
            $exist = true;
211
        } else {
212
            $exist = false;
213
        }
214
        curl_close($ch);
215
216
        return $exist;
217
    }
218
}
219