Completed
Push — 4.1 ( 2ae6e4...1d93ff )
by Andrea
13:18
created

MenuExtension::checkUrl()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 23
ccs 0
cts 16
cp 0
rs 9.7
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 20
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 $container;
14
    protected $em;
15
    protected $gestionepermessi;
16
    protected $urlgenerator;
17
    
18 10
    public function __construct(ContainerInterface $container, ObjectManager $em, UrlGeneratorInterface $urlgenerator, TokenStorageInterface $user)
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

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

18
    public function __construct(ContainerInterface $container, ObjectManager $em, UrlGeneratorInterface $urlgenerator, /** @scrutinizer ignore-unused */ TokenStorageInterface $user)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
    {
20 10
        $this->container = $container;
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...
21 10
        $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...
22 10
        $this->urlgenerator = $urlgenerator;
23
        //$this->gestionepermessi = $container->get("fi.corebundle.controller.permessi");
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
24 10
    }
25 7
    public function getFunctions()
26
    {
27
        return [
28 7
            new \Twig_SimpleFunction('generamenu', [$this, 'generamenu'], [
29 7
                'needs_environment' => true,
30
                'is_safe' => ['html']
31
                    ])
32
        ];
33
    }
34 7
    public function generamenu(\Twig_Environment $environment)
35
    {
36 7
        $router = $this->container->get('router')->match('/')['_route'];
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...
37 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...
38 7
        $rispostahome[] = array('percorso' => $this->getUrlObject($this->container->getParameter('appname'), $router, ''),
39 7
            'nome' => 'Home',
40 7
            'target' => '',
41
        );
42
43 7
        $em = $this->container->get('doctrine')->getManager();
44
        /* @var $qb \Doctrine\ORM\QueryBuilder */
45 7
        $qb = $em->createQueryBuilder();
46 7
        $qb->select(array('a'));
47 7
        $qb->from('CoreBundle:MenuApplicazione', 'a');
48 7
        $qb->where('a.attivo = :attivo and (a.padre is null or a.padre = 0)');
49 7
        $qb->setParameter('attivo', true);
50 7
        $qb->orderBy('a.padre', 'ASC');
51 7
        $qb->orderBy('a.ordine', 'ASC');
52 7
        $menu = $qb->getQuery()->useQueryCache(true)->useResultCache(true, null, 'MenuApplicazione')->getResult();
53
54 7
        $risposta = array_merge($rispostahome, $this->getMenu($menu));
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...
55 7
        $webdir = $this->container->get('kernel')->getRootDir() . '/../web';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
56 7
        $pathmanuale = '/uploads/manuale.pdf';
57 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...
58 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...
59
60 7
        if (file_exists($webdir . $pathmanuale)) {
61
            $risposta[] = array(
62
                'percorso' => $this->container->getUrlObject('Manuale', $pathmanuale, '_blank'),
0 ignored issues
show
Bug introduced by
The method getUrlObject() does not exist on Symfony\Component\Depend...tion\ContainerInterface. ( Ignorable by Annotation )

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

62
                'percorso' => $this->container->/** @scrutinizer ignore-call */ getUrlObject('Manuale', $pathmanuale, '_blank'),

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...
63
                'nome' => 'Manuale', 'target' => '_blank');
64
        }
65
66 7
        if ($this->container->get('security.token_storage')->getToken()->getProviderKey() === 'secured_area') {
67
            $username = $this->container->getUser()->getUsername();
0 ignored issues
show
Bug introduced by
The method getUser() does not exist on Symfony\Component\Depend...tion\ContainerInterface. ( Ignorable by Annotation )

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

67
            $username = $this->container->/** @scrutinizer ignore-call */ getUser()->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...
68
            $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

68
            /** @scrutinizer ignore-call */ 
69
            $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...
69
        }
70
71 7
        if ($this->container->get('security.token_storage')->getToken()->getProviderKey() === 'main') {
72 7
            $username = $this->container->get('security.token_storage')->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...
73 7
            $urlLogout = $this->urlgenerator->generate('fos_user_security_logout');
74
        }
75
76 7
        $risposta[] = array('percorso' => $this->getUrlObject($username, '', ''), 'nome' => $username, 'target' => '',
77
            'sottolivello' => array(
78 7
                array('percorso' => $urlLogout, 'nome' => 'Logout', 'target' => ''),
79
            ),
80
        );
81 7
        return $environment->render('CoreBundle:Menu:menu.html.twig', array('risposta' => $risposta));
82
    }
83 7
    protected function getMenu($menu)
84
    {
85 7
        $gestionepermessi = $this->gestionepermessi;
86
87 7
        $risposta = array();
88 7
        $em = $this->container->get('doctrine')->getManager();
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...
89
90 7
        foreach ($menu as $item) {
91 7
            $visualizzare = true;
92
93 7
            if ($item->isAutorizzazionerichiesta()) {
94
                $visualizzare = $gestionepermessi->sulmenu(array('modulo' => $item->getTag()));
95
            }
96
97 7
            if ($visualizzare) {
98 7
                $qb = $em->createQueryBuilder();
99 7
                $qb->select(array('a'));
100 7
                $qb->from('CoreBundle:MenuApplicazione', 'a');
101 7
                $qb->where('a.padre = :padre_id');
102 7
                $qb->andWhere('a.attivo = :attivo');
103 7
                $qb->orderBy('a.padre', 'ASC');
104 7
                $qb->orderBy('a.ordine', 'ASC');
105 7
                $qb->setParameter('padre_id', $item->getId());
106 7
                $qb->setParameter('attivo', true);
107 7
                $submenu = $qb->getQuery()->useQueryCache(true)->useResultCache(true, null, 'MenuApplicazione')->getResult();
108
109 7
                $sottomenutabelle = $this->getSubMenu($submenu);
110
111 7
                $risposta[] = array(
112 7
                    'percorso' => $this->getUrlObject($item->getNome(), $item->getPercorso(), $item->getTarget()),
113 7
                    'nome' => $item->getNome(),
114 7
                    'sottolivello' => $sottomenutabelle,
115 7
                    'target' => $item->getTarget(),
116 7
                    'notifiche' => $item->hasNotifiche(),
117 7
                    'tag' => $item->getTag(),
118 7
                    'percorsonotifiche' => $this->getUrlObject($item->getNome(), $item->getPercorsonotifiche(), ''),
119
                );
120 7
                unset($submenu);
121 7
                unset($sottomenutabelle);
122
            }
123
        }
124
125 7
        return $risposta;
126
    }
127 7
    protected function getSubMenu($submenu)
128
    {
129 7
        $gestionepermessi = $this->gestionepermessi;
130
131 7
        $sottomenutabelle = array();
132 7
        foreach ($submenu as $subitem) {
133 7
            $visualizzare = true;
134 7
            if ($subitem->isAutorizzazionerichiesta()) {
135
                $visualizzare = $gestionepermessi->sulmenu(array('modulo' => $subitem->getTag()));
136
            }
137
138 7
            if ($visualizzare) {
139 7
                $vettoresottomenu = $this->getMenu(array($subitem));
140 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...
141
142 7
                if (isset($sottomenu['sottolivello']) && count($sottomenu['sottolivello']) > 0) {
143 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...
144 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...
145 7
                    $sottomenutabelle[] = array_merge($menuobj, $sottolivellomenu);
146
                } else {
147 7
                    $sottomenutabelle[] = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
148
                }
149
            }
150
        }
151
152 7
        return $sottomenutabelle;
153
    }
154 7
    protected function getUrlObject($nome, $percorso, $target)
155
    {
156 7
        if ($this->routeExists($percorso)) {
157 7
            return array('percorso' => $this->urlgenerator->generate($percorso), 'nome' => $nome, 'target' => $target);
158
        } else {
159 7
            return array('percorso' => $percorso, 'nome' => $nome, 'target' => $target);
160
        }
161
    }
162 7
    protected function routeExists($name)
163
    {
164 7
        $router = $this->container->get('router');
165
166 7
        if ((null === $router->getRouteCollection()->get($name)) ? false : true) {
167 7
            return true;
168
        } else {
169 7
            return false;
170
        }
171
    }
172
    protected function urlExists($name)
173
    {
174
        if ($this->checkUrl($name, false)) {
175
            return true;
176
        } else {
177
            if ($this->checkUrl($name, true)) {
178
                return true;
179
            } else {
180
                return false;
181
            }
182
        }
183
    }
184
    protected function checkUrl($name, $proxy)
185
    {
186
        $ch = curl_init($name);
187
188
        curl_setopt($ch, CURLOPT_URL, $name);
189
        if ($proxy) {
190
            curl_setopt($ch, CURLOPT_PROXY, 'proxyhttp.comune.intranet:8080');
191
        } else {
192
            curl_setopt($ch, CURLOPT_PROXY, null);
193
        }
194
        curl_setopt($ch, CURLOPT_NOBODY, true);
195
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
196
        curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout in seconds
197
        curl_exec($ch);
198
        $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
199
        if ($retcode === 200 || $retcode === 401) {
200
            $exist = true;
201
        } else {
202
            $exist = false;
203
        }
204
        curl_close($ch);
205
206
        return $exist;
207
    }
208
}
209