HasCode   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 7
eloc 14
dl 0
loc 42
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A canUpdateCode() 0 10 2
A setCode() 0 11 4
A getCode() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Traits;
6
7
use Application\Model\User;
8
use Doctrine\ORM\Mapping as ORM;
9
use Ecodev\Felix\Api\Exception;
10
11
trait HasCode
12
{
13
    #[ORM\Column(type: 'string', length: 30, nullable: true)]
14
    private ?string $code = null;
15
16
    /**
17
     * Set code.
18
     */
19 2
    public function setCode(?string $code): void
20
    {
21 2
        if ($code === '') {
22
            $code = null;
23
        }
24
25 2
        if ($code !== $this->code && !$this->canUpdateCode()) {
26
            throw new Exception('Only majors and administrators are allowed to update card.code');
27
        }
28
29 2
        $this->code = $code;
30
    }
31
32
    /**
33
     * Get code.
34
     */
35 7
    public function getCode(): ?string
36
    {
37 7
        return $this->code;
38
    }
39
40
    /**
41
     * Simple ACL check.
42
     */
43 5
    private function canUpdateCode(): bool
44
    {
45 5
        $whitelist = [
46 5
            User::ROLE_MAJOR,
47 5
            User::ROLE_ADMINISTRATOR,
48 5
        ];
49
50 5
        $currentRole = User::getCurrent() ? User::getCurrent()->getRole() : User::ROLE_ANONYMOUS;
51
52 5
        return in_array($currentRole, $whitelist, true);
53
    }
54
}
55