Completed
Push — 4.1 ( c9c1fa...5cccd6 )
by Andrea
13:13
created

MenuExtension::getSubMenu()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.0702

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 25
ccs 14
cts 16
cp 0.875
rs 9.1111
c 0
b 0
f 0
cc 6
nc 7
nop 1
crap 6.0702
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 $urlgenerator;
16
    protected $user;
17
    
18 10
    public function __construct(ContainerInterface $container, ObjectManager $em, UrlGeneratorInterface $urlgenerator, TokenStorageInterface $user)
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 10
        $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...
24 10
    }
25
    public function getFunctions()
26
    {
27
        return [
28
            new \Twig_SimpleFunction('generamenu', [$this, 'generamenu'], [
29
                '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()->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
        $risposta = array();
86 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...
87
88 7
        foreach ($menu as $item) {
89 7
            $visualizzare = true;
90
91 7
            if ($item->isAutorizzazionerichiesta()) {
92
                $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...
93
                $visualizzare = $permessi->canRead();
94
            }
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()->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
        $sottomenutabelle = array();
130 7
        foreach ($submenu as $subitem) {
131 7
            $visualizzare = true;
132 7
            if ($subitem->isAutorizzazionerichiesta()) {
133
                $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...
134
                $visualizzare = $permessi->canRead();
135
            }
136
137 7
            if ($visualizzare) {
138 7
                $vettoresottomenu = $this->getMenu(array($subitem));
139 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...
140
141 7
                if (isset($sottomenu['sottolivello']) && count($sottomenu['sottolivello']) > 0) {
142 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...
143 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...
144 7
                    $sottomenutabelle[] = array_merge($menuobj, $sottolivellomenu);
145
                } else {
146 7
                    $sottomenutabelle[] = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
147
                }
148
            }
149
        }
150
151 7
        return $sottomenutabelle;
152
    }
153 7
    protected function getUrlObject($nome, $percorso, $target)
154
    {
155 7
        if ($this->routeExists($percorso)) {
156 7
            return array('percorso' => $this->urlgenerator->generate($percorso), 'nome' => $nome, 'target' => $target);
157
        } else {
158 7
            return array('percorso' => $percorso, 'nome' => $nome, 'target' => $target);
159
        }
160
    }
161 7
    protected function routeExists($name)
162
    {
163 7
        $router = $this->container->get('router');
164
165 7
        if ((null === $router->getRouteCollection()->get($name)) ? false : true) {
166 7
            return true;
167
        } else {
168 7
            return false;
169
        }
170
    }
171
    protected function urlExists($name)
172
    {
173
        if ($this->checkUrl($name, false)) {
174
            return true;
175
        } else {
176
            if ($this->checkUrl($name, true)) {
177
                return true;
178
            } else {
179
                return false;
180
            }
181
        }
182
    }
183
    protected function checkUrl($name, $proxy)
184
    {
185
        $ch = curl_init($name);
186
187
        curl_setopt($ch, CURLOPT_URL, $name);
188
        if ($proxy) {
189
            curl_setopt($ch, CURLOPT_PROXY, 'proxyhttp.comune.intranet:8080');
190
        } else {
191
            curl_setopt($ch, CURLOPT_PROXY, null);
192
        }
193
        curl_setopt($ch, CURLOPT_NOBODY, true);
194
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
195
        curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout in seconds
196
        curl_exec($ch);
197
        $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
198
        if ($retcode === 200 || $retcode === 401) {
199
            $exist = true;
200
        } else {
201
            $exist = false;
202
        }
203
        curl_close($ch);
204
205
        return $exist;
206
    }
207
}
208