Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 71 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ManagementDefaultPluginLogic | |
0.00% |
0 / 71 |
|
0.00% |
0 / 4 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
startup | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
validateImpl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
executeImpl | |
0.00% |
0 / 61 |
|
0.00% |
0 / 1 |
90 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\App\Models\Domain\Page\Management; |
6 | |
7 | use PeServer\App\Models\AppConfiguration; |
8 | use PeServer\App\Models\AppDatabaseCache; |
9 | use PeServer\App\Models\Dao\Entities\PeSettingEntityDao; |
10 | use PeServer\App\Models\Dao\Entities\PluginCategoryMappingsEntityDao; |
11 | use PeServer\App\Models\Dao\Entities\PluginsEntityDao; |
12 | use PeServer\App\Models\Dao\Entities\PluginUrlsEntityDao; |
13 | use PeServer\App\Models\Domain\DefaultPlugin; |
14 | use PeServer\App\Models\Domain\Page\PageLogicBase; |
15 | use PeServer\App\Models\Domain\PeVersionUpdater; |
16 | use PeServer\App\Models\Domain\PluginState; |
17 | use PeServer\App\Models\Domain\PluginUrlKey; |
18 | use PeServer\App\Models\Domain\PluginUtility; |
19 | use PeServer\App\Models\SessionKey; |
20 | use PeServer\Core\Collection\Arr; |
21 | use PeServer\Core\Database\IDatabaseContext; |
22 | use PeServer\Core\Mvc\LogicCallMode; |
23 | use PeServer\Core\Mvc\LogicParameter; |
24 | use PeServer\Core\Text; |
25 | use PeServer\Core\TypeUtility; |
26 | |
27 | class ManagementDefaultPluginLogic extends PageLogicBase |
28 | { |
29 | /** @var array{item:DefaultPlugin,registered:bool}[] */ |
30 | private array $defaultPlugins; |
31 | |
32 | public function __construct(LogicParameter $parameter, private AppDatabaseCache $dbCache, private AppConfiguration $config) |
33 | { |
34 | parent::__construct($parameter); |
35 | |
36 | $this->defaultPlugins = Arr::map(DefaultPlugin::get(), fn ($i) => [ |
37 | 'item' => $i, |
38 | 'registered' => false, |
39 | ]); |
40 | } |
41 | |
42 | protected function startup(LogicCallMode $callMode): void |
43 | { |
44 | $database = $this->openDatabase(); |
45 | |
46 | $pluginsEntityDao = new PluginsEntityDao($database); |
47 | for ($i = 0; $i < Arr::getCount($this->defaultPlugins); $i++) { |
48 | $this->defaultPlugins[$i]['registered'] = $pluginsEntityDao->selectExistsPluginId($this->defaultPlugins[$i]['item']->pluginId); |
49 | } |
50 | } |
51 | |
52 | protected function validateImpl(LogicCallMode $callMode): void |
53 | { |
54 | //NOP |
55 | } |
56 | |
57 | protected function executeImpl(LogicCallMode $callMode): void |
58 | { |
59 | if ($callMode === LogicCallMode::Initialize) { |
60 | $this->setValue('plugins', $this->defaultPlugins); |
61 | return; |
62 | } |
63 | |
64 | $account = $this->requireSession(SessionKey::ACCOUNT); |
65 | |
66 | if (TypeUtility::parseBoolean($this->getRequest('delete'))) { |
67 | $params = [ |
68 | 'plugins' => array_filter($this->defaultPlugins, function ($i) { |
69 | return $i['registered']; |
70 | }), |
71 | 'user_id' => $account->userId, |
72 | ]; |
73 | |
74 | if (Arr::getCount($params['plugins'])) { |
75 | $database = $this->openDatabase(); |
76 | $database->transaction(function (IDatabaseContext $context) use ($params) { |
77 | |
78 | foreach ($params['plugins'] as $plugin) { |
79 | /** @var $plugin array{item:DefaultPlugin,registered:bool}[] */ |
80 | |
81 | PluginUtility::removePlugin($context, $plugin['item']->pluginId); |
82 | $this->addTemporaryMessage('削除: ' . $plugin['item']->pluginName); |
83 | } |
84 | |
85 | return true; |
86 | }); |
87 | |
88 | $this->dbCache->exportPluginInformation(); |
89 | } else { |
90 | $this->addTemporaryMessage('なにも削除されず'); |
91 | } |
92 | } else { |
93 | $params = [ |
94 | 'plugins' => array_filter($this->defaultPlugins, function ($i) { |
95 | return !$i['registered']; |
96 | }), |
97 | 'user_id' => $account->userId, |
98 | ]; |
99 | |
100 | if (Arr::getCount($params['plugins'])) { |
101 | $database = $this->openDatabase(); |
102 | $database->transaction(function (IDatabaseContext $context) use ($params) { |
103 | $pluginsEntityDao = new PluginsEntityDao($context); |
104 | $pluginUrlsEntityDao = new PluginUrlsEntityDao($context); |
105 | $pluginCategoryMappingsEntityDao = new PluginCategoryMappingsEntityDao($context); |
106 | $peSettingEntityDao = new PeSettingEntityDao($context); |
107 | |
108 | foreach ($params['plugins'] as $plugin) { |
109 | /** @var $plugin array{item:DefaultPlugin,registered:bool}[] */ |
110 | |
111 | $pluginsEntityDao->insertPlugin( |
112 | $plugin['item']->pluginId, |
113 | $params['user_id'], |
114 | $plugin['item']->pluginName, |
115 | $plugin['item']->pluginName, |
116 | PluginState::ENABLED, |
117 | Text::join("\n\n", $plugin['item']->descriptions), |
118 | 'Pe専用プラグイン' |
119 | ); |
120 | |
121 | $map = [ |
122 | PluginUrlKey::CHECK => $plugin['item']->checkUrl, |
123 | PluginUrlKey::PROJECT => $plugin['item']->projectUrl, |
124 | PluginUrlKey::LANDING => Text::EMPTY, |
125 | ]; |
126 | foreach ($map as $k => $v) { |
127 | $pluginUrlsEntityDao->insertUrl($plugin['item']->pluginId, $k, $v); |
128 | } |
129 | |
130 | foreach ($plugin['item']->categories as $categoryId) { |
131 | $pluginCategoryMappingsEntityDao->insertPluginCategoryMapping($plugin['item']->pluginId, $categoryId); |
132 | } |
133 | |
134 | $this->addTemporaryMessage('登録: ' . $plugin['item']->pluginName); |
135 | } |
136 | |
137 | $version = $peSettingEntityDao->selectPeSettingVersion(); |
138 | |
139 | $peVersionUpdater = new PeVersionUpdater(); |
140 | $peVersionUpdater->updateDatabase($context, $this->config->setting->config->address->families->pluginUpdateInfoUrlBase, $version); |
141 | |
142 | return true; |
143 | }); |
144 | |
145 | $this->dbCache->exportPluginInformation(); |
146 | } else { |
147 | $this->addTemporaryMessage('なにも登録されず'); |
148 | } |
149 | } |
150 | } |
151 | } |