Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (success)
87.50%
21 / 24
87.50% covered (success)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConfigUpdateOperation
87.50% covered (success)
87.50%
21 / 24
87.50% covered (success)
87.50%
7 / 8
15.44
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDescription
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 toArray
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 formatValue
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
7.33
1<?php
2
3/**
4 * Configuration update operation for migrations.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2023-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2026-01-25
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Setup\Migration\Operations;
21
22use phpMyFAQ\Configuration;
23
24readonly class ConfigUpdateOperation implements OperationInterface
25{
26    public function __construct(
27        private Configuration $configuration,
28        private string $key,
29        private mixed $value,
30    ) {
31    }
32
33    public function getType(): string
34    {
35        return 'config_update';
36    }
37
38    public function getDescription(): string
39    {
40        $displayValue = $this->formatValue($this->value);
41        return sprintf('Update configuration: %s = %s', $this->key, $displayValue);
42    }
43
44    public function getKey(): string
45    {
46        return $this->key;
47    }
48
49    public function getValue(): mixed
50    {
51        return $this->value;
52    }
53
54    public function execute(): bool
55    {
56        $value = $this->value;
57        $normalizedValue = is_scalar($value) || $value instanceof \Stringable ? (string) $value : '';
58
59        return $this->configuration->update([$this->key => $normalizedValue]);
60    }
61
62    public function toArray(): array
63    {
64        return [
65            'type' => $this->getType(),
66            'description' => $this->getDescription(),
67            'key' => $this->key,
68            'value' => $this->value,
69        ];
70    }
71
72    private function formatValue(mixed $value): string
73    {
74        if (is_bool($value)) {
75            return $value ? 'true' : 'false';
76        }
77        if (is_string($value)) {
78            if (strlen($value) > 50) {
79                return "'" . substr($value, offset: 0, length: 47) . "...'";
80            }
81            return "'{$value}'";
82        }
83        if (is_null($value)) {
84            return 'null';
85        }
86        return (string) $value;
87    }
88}