PermessiManager::canUpdate()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

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