Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.46% covered (success)
88.46%
23 / 26
80.00% covered (success)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserCreateOperation
88.46% covered (success)
88.46%
23 / 26
80.00% covered (success)
80.00%
4 / 5
11.19
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
 execute
81.25% covered (success)
81.25%
13 / 16
0.00% covered (danger)
0.00%
0 / 1
7.32
 toArray
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Operation for creating a user during installation.
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 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-31
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Setup\Migration\Operations;
21
22use phpMyFAQ\Configuration;
23use phpMyFAQ\User;
24use SensitiveParameter;
25
26readonly class UserCreateOperation implements OperationInterface
27{
28    /* @mago-expect lint:excessive-parameter-list - mirrors the user-creation fields of the migration format */
29    public function __construct(
30        private Configuration $configuration,
31        private string $loginName,
32        #[SensitiveParameter]
33        private string $password,
34        private string $displayName,
35        private string $email,
36        private int $userId,
37        private bool $isSuperAdmin = false,
38        private string $status = 'protected',
39    ) {
40    }
41
42    public function getType(): string
43    {
44        return 'user_create';
45    }
46
47    public function getDescription(): string
48    {
49        return sprintf('Create user "%s" (ID: %d)', $this->loginName, $this->userId);
50    }
51
52    public function execute(): bool
53    {
54        try {
55            $user = new User($this->configuration);
56            if (!$user->createUser($this->loginName, $this->password, '', $this->userId)) {
57                return false;
58            }
59
60            if (!$user->setStatus($this->status)) {
61                return false;
62            }
63
64            $userData = [
65                'display_name' => $this->displayName,
66                'email' => $this->email,
67            ];
68            if (!$user->setUserData($userData)) {
69                return false;
70            }
71
72            if ($this->isSuperAdmin && !$user->setSuperAdmin(true)) {
73                return false;
74            }
75
76            return true;
77        } catch (\Throwable) {
78            return false;
79        }
80    }
81
82    public function toArray(): array
83    {
84        return [
85            'type' => $this->getType(),
86            'description' => $this->getDescription(),
87            'login_name' => $this->loginName,
88            'user_id' => $this->userId,
89            'is_super_admin' => $this->isSuperAdmin,
90        ];
91    }
92}