Passed
Push — master ( e3dde1...fec6ab )
by Andrea
24:31 queued 12s
created

PermessiManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Cdf\BiCoreBundle\Service\Permessi;
10
11
use Cdf\BiCoreBundle\Entity\Permessi;
12
use Doctrine\ORM\EntityManagerInterface;
13
14
class PermessiManager
15
{
16
17
    private EntityManagerInterface $em;
18
    private $user;
19
20 18
    public function __construct(EntityManagerInterface $em, $user)
21
    {
22 18
        $this->em = $em;
23 18
        $this->user = $user->getToken()->getUser();
24 18
    }
25
26 16
    public function canRead(string $modulo): bool
27
    {
28
29 16
        $permessi = $this->getPermessi($modulo);
30 16
        $canread = false;
31 16
        if ($permessi) {
32 4
            if (false !== stripos(strtoupper($permessi->getCrud()), 'R')) {
33 4
                $canread = true;
34
            }
35
        } else {
36 16
            if ($this->user->getRuoli() && $this->user->getRuoli()->isSuperadmin()) {
37 13
                $canread = true;
38
            }
39
        }
40
41 16
        return $canread;
42
    }
43
44 13
    public function canCreate(string $modulo): bool
45
    {
46 13
        $permessi = $this->getPermessi($modulo);
47 13
        $cancreate = false;
48 13
        if ($permessi) {
49 4
            if (false !== stripos(strtoupper($permessi->getCrud()), 'C')) {
50 4
                $cancreate = true;
51
            }
52
        } else {
53 10
            if ($this->user->getRuoli() && $this->user->getRuoli()->isSuperadmin()) {
54 9
                $cancreate = true;
55
            }
56
        }
57
58 13
        return $cancreate;
59
    }
60
61 15
    public function canUpdate(string $modulo): bool
62
    {
63 15
        $permessi = $this->getPermessi($modulo);
64 15
        $canupdate = false;
65 15
        if ($permessi) {
66 4
            if (false !== stripos(strtoupper($permessi->getCrud()), 'U')) {
67 4
                $canupdate = true;
68
            }
69
        } else {
70 12
            if ($this->user->getRuoli() && $this->user->getRuoli()->isSuperadmin()) {
71 10
                $canupdate = true;
72
            }
73
        }
74
75 15
        return $canupdate;
76
    }
77
78 13
    public function canDelete(string $modulo): bool
79
    {
80 13
        $permessi = $this->getPermessi($modulo);
81 13
        $candelete = false;
82 13
        if ($permessi) {
83 4
            if (false !== stripos(strtoupper($permessi->getCrud()), 'D')) {
84 4
                $candelete = true;
85
            }
86
        } else {
87 10
            if ($this->user->getRuoli() && $this->user->getRuoli()->isSuperadmin()) {
88 9
                $candelete = true;
89
            }
90
        }
91
92 13
        return $candelete;
93
    }
94
95 18
    private function getPermessi(string $modulo): ?Permessi
96
    {
97
        /** @var \Cdf\BiCoreBundle\Repository\PermessiRepository $repository */
98 18
        $repository = $this->em->getRepository(Permessi::class);
99 18
        return $repository->findPermessoModuloOperatore($modulo, $this->user);
100
    }
101
102
    /**
103
     *
104
     * @param string $modulo
105
     * @return array<string, bool>
106
     */
107 13
    public function toJson(string $modulo): array
108
    {
109
        return array(
110 13
            'read' => $this->canRead($modulo),
111 13
            'create' => $this->canCreate($modulo),
112 13
            'delete' => $this->canDelete($modulo),
113 13
            'update' => $this->canUpdate($modulo),
114
        );
115
    }
116
}
117