Passed
Push — master ( 3eee26...868059 )
by Andrea
16:12
created

MenuExtension::getUrlObject()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 3
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Cdf\BiCoreBundle\Twig\Extension;
3
4
use Symfony\Component\DependencyInjection\ContainerInterface;
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
12
    protected $em;
13
    protected $urlgenerator;
14
    protected $user;
15
16 24
    public function __construct(ObjectManager $em, UrlGeneratorInterface $urlgenerator, TokenStorageInterface $user, $rootpath)
17
    {
18 24
        $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...
19 24
        $this->urlgenerator = $urlgenerator;
20 24
        $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...
21 24
        $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...
22 24
    }
23
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
34 12
    public function generamenu(\Twig_Environment $environment)
35
    {
36 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

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

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

66
        if ($this->user->getToken()->/** @scrutinizer ignore-call */ getProviderKey() === 'ssocdf') {
Loading history...
67
            $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...
68
            $urlLogout = $this->urlgenerator->generate('fi_autenticazione_signout');
69
        }
70
71 12
        if ($this->user->getToken()->getProviderKey() === 'main') {
72 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...
73 12
            $urlLogout = $this->urlgenerator->generate('fos_user_security_logout');
74
        }
75
76 12
        $risposta[] = array('percorso' => $this->getUrlObject($username, '', ''), 'nome' => $username, 'target' => '',
77
            'sottolivello' => array(
78 12
                array('percorso' => $urlLogout, 'nome' => 'Logout', 'target' => '_self'),
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\Utils\Permessi\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...
94 12
                $visualizzare = $permessi->canRead();
95
            }
96
97
98 12
            if ($visualizzare) {
99 12
                $qb = $em->createQueryBuilder();
100 12
                $qb->select(array('a'));
101 12
                $qb->from('BiCoreBundle:Menuapplicazione', 'a');
102 12
                $qb->where('a.padre = :padre_id');
103 12
                $qb->andWhere('a.attivo = :attivo');
104 12
                $qb->orderBy('a.padre', 'ASC');
105 12
                $qb->orderBy('a.ordine', 'ASC');
106 12
                $qb->setParameter('padre_id', $item->getId());
107 12
                $qb->setParameter('attivo', true);
108 12
                $submenu = $qb->getQuery()->getResult();
109
110 12
                $sottomenutabelle = $this->getSubMenu($submenu);
111
112 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...
113 12
                $risposta[] = array(
114 12
                    'percorso' => $percorso,
115 12
                    'nome' => $item->getNome(),
116 12
                    'sottolivello' => $sottomenutabelle,
117 12
                    'target' => $item->getTarget(),
118 12
                    'notifiche' => $item->hasNotifiche(),
119 12
                    'tag' => $item->getTag(),
120 12
                    'percorsonotifiche' => $this->getUrlObject($item->getNome(), $item->getPercorsonotifiche(), ''),
121
                );
122 12
                unset($submenu);
123 12
                unset($sottomenutabelle);
124
            }
125
        }
126
127 12
        return $risposta;
128
    }
129
130 12
    protected function getSubMenu($submenu)
131
    {
132 12
        $sottomenutabelle = array();
133 12
        foreach ($submenu as $subitem) {
134 12
            $visualizzare = true;
135 12
            if ($subitem->isAutorizzazionerichiesta()) {
136
                $permessi = new \Cdf\BiCoreBundle\Utils\Permessi\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...
137
                $visualizzare = $permessi->canRead();
138
            }
139
140 12
            if ($visualizzare) {
141 12
                $vettoresottomenu = $this->getMenu(array($subitem));
142 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...
143
144 12
                if (isset($sottomenu['sottolivello']) && count($sottomenu['sottolivello']) > 0) {
145 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...
146 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...
147 11
                    $sottomenutabelle[] = array_merge($menuobj, $sottolivellomenu);
148
                } else {
149 12
                    $sottomenutabelle[] = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
150
                }
151
            }
152
        }
153
154 12
        return $sottomenutabelle;
155
    }
156
157 12
    protected function getUrlObject($nome, $percorso, $target)
158
    {
159 12
        if ($this->routeExists($percorso)) {
160 12
            $percorso = $this->urlgenerator->generate($percorso);
161
        } else {
162 12
            $percorso = "#";
0 ignored issues
show
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...
163
        }
164 12
        if (!$target) {
165 12
            $target = '_self';
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 ($retcode === 200 || $retcode === 401) {
210
            $exist = true;
211
        } else {
212
            $exist = false;
213
        }
214
        curl_close($ch);
215
216
        return $exist;
217
    }
218
}
219