Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
1.32% |
1 / 76 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
Installer | |
1.32% |
1 / 76 |
|
0.00% |
0 / 3 |
329.38 | |
0.00% |
0 / 1 |
install | |
14.29% |
1 / 7 |
|
0.00% |
0 / 1 |
4.52 | |||
createTaskAttributeTypes | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
42 | |||
createTaskAttributeValues | |
0.00% |
0 / 37 |
|
0.00% |
0 / 1 |
110 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package Modules\Tasks\Admin |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace Modules\Tasks\Admin; |
16 | |
17 | use phpOMS\Application\ApplicationAbstract; |
18 | use phpOMS\Config\SettingsInterface; |
19 | use phpOMS\Message\Http\HttpRequest; |
20 | use phpOMS\Message\Http\HttpResponse; |
21 | use phpOMS\Module\InstallerAbstract; |
22 | use phpOMS\Module\ModuleInfo; |
23 | use phpOMS\Uri\HttpUri; |
24 | |
25 | /** |
26 | * Installer class. |
27 | * |
28 | * @package Modules\Tasks\Admin |
29 | * @license OMS License 2.0 |
30 | * @link https://jingga.app |
31 | * @since 1.0.0 |
32 | */ |
33 | final class Installer extends InstallerAbstract |
34 | { |
35 | /** |
36 | * Path of the file |
37 | * |
38 | * @var string |
39 | * @since 1.0.0 |
40 | */ |
41 | public const PATH = __DIR__; |
42 | |
43 | /** |
44 | * {@inheritdoc} |
45 | */ |
46 | public static function install(ApplicationAbstract $app, ModuleInfo $info, SettingsInterface $cfgHandler) : void |
47 | { |
48 | parent::install($app, $info, $cfgHandler); |
49 | |
50 | /* Attributes */ |
51 | $fileContent = \file_get_contents(__DIR__ . '/Install/attributes.json'); |
52 | if ($fileContent === false) { |
53 | return; |
54 | } |
55 | |
56 | /** @var array $attributes */ |
57 | $attributes = \json_decode($fileContent, true); |
58 | $attrTypes = self::createTaskAttributeTypes($app, $attributes); |
59 | $attrValues = self::createTaskAttributeValues($app, $attrTypes, $attributes); |
60 | } |
61 | |
62 | /** |
63 | * Install default attribute types |
64 | * |
65 | * @param ApplicationAbstract $app Application |
66 | * @param array $attributes Attribute definition |
67 | * |
68 | * @return array |
69 | * |
70 | * @since 1.0.0 |
71 | */ |
72 | private static function createTaskAttributeTypes(ApplicationAbstract $app, array $attributes) : array |
73 | { |
74 | /** @var array<string, array> $taskAttrType */ |
75 | $taskAttrType = []; |
76 | |
77 | /** @var \Modules\Tasks\Controller\ApiController $module */ |
78 | $module = $app->moduleManager->getModuleInstance('Tasks'); |
79 | |
80 | /** @var array $attribute */ |
81 | foreach ($attributes as $attribute) { |
82 | $response = new HttpResponse(); |
83 | $request = new HttpRequest(new HttpUri('')); |
84 | |
85 | $request->header->account = 1; |
86 | $request->setData('name', $attribute['name'] ?? ''); |
87 | $request->setData('title', \reset($attribute['l11n'])); |
88 | $request->setData('language', \array_keys($attribute['l11n'])[0] ?? 'en'); |
89 | $request->setData('is_required', $attribute['is_required'] ?? false); |
90 | $request->setData('custom', $attribute['is_custom_allowed'] ?? false); |
91 | $request->setData('validation_pattern', $attribute['validation_pattern'] ?? ''); |
92 | |
93 | $module->apiTaskAttributeTypeCreate($request, $response); |
94 | |
95 | $responseData = $response->getData(''); |
96 | if (!\is_array($responseData)) { |
97 | continue; |
98 | } |
99 | |
100 | $taskAttrType[$attribute['name']] = \is_array($responseData['response']) |
101 | ? $responseData['response'] |
102 | : $responseData['response']->toArray(); |
103 | |
104 | $isFirst = true; |
105 | foreach ($attribute['l11n'] as $language => $l11n) { |
106 | if ($isFirst) { |
107 | $isFirst = false; |
108 | continue; |
109 | } |
110 | |
111 | $response = new HttpResponse(); |
112 | $request = new HttpRequest(new HttpUri('')); |
113 | |
114 | $request->header->account = 1; |
115 | $request->setData('title', $l11n); |
116 | $request->setData('language', $language); |
117 | $request->setData('type', $taskAttrType[$attribute['name']]['id']); |
118 | |
119 | $module->apiTaskAttributeTypeL11nCreate($request, $response); |
120 | } |
121 | } |
122 | |
123 | return $taskAttrType; |
124 | } |
125 | |
126 | /** |
127 | * Create default attribute values for types |
128 | * |
129 | * @param ApplicationAbstract $app Application |
130 | * @param array $taskAttrType Attribute types |
131 | * @param array $attributes Attribute definition |
132 | * |
133 | * @return array |
134 | * |
135 | * @since 1.0.0 |
136 | */ |
137 | private static function createTaskAttributeValues(ApplicationAbstract $app, array $taskAttrType, array $attributes) : array |
138 | { |
139 | /** @var array<string, array> $taskAttrValue */ |
140 | $taskAttrValue = []; |
141 | |
142 | /** @var \Modules\Tasks\Controller\ApiController $module */ |
143 | $module = $app->moduleManager->getModuleInstance('Tasks'); |
144 | |
145 | foreach ($attributes as $attribute) { |
146 | $taskAttrValue[$attribute['name']] = []; |
147 | |
148 | /** @var array $value */ |
149 | foreach ($attribute['values'] as $value) { |
150 | $response = new HttpResponse(); |
151 | $request = new HttpRequest(new HttpUri('')); |
152 | |
153 | $request->header->account = 1; |
154 | $request->setData('value', $value['value'] ?? ''); |
155 | $request->setData('value_type', $attribute['value_type'] ?? 0); |
156 | $request->setData('unit', $value['unit'] ?? ''); |
157 | $request->setData('default', isset($attribute['values']) && !empty($attribute['values'])); |
158 | $request->setData('attributetype', $taskAttrType[$attribute['name']]['id']); |
159 | |
160 | if (isset($value['l11n']) && !empty($value['l11n'])) { |
161 | $request->setData('title', \reset($value['l11n'])); |
162 | $request->setData('language', \array_keys($value['l11n'])[0] ?? 'en'); |
163 | } |
164 | |
165 | $module->apiTaskAttributeValueCreate($request, $response); |
166 | |
167 | $responseData = $response->getData(''); |
168 | if (!\is_array($responseData)) { |
169 | continue; |
170 | } |
171 | |
172 | $attrValue = \is_array($responseData['response']) |
173 | ? $responseData['response'] |
174 | : $responseData['response']->toArray(); |
175 | |
176 | $taskAttrValue[$attribute['name']][] = $attrValue; |
177 | |
178 | $isFirst = true; |
179 | foreach (($value['l11n'] ?? []) as $language => $l11n) { |
180 | if ($isFirst) { |
181 | $isFirst = false; |
182 | continue; |
183 | } |
184 | |
185 | $response = new HttpResponse(); |
186 | $request = new HttpRequest(new HttpUri('')); |
187 | |
188 | $request->header->account = 1; |
189 | $request->setData('title', $l11n); |
190 | $request->setData('language', $language); |
191 | $request->setData('value', $attrValue['id']); |
192 | |
193 | $module->apiTaskAttributeValueL11nCreate($request, $response); |
194 | } |
195 | } |
196 | } |
197 | |
198 | return $taskAttrValue; |
199 | } |
200 | } |