Lines 87.50% 21 / 24
Methods 87.50% 7 / 8
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 getType 100.00% 1 / 1 100.00% 1 / 1 1
 getDescription 100.00% 2 / 2 100.00% 1 / 1 1
 getKey 100.00% 1 / 1 100.00% 1 / 1 1
 getValue 100.00% 1 / 1 100.00% 1 / 1 1
 execute 100.00% 3 / 3 100.00% 1 / 1 3
 toArray 100.00% 6 / 6 100.00% 1 / 1 1
 formatValue 66.66% 6 / 9 0.00% 0 / 1 7.33
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}