Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
64 / 64 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
AccountUserEditLogic | |
100.00% |
64 / 64 |
|
100.00% |
6 / 6 |
9 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
startup | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
validateImpl | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
executeImpl | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
executeInitialize | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
executeSubmit | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\App\Models\Domain\Page\Account; |
6 | |
7 | use PeServer\App\Models\AppDatabaseCache; |
8 | use PeServer\App\Models\AuditLog; |
9 | use PeServer\App\Models\Dao\Entities\UsersEntityDao; |
10 | use PeServer\App\Models\Domain\AccountValidator; |
11 | use PeServer\App\Models\Domain\Page\PageLogicBase; |
12 | use PeServer\App\Models\Data\SessionAccount; |
13 | use PeServer\App\Models\SessionKey; |
14 | use PeServer\Core\Database\IDatabaseContext; |
15 | use PeServer\Core\I18n; |
16 | use PeServer\Core\Mvc\LogicCallMode; |
17 | use PeServer\Core\Mvc\LogicParameter; |
18 | |
19 | class AccountUserEditLogic extends PageLogicBase |
20 | { |
21 | public function __construct(LogicParameter $parameter, private AppDatabaseCache $dbCache) |
22 | { |
23 | parent::__construct($parameter); |
24 | } |
25 | |
26 | #region PageLogicBase |
27 | |
28 | protected function startup(LogicCallMode $callMode): void |
29 | { |
30 | $this->registerParameterKeys([ |
31 | 'account_edit_name', |
32 | 'account_edit_website', |
33 | 'account_edit_description', |
34 | ], true); |
35 | } |
36 | |
37 | protected function validateImpl(LogicCallMode $callMode): void |
38 | { |
39 | if ($callMode === LogicCallMode::Initialize) { |
40 | return; |
41 | } |
42 | |
43 | $this->validation('account_edit_name', function (string $key, string $value) { |
44 | $accountValidator = new AccountValidator($this, $this->validator); |
45 | $accountValidator->isUserName($key, $value); |
46 | }); |
47 | |
48 | $this->validation('account_edit_website', function (string $key, string $value) { |
49 | $accountValidator = new AccountValidator($this, $this->validator); |
50 | $accountValidator->isWebsite($key, $value); |
51 | }); |
52 | |
53 | $this->validation('account_edit_description', function (string $key, string $value) { |
54 | $accountValidator = new AccountValidator($this, $this->validator); |
55 | $accountValidator->isDescription($key, $value); |
56 | }); |
57 | } |
58 | |
59 | protected function executeImpl(LogicCallMode $callMode): void |
60 | { |
61 | if ($callMode === LogicCallMode::Initialize) { |
62 | $this->executeInitialize($callMode); |
63 | } else { |
64 | $this->executeSubmit($callMode); |
65 | } |
66 | } |
67 | |
68 | private function executeInitialize(LogicCallMode $callMode): void |
69 | { |
70 | $userInfo = $this->requireSession(SessionKey::ACCOUNT); |
71 | |
72 | $database = $this->openDatabase(); |
73 | $usersEntityDao = new UsersEntityDao($database); |
74 | |
75 | $userEditData = $usersEntityDao->selectUserEditData($userInfo->userId); |
76 | |
77 | $map = [ |
78 | 'name' => 'account_edit_name', |
79 | 'website' => 'account_edit_website', |
80 | 'description' => 'account_edit_description', |
81 | ]; |
82 | |
83 | foreach ($userEditData->fields as $key => $value) { |
84 | $this->setValue($map[$key], $value); |
85 | } |
86 | } |
87 | |
88 | private function executeSubmit(LogicCallMode $callMode): void |
89 | { |
90 | $userInfo = $this->requireSession(SessionKey::ACCOUNT); |
91 | |
92 | $params = [ |
93 | 'user_id' => $userInfo->userId, |
94 | 'user_name' => $this->getRequest('account_edit_name'), |
95 | 'website' => $this->getRequest('account_edit_website'), |
96 | 'description' => $this->getRequest('account_edit_description'), |
97 | ]; |
98 | |
99 | $database = $this->openDatabase(); |
100 | |
101 | $database->transaction(function (IDatabaseContext $context) use ($params) { |
102 | $usersEntityDao = new UsersEntityDao($context); |
103 | |
104 | // ユーザー情報更新 |
105 | $usersEntityDao->updateUserSetting( |
106 | $params['user_id'], |
107 | $params['user_name'], |
108 | $params['website'], |
109 | $params['description'] |
110 | ); |
111 | |
112 | $this->writeAuditLogCurrentUser(AuditLog::USER_EDIT, null, $context); |
113 | |
114 | return true; |
115 | }); |
116 | |
117 | |
118 | $source = $this->requireSession(SessionKey::ACCOUNT); |
119 | $account = new SessionAccount( |
120 | $source->userId, |
121 | $source->loginId, |
122 | $params['user_name'], |
123 | $source->level, |
124 | $source->state |
125 | ); |
126 | $this->setSession(SessionKey::ACCOUNT, $account); |
127 | |
128 | $this->addTemporaryMessage(I18n::message('message/flash/updated_user')); |
129 | $this->dbCache->exportUserInformation(); |
130 | } |
131 | |
132 | #endregion |
133 | } |