Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
54.17% covered (warning)
54.17%
13 / 24
87.50% covered (success)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
PermissionGrantOperation
54.17% covered (warning)
54.17%
13 / 24
87.50% covered (success)
87.50%
7 / 8
19.63
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDescription
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPermissionName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPermissionDescription
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUserId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 toArray
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Permission grant operation for migrations.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2023-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2026-01-25
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Setup\Migration\Operations;
21
22use phpMyFAQ\Configuration;
23use phpMyFAQ\User;
24
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}