* Persist source settings and run post-save health checks for enabled sources. * * @param array $body * @return array */
(array $body)
| 21 | * @return array<string,mixed> |
| 22 | */ |
| 23 | public static function save(array $body): array |
| 24 | { |
| 25 | $didWrite = false; |
| 26 | $resultSource = null; |
| 27 | |
| 28 | if (array_key_exists('enabled', $body)) { |
| 29 | $enabled = (bool)$body['enabled']; |
| 30 | $ok = SourcesConfig::saveEnabled($enabled); |
| 31 | if (!$ok) { |
| 32 | throw new RuntimeException('Failed to save sources setting', 500); |
| 33 | } |
| 34 | $didWrite = true; |
| 35 | } |
| 36 | |
| 37 | if (isset($body['source'])) { |
| 38 | if (!is_array($body['source'])) { |
| 39 | throw new RuntimeException('Invalid source payload', 400); |
| 40 | } |
| 41 | $res = SourcesConfig::upsertSource($body['source']); |
| 42 | if (empty($res['ok'])) { |
| 43 | throw new RuntimeException((string)($res['error'] ?? 'Failed to save source'), 400); |
| 44 | } |
| 45 | $resultSource = isset($res['source']) && is_array($res['source']) ? $res['source'] : null; |
| 46 | $didWrite = true; |
| 47 | } |
| 48 | |
| 49 | if (!$didWrite) { |
| 50 | throw new RuntimeException('No changes provided', 400); |
| 51 | } |
| 52 | |
| 53 | $sourceFallback = (isset($body['source']) && is_array($body['source'])) ? $body['source'] : null; |
| 54 | $auto = self::autoTestAndMaybeDisable($resultSource, $sourceFallback); |
| 55 | $resultSource = $auto['resultSource']; |
| 56 | |
| 57 | self::refreshPublicSiteConfig(); |
| 58 | |
| 59 | return [ |
| 60 | 'source' => $resultSource, |
| 61 | 'autoTested' => $auto['autoTested'], |
| 62 | 'autoTestOk' => $auto['autoTestOk'], |
| 63 | 'autoTestLimited' => $auto['autoTestLimited'], |
| 64 | 'autoTestError' => $auto['autoTestError'], |
| 65 | 'autoTest' => $auto['autoTest'], |
| 66 | 'autoDisabled' => $auto['autoDisabled'], |
| 67 | 'autoDisableFailed' => $auto['autoDisableFailed'], |
| 68 | ]; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Execute a source health check for one source id. |
no test coverage detected