Passed
Push — master ( 6806fd...ae3585 )
by Andrea
11:55 queued 34s
created

GestionePermessi::leggere()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 5
nop 1
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Fi\CoreBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
6
7
/*
8
 * Se c'è l'accoppiata UTENTE + MODULO allora vale quel permesso
9
 * Se c'è l'accoppiata RUOLO + MODULO allora vale quel permesso
10
 * Altrimenti solo MODULO
11
 * Se non trovo informazioni di sorta, il modulo è chiuso
12
 */
13
14
class GestionePermessi
15
{
16
17
    protected $modulo;
18
    protected $crud;
19
    private $container;
20
21 14
    public function __construct(Container $container)
22
    {
23 14
        $this->container = $container;
24 14
    }
25
26 12
    private function presente($lettera)
27
    {
28 12
        if (stripos($this->crud, $lettera) !== false) {
29 11
            return true;
30
        } else {
31 2
            return false;
32
        }
33
    }
34
35 13
    public function leggere($parametri = array())
36
    {
37 13
        if (!$this->container->get('security.token_storage')->getToken()) {
38 1
            return null;
39
        }
40 12
        if (isset($parametri['modulo'])) {
41 12
            $this->modulo = $parametri['modulo'];
42 12
        }
43
44 12
        $this->setCrud();
45
46 12
        return $this->presente('R') || ($this->isSuperAdmin()); //SuperAdmin
47
    }
48
49 13
    public function cancellare($parametri = array())
50
    {
51 13
        if (!$this->container->get('security.token_storage')->getToken()) {
52 1
            return null;
53
        }
54 12
        if (isset($parametri['modulo'])) {
55 12
            $this->modulo = $parametri['modulo'];
56 12
        }
57 12
        $this->setCrud();
58
59 12
        return $this->presente('D') || ($this->isSuperAdmin()); //SuperAdmin
60
    }
61
62 13
    public function creare($parametri = array())
63
    {
64 13
        if (!$this->container->get('security.token_storage')->getToken()) {
65 1
            return null;
66
        }
67 12
        if (isset($parametri['modulo'])) {
68 12
            $this->modulo = $parametri['modulo'];
69 12
        }
70 12
        $this->setCrud();
71
72 12
        return $this->presente('C') || ($this->isSuperAdmin()); //SuperAdmin
73
    }
74
75 13
    public function aggiornare($parametri = array())
76
    {
77 13
        if (!$this->container->get('security.token_storage')->getToken()) {
78 1
            return null;
79
        }
80
81 12
        if (isset($parametri['modulo'])) {
82 12
            $this->modulo = $parametri['modulo'];
83 12
        }
84 12
        $this->setCrud();
85
86 12
        return $this->presente('U') || ($this->isSuperAdmin()); //SuperAdmin
87
    }
88
89 2
    private function isSuperAdmin()
90
    {
91 2
        $utente = $this->container->get('security.token_storage')->getToken()->getUser()->getId();
92 2
        $q = $this->container->get('doctrine')
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...
93 2
                ->getRepository('FiCoreBundle:Operatori')
94 2
                ->find($utente);
95
96 2
        $isSuperAdmin = false;
97 2
        if ($q) {
98 2
            if ($q->getRuoli()) {
99 2
                $isSuperAdmin = $q->getRuoli()->isSuperadmin();
100 2
            }
101 2
        }
102 2
        return $isSuperAdmin;
103
    }
104
105
    public function sulmenu($parametri = array())
106
    {
107
        if (isset($parametri['modulo'])) {
108
            $this->modulo = $parametri['modulo'];
109
        }
110
        $permesso = $this->leggere($parametri) ||
111
                $this->cancellare($parametri) ||
112
                $this->creare($parametri) ||
113
                $this->aggiornare($parametri);
114
115
        if ($permesso) {
116
            return true;
117
        }
118
119
        return false;
120
    }
121
122 12
    public function setCrud($parametri = array())
123
    {
124 12
        if (isset($parametri['modulo'])) {
125
            $this->modulo = $parametri['modulo'];
126
        }
127
128 12
        $utentecorrente = $this->utentecorrente();
129
130 12
        $q = $this->container->get('doctrine')
131 12
                ->getRepository('FiCoreBundle:Permessi')
132 12
                ->findOneBy(array('operatori_id' => $utentecorrente['id'], 'modulo' => $this->modulo));
133
134 12
        if ($q) {
135
            $this->crud = $q->getCrud();
136
            return;
137
        }
138
139 12
        $q = $this->container->get('doctrine')
140 12
                ->getRepository('FiCoreBundle:Permessi')
141 12
                ->findOneBy(array('ruoli_id' => $utentecorrente['ruolo_id'], 'modulo' => $this->modulo, 'operatori_id' => null));
142
143 12
        if ($q) {
144 11
            $this->crud = $q->getCrud();
145 11
            return;
146
        }
147
148 1
        $q = $this->container->get('doctrine')
149 1
                ->getRepository('FiCoreBundle:Permessi')
150 1
                ->findOneBy(array('ruoli_id' => null, 'modulo' => $this->modulo, 'operatori_id' => null));
151
152 1
        if ($q) {
153
            $this->crud = $q->getCrud();
154
            return;
155
        }
156
157 1
        $this->crud = '';
158 1
    }
159
160 13
    public function utentecorrente()
161
    {
162 13
        $utentecorrente = array();
163
164 13
        if (!$this->container->get('security.token_storage')->getToken()) {
165 1
            $utentecorrente['nome'] = 'Utente non registrato';
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...
166 1
            $utentecorrente['id'] = 0;
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...
167 1
            $utentecorrente['ruolo_id'] = 0;
168
169 1
            return $utentecorrente;
170
        }
171
172 12
        $utente = $this->container->get('security.token_storage')->getToken()->getUser()->getId();
173 12
        $q = $this->container->get('doctrine')
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...
174 12
                ->getRepository('FiCoreBundle:Operatori')
175 12
                ->find($utente);
176
177 12
        $utentecorrente['username'] = $utente;
178 12
        $utentecorrente['codice'] = $utente;
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...
179
180 12
        if (!$q) {
181
            $utentecorrente['nome'] = 'Utente non registrato';
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...
182
            $utentecorrente['id'] = 0;
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...
183
            $utentecorrente['ruolo_id'] = 0;
184
185
            return $utentecorrente;
186
        }
187
188 12
        $utentecorrente['nome'] = $q->getOperatore();
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...
189 12
        $utentecorrente['id'] = $q->getId();
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...
190 12
        $utentecorrente['ruolo_id'] = ($q->getRuoli() ? $q->getRuoli()->getId() : 0);
191
192 12
        return $utentecorrente;
193
    }
194
195 13
    public function impostaPermessi($parametri = array())
196
    {
197 13
        $risposta = array();
198
199 13
        $risposta['permessiedit'] = $this->aggiornare($parametri);
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...
200 13
        $risposta['permessidelete'] = $this->cancellare($parametri);
201 13
        $risposta['permessicreate'] = $this->creare($parametri);
202 13
        $risposta['permessiread'] = $this->leggere($parametri);
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...
203
204 13
        return $risposta;
205
    }
206
}
207