Lines 54.16% 13 / 24
Methods 87.50% 7 / 8
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 getType 100.00% 1 / 1 100.00% 1 / 1 1
 getDescription 100.00% 1 / 1 100.00% 1 / 1 1
 getPermissionName 100.00% 1 / 1 100.00% 1 / 1 1
 getPermissionDescription 100.00% 1 / 1 100.00% 1 / 1 1
 getUserId 100.00% 1 / 1 100.00% 1 / 1 1
 execute 0.00% 0 / 11 0.00% 0 / 1 12
 toArray 100.00% 7 / 7 100.00% 1 / 1 1
25readonly class PermissionGrantOperation implements OperationInterface
26{
27    public function __construct(
28        private Configuration $configuration,
29        private string $permissionName,
30        private string $permissionDescription,
31        private int $userId = 1,
32    ) {
33    }
34
35    public function getType(): string
36    {
37        return 'permission_grant';
38    }
39
40    public function getDescription(): string
41    {
42        return sprintf('Add permission: %s (%s)', $this->permissionName, $this->permissionDescription);
43    }
44
45    public function getPermissionName(): string
46    {
47        return $this->permissionName;
48    }
49
50    public function getPermissionDescription(): string
51    {
52        return $this->permissionDescription;
53    }
54
55    public function getUserId(): int
56    {
57        return $this->userId;
58    }
59
60    public function execute(): bool
61    {
62        try {
63            $user = new User($this->configuration);
64            $rightData = [
65                'name' => $this->permissionName,
66                'description' => $this->permissionDescription,
67            ];
68            $rightId = $user->perm->addRight($rightData);
69            if ($rightId <= 0) {
70                return false;
71            }
72
73            return $user->perm->grantUserRight($this->userId, $rightId);
74        } catch (\Exception) {
75            return false;
76        }
77    }
78
79    public function toArray(): array
80    {
81        return [
82            'type' => $this->getType(),
83            'description' => $this->getDescription(),
84            'permissionName' => $this->permissionName,
85            'permissionDescription' => $this->permissionDescription,
86            'userId' => $this->userId,
87        ];
88    }
89}