Completed
Push — master ( df26d5...50dd5b )
by Andrea
17:50 queued 14:41
created

MenuExtension   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Test Coverage

Coverage 73.11%

Importance

Changes 0
Metric Value
eloc 123
dl 0
loc 195
ccs 87
cts 119
cp 0.7311
rs 10
c 0
b 0
f 0
wmc 28

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A urlExists() 0 9 3
A checkUrl() 0 23 4
A getSubMenu() 0 25 6
A generamenu() 0 48 4
A routeExists() 0 8 3
A getUrlObject() 0 6 2
A getFunctions() 0 6 1
A getMenu() 0 43 4
1
<?php
2
3
namespace Cdf\BiCoreBundle\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 19
    public function __construct(ObjectManager $em, UrlGeneratorInterface $urlgenerator, TokenStorageInterface $user, $rootpath)
18
    {
19 19
        $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 19
        $this->urlgenerator = $urlgenerator;
21 19
        $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 19
        $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 19
    }
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 11
    public function generamenu(\Twig_Environment $environment)
34
    {
35 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

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 11
        $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 11
        $rispostahome[] = array('percorso' => $this->getUrlObject(getenv('bicorebundle_appname'), $router, ''),
38 11
            'nome' => 'Home',
39 11
            'target' => '',
40
        );
41
42 11
        $em = $this->em;
43
        /* @var $qb \Doctrine\ORM\QueryBuilder */
44 11
        $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 11
        $qb->select(array('a'));
46 11
        $qb->from('BiCoreBundle:Menuapplicazione', 'a');
47 11
        $qb->where('a.attivo = :attivo and (a.padre is null or a.padre = 0)');
48 11
        $qb->setParameter('attivo', true);
49 11
        $qb->orderBy('a.padre', 'ASC');
50 11
        $qb->orderBy('a.ordine', 'ASC');
51 11
        $menu = $qb->getQuery()->getResult();
52
53 11
        $risposta = array_merge($rispostahome, $this->getMenu($menu));
54
55 11
        $pathmanuale = $this->rootpath . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'manuale.pdf';
56 11
        $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 11
        $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 11
        if (file_exists($pathmanuale)) {
60
            $risposta[] = array(
61
                'percorso' => $this->getUrlObject('Manuale', $pathmanuale, '_blank'),
62
                'nome' => 'Manuale', 'target' => '_blank');
63
        }
64
65 11
        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

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

164
        if ((null === $router->/** @scrutinizer ignore-call */ getRouteCollection()->get($name)) ? false : true) {
Loading history...
165 11
            return true;
166
        } else {
167 11
            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