Completed
Push — master ( 040ef3...d11a3f )
by Andrea
59:12 queued 55:09
created

MenuExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Twig\Extension;
4
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\AbstractExtension
10
{
11
    protected $em;
12
    protected $urlgenerator;
13
    protected $user;
14
15 26
    public function __construct(ObjectManager $em, UrlGeneratorInterface $urlgenerator, TokenStorageInterface $user, $rootpath)
16
    {
17 26
        $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...
18 26
        $this->urlgenerator = $urlgenerator;
19 26
        $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...
20 26
        $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...
21 26
    }
22
23
    public function getFunctions()
24
    {
25
        return [
26
            new \Twig_SimpleFunction('generamenu', [$this, 'generamenu'], [
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

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

26
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFunction('generamenu', [$this, 'generamenu'], [
Loading history...
27
                'needs_environment' => true,
28
                'is_safe' => ['html'],
29
                ]),
30
        ];
31
    }
32
33 12
    public function generamenu(\Twig_Environment $environment)
34
    {
35 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

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

44
        /** @scrutinizer ignore-call */ 
45
        $qb = $em->createQueryBuilder();
Loading history...
45 12
        $qb->select(array('a'));
46 12
        $qb->from('BiCoreBundle:Menuapplicazione', 'a');
47 12
        $qb->where('a.attivo = :attivo and (a.padre is null or a.padre = 0)');
48 12
        $qb->setParameter('attivo', true);
49 12
        $qb->orderBy('a.padre', 'ASC');
50 12
        $qb->orderBy('a.ordine', 'ASC');
51 12
        $menu = $qb->getQuery()->getResult();
52
53 12
        $risposta = array_merge($rispostahome, $this->getMenu($menu));
54
55 12
        $pathmanuale = $this->rootpath.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'manuale.pdf';
56 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...
57 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...
58
59 12
        if (file_exists($pathmanuale)) {
60
            $risposta[] = array(
61
                'percorso' => $this->getUrlObject('Manuale', $pathmanuale, '_blank'),
62
                'nome' => 'Manuale', 'target' => '_blank', );
63
        }
64
65 12
        if ('ssocdf' === $this->user->getToken()->getProviderKey()) {
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 ('ssocdf' === $this->user->getToken()->/** @scrutinizer ignore-call */ getProviderKey()) {
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 12
        if ('ssolineacomune' === $this->user->getToken()->getProviderKey()) {
71
            $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
            $urlLogout = $this->urlgenerator->generate('fi_Lineacomuneauth_signout');
73
        }
74
75 12
        if ('main' === $this->user->getToken()->getProviderKey()) {
76 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...
77 12
            $urlLogout = $this->urlgenerator->generate('fos_user_security_logout');
78
        }
79
80 12
        $risposta[] = array('percorso' => $this->getUrlObject($username, '', ''), 'nome' => $username, 'target' => '',
81
            'sottolivello' => array(
82 12
                array('percorso' => $urlLogout, 'nome' => 'Logout', 'target' => '_self'),
83
            ),
84
        );
85
86 12
        return $environment->render('BiCoreBundle:Menu:menu.html.twig', array('risposta' => $risposta));
87
    }
88
89 12
    protected function getMenu($menu)
90
    {
91 12
        $risposta = array();
92 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...
93
94 12
        foreach ($menu as $item) {
95 12
            $visualizzare = true;
96
97 12
            if ($item->isAutorizzazionerichiesta()) {
98 12
                $permessi = new \Cdf\BiCoreBundle\Service\Permessi\PermessiManager($this->em, $this->user);
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...
99 12
                $visualizzare = $permessi->canRead($item->getTag());
100
            }
101
102 12
            if ($visualizzare) {
103 12
                $qb = $em->createQueryBuilder();
104 12
                $qb->select(array('a'));
105 12
                $qb->from('BiCoreBundle:Menuapplicazione', 'a');
106 12
                $qb->where('a.padre = :padre_id');
107 12
                $qb->andWhere('a.attivo = :attivo');
108 12
                $qb->orderBy('a.padre', 'ASC');
109 12
                $qb->orderBy('a.ordine', 'ASC');
110 12
                $qb->setParameter('padre_id', $item->getId());
111 12
                $qb->setParameter('attivo', true);
112 12
                $submenu = $qb->getQuery()->getResult();
113
114 12
                $sottomenutabelle = $this->getSubMenu($submenu);
115
116 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...
117 12
                $risposta[] = array(
118 12
                    'percorso' => $percorso,
119 12
                    'nome' => $item->getNome(),
120 12
                    'sottolivello' => $sottomenutabelle,
121 12
                    'target' => $item->getTarget(),
122 12
                    'notifiche' => $item->hasNotifiche(),
123 12
                    'tag' => $item->getTag(),
124 12
                    'percorsonotifiche' => $this->getUrlObject($item->getNome(), $item->getPercorsonotifiche(), ''),
125
                );
126 12
                unset($submenu);
127 12
                unset($sottomenutabelle);
128
            }
129
        }
130
131 12
        return $risposta;
132
    }
133
134 12
    protected function getSubMenu($submenu)
135
    {
136 12
        $sottomenutabelle = array();
137 12
        foreach ($submenu as $subitem) {
138 12
            $visualizzare = true;
139 12
            if ($subitem->isAutorizzazionerichiesta()) {
140
                $permessi = new \Cdf\BiCoreBundle\Service\Permessi\PermessiManager($this->em, $this->user);
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...
141
                $visualizzare = $permessi->canRead($subitem->getTag());
142
            }
143
144 12
            if ($visualizzare) {
145 12
                $vettoresottomenu = $this->getMenu(array($subitem));
146 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...
147
148 12
                if (isset($sottomenu['sottolivello']) && count($sottomenu['sottolivello']) > 0) {
149 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...
150 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...
151 11
                    $sottomenutabelle[] = array_merge($menuobj, $sottolivellomenu);
152
                } else {
153 12
                    $sottomenutabelle[] = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget());
154
                }
155
            }
156
        }
157
158 12
        return $sottomenutabelle;
159
    }
160
161 12
    protected function getUrlObject($nome, $percorso, $target)
162
    {
163 12
        if ($this->routeExists($percorso)) {
164 12
            $percorso = $this->urlgenerator->generate($percorso);
165
        } else {
166 12
            $percorso = '#';
167
        }
168 12
        if (!$target) {
169 12
            $target = '_self';
170
        }
171
172 12
        return array('percorso' => $percorso, 'nome' => $nome, 'target' => $target);
173
    }
174
175 12
    protected function routeExists($name)
176
    {
177 12
        $router = $this->urlgenerator;
178
179 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

179
        if ((null === $router->/** @scrutinizer ignore-call */ getRouteCollection()->get($name)) ? false : true) {
Loading history...
180 12
            return true;
181
        } else {
182 12
            return false;
183
        }
184
    }
185
186
    protected function urlExists($name)
187
    {
188
        if ($this->checkUrl($name, false)) {
189
            return true;
190
        } else {
191
            if ($this->checkUrl($name, true)) {
192
                return true;
193
            } else {
194
                return false;
195
            }
196
        }
197
    }
198
199
    protected function checkUrl($name, $proxy)
200
    {
201
        $ch = curl_init($name);
202
203
        curl_setopt($ch, CURLOPT_URL, $name);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

203
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $name);
Loading history...
204
        if ($proxy) {
205
            curl_setopt($ch, CURLOPT_PROXY, $proxy);
206
        } else {
207
            curl_setopt($ch, CURLOPT_PROXY, null);
208
        }
209
        curl_setopt($ch, CURLOPT_NOBODY, true);
210
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
211
        curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout in seconds
212
        curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

212
        curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
213
        $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_getinfo() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

213
        $retcode = curl_getinfo(/** @scrutinizer ignore-type */ $ch, CURLINFO_HTTP_CODE);
Loading history...
214
        if (200 === $retcode || 401 === $retcode) {
215
            $exist = true;
216
        } else {
217
            $exist = false;
218
        }
219
        curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

219
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
220
221
        return $exist;
222
    }
223
}
224