Lines 65.62% 21 / 32
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% 1 / 1 100.00% 1 / 1 1
 toArray 100.00% 6 / 6 100.00% 1 / 1 1
 formatValue 42.10% 8 / 19 0.00% 0 / 1 39.94
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}