Completed
Push — 4.1 ( 7e926b...16ef75 )
by Andrea
13:15
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
eloc 5
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Fi\CoreBundle\Twig\Extension;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
8
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
9
10
class MenuExtension extends \Twig_Extension
11
{
12
13
    protected $em;
14
    protected $urlgenerator;
15
    protected $user;
16
17 13
    public function __construct(ObjectManager $em, UrlGeneratorInterface $urlgenerator, TokenStorageInterface $user, $rootpath)
18
    {
19 13
        $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...
20 13
        $this->urlgenerator = $urlgenerator;
21 13
        $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...
22 13
        $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...
23 13
    }
24
    public function getFunctions()
25
    {
26
        return [
27
            new \Twig_SimpleFunction('generamenu', [$this, 'generamenu'], [
28
                'needs_environment' => true,
29
                'is_safe' => ['html']
30
                    ])
31
        ];
32
    }
33 7
    public function generamenu(\Twig_Environment $environment)
34
    {
35 7
        $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 7
        $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 7
        $rispostahome[] = array('percorso' => $this->getUrlObject(getenv('ficorebundle_appname'), $router, ''),
38 7
            'nome' => 'Home',
39 7
            'target' => '',
40
        );
41
42 7
        $em = $this->em;
43
        /* @var $qb \Doctrine\ORM\QueryBuilder */
44 7
        $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 7
        $qb->select(array('a'));
46 7
        $qb->from('CoreBundle:Menuapplicazione', 'a');
47 7
        $qb->where('a.attivo = :attivo and (a.padre is null or a.padre = 0)');
48 7
        $qb->setParameter('attivo', true);
49 7
        $qb->orderBy('a.padre', 'ASC');
50 7
        $qb->orderBy('a.ordine', 'ASC');
51 7
        $menu = $qb->getQuery()->getResult();
52
53 7
        $risposta = array_merge($rispostahome, $this->getMenu($menu));
54
55 7
        $pathmanuale = $this->rootpath . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'manuale.pdf';
56 7
        $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...
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
57 7
        $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...
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
58
59 7
        if (file_exists($pathmanuale)) {
60
            $risposta[] = array(
61
                'percorso' => $this->getUrlObject('Manuale', $pathmanuale, '_blank'),
62
                'nome' => 'Manuale', 'target' => '_blank');
63
        }
64
65 7
        if ($this->user->getToken()->getProviderKey() === 'secured_area') {
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 ($this->user->getToken()->/** @scrutinizer ignore-call */ getProviderKey() === 'secured_area') {
Loading history...
66
            $username = $this->user->getUsername();
0 ignored issues
show
Bug introduced by
The method getUsername() does not exist on Symfony\Component\Securi...e\TokenStorageInterface. ( Ignorable by Annotation )

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

66
            /** @scrutinizer ignore-call */ 
67
            $username = $this->user->getUsername();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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->generateUrl('fi_autenticazione_signout');
0 ignored issues
show
Bug introduced by
The method generateUrl() does not exist on Fi\CoreBundle\Twig\Extension\MenuExtension. ( Ignorable by Annotation )

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

67
            /** @scrutinizer ignore-call */ 
68
            $urlLogout = $this->generateUrl('fi_autenticazione_signout');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
        }
69
70 7
        if ($this->user->getToken()->getProviderKey() === 'main') {
71 7
            $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 7
            $urlLogout = $this->urlgenerator->generate('fos_user_security_logout');
73
        }
74
75 7
        $risposta[] = array('percorso' => $this->getUrlObject($username, '', ''), 'nome' => $username, 'target' => '',
76
            'sottolivello' => array(
77 7
                array('percorso' => $urlLogout, 'nome' => 'Logout', 'target' => ''),
78
            ),
79
        );
80 7
        return $environment->render('CoreBundle:Menu:menu.html.twig', array('risposta' => $risposta));
81
    }
82 7
    protected function getMenu($menu)
83
    {
84 7
        $risposta = array();
85 7
        $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...
86
87 7
        foreach ($menu as $item) {
88 7
            $visualizzare = true;
89
90 7
            if ($item->isAutorizzazionerichiesta()) {
91
                $permessi = new \Fi\CoreBundle\Utils\PermessiUtils($this->em, $item->getTag(), $this->user->getToken()->getUser());
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...
92
                $visualizzare = $permessi->canRead();
93
            }
94
95
96 7
            if ($visualizzare) {
97 7
                $qb = $em->createQueryBuilder();
98 7
                $qb->select(array('a'));
99 7
                $qb->from('CoreBundle:Menuapplicazione', 'a');
100 7
                $qb->where('a.padre = :padre_id');
101 7
                $qb->andWhere('a.attivo = :attivo');
102 7
                $qb->orderBy('a.padre', 'ASC');
103 7
                $qb->orderBy('a.ordine', 'ASC');
104 7
                $qb->setParameter('padre_id', $item->getId());
105 7
                $qb->setParameter('attivo', true);
106 7
                $submenu = $qb->getQuery()->getResult();
107
108 7
                $sottomenutabelle = $this->getSubMenu($submenu);
109
110 7
                $risposta[] = array(
111 7
                    'percorso' => $this->getUrlObject($item->getNome(), $item->getPercorso(), $item->getTarget()),
112 7
                    'nome' => $item->getNome(),
113 7
                    'sottolivello' => $sottomenutabelle,
114 7
                    'target' => $item->getTarget(),
115 7
                    'notifiche' => $item->hasNotifiche(),
116 7
                    'tag' => $item->getTag(),
117 7
                    'percorsonotifiche' => $this->getUrlObject($item->getNome(), $item->getPercorsonotifiche(), ''),
118
                );
119 7
                unset($submenu);
120 7
                unset($sottomenutabelle);
121
            }
122
        }
123
124 7
        return $risposta;
125
    }
126 7
    protected function getSubMenu($submenu)
127
    {
128 7
        $sottomenutabelle = array();
129 7
        foreach ($submenu as $subitem) {
130 7
            $visualizzare = true;
131 7
            if ($subitem->isAutorizzazionerichiesta()) {
132
                $permessi = new \Fi\CoreBundle\Utils\PermessiUtils($this->em, $subitem->getTag(), $this->user->getToken()->getUser());
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...
133
                $visualizzare = $permessi->canRead();
134
            }
135
136 7
            if ($visualizzare) {
137 7
                $vettoresottomenu = $this->getMenu(array($subitem));
138 7
                $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...
139
140 7
                if (isset($sottomenu['sottolivello']) && count($sottomenu['sottolivello']) > 0) {
141 7
                    $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...
142 7
                    $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...
143 7
                    $sottomenutabelle[] = array_merge($menuobj, $sottolivellomenu);
144
                } else {
145 7
                    $sottomenutabelle[] = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
146
                }
147
            }
148
        }
149
150 7
        return $sottomenutabelle;
151
    }
152 7
    protected function getUrlObject($nome, $percorso, $target)
153
    {
154 7
        if ($this->routeExists($percorso)) {
155 7
            return array('percorso' => $this->urlgenerator->generate($percorso), 'nome' => $nome, 'target' => $target);
156
        } else {
157 7
            return array('percorso' => $percorso, 'nome' => $nome, 'target' => $target);
158
        }
159
    }
160 7
    protected function routeExists($name)
161
    {
162 7
        $router = $this->urlgenerator;
163
164 7
        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

164
        if ((null === $router->/** @scrutinizer ignore-call */ getRouteCollection()->get($name)) ? false : true) {
Loading history...
165 7
            return true;
166
        } else {
167 7
            return false;
168
        }
169
    }
170
    protected function urlExists($name)
171
    {
172
        if ($this->checkUrl($name, false)) {
173
            return true;
174
        } else {
175
            if ($this->checkUrl($name, true)) {
176
                return true;
177
            } else {
178
                return false;
179
            }
180
        }
181
    }
182
    protected function checkUrl($name, $proxy)
183
    {
184
        $ch = curl_init($name);
185
186
        curl_setopt($ch, CURLOPT_URL, $name);
187
        if ($proxy) {
188
            curl_setopt($ch, CURLOPT_PROXY, 'proxyhttp.comune.intranet:8080');
189
        } else {
190
            curl_setopt($ch, CURLOPT_PROXY, null);
191
        }
192
        curl_setopt($ch, CURLOPT_NOBODY, true);
193
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
194
        curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout in seconds
195
        curl_exec($ch);
196
        $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
197
        if ($retcode === 200 || $retcode === 401) {
198
            $exist = true;
199
        } else {
200
            $exist = false;
201
        }
202
        curl_close($ch);
203
204
        return $exist;
205
    }
206
}
207