Lines 88.46% 23 / 26
Methods 80.00% 4 / 5
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
 execute 81.25% 13 / 16 0.00% 0 / 1 7.32
 toArray 100.00% 7 / 7 100.00% 1 / 1 1
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}