Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
65.62% covered (warning)
65.62%
21 / 32
87.50% covered (success)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConfigAddOperation
65.62% covered (warning)
65.62%
21 / 32
87.50% covered (success)
87.50%
7 / 8
33.66
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%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 formatValue
42.11% covered (danger)
42.11%
8 / 19
0.00% covered (danger)
0.00%
0 / 1
39.94
1<?php
2
3/**
4 * Configuration add 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 ConfigAddOperation 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_add';
36    }
37
38    public function getDescription(): string
39    {
40        $displayValue = $this->formatValue($this->value);
41        return sprintf('Add 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        return $this->configuration->add($this->key, $this->value);
57    }
58
59    public function toArray(): array
60    {
61        return [
62            'type' => $this->getType(),
63            'description' => $this->getDescription(),
64            'key' => $this->key,
65            'value' => $this->value,
66        ];
67    }
68
69    private function formatValue(mixed $value): string
70    {
71        if (is_bool($value)) {
72            return $value ? 'true' : 'false';
73        }
74        if (is_string($value)) {
75            if (strlen($value) > 50) {
76                return "'" . substr($value, offset: 0, length: 47) . "...'";
77            }
78            return "'{$value}'";
79        }
80        if (is_null($value)) {
81            return 'null';
82        }
83        if (is_array($value)) {
84            $json = json_encode($value);
85            if ($json !== false && strlen($json) <= 50) {
86                return "'" . $json . "'";
87            }
88            return "'[array]...'";
89        }
90        if (is_object($value)) {
91            $json = json_encode($value);
92            if ($json !== false && strlen($json) <= 50) {
93                return "'" . $json . "'";
94            }
95            return "'[object " . get_class($value) . "]'";
96        }
97        return (string) $value;
98    }
99}