Lines 84.21% 16 / 19
Methods 50.00% 1 / 2
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 connect 57.14% 4 / 7 0.00% 0 / 1 2.31
 validConfig 100.00% 12 / 12 100.00% 1 / 1 7
16final class CsvConnector implements ConnectorInterface
17{
18    /**
19     * Create a CSV connection from the given config.
20     *
21     * @param  array{path: mixed, readonly?: bool, ...<mixed>}  $config
22     * @return ConnectionInterface
23     * @throws \InvalidArgumentException
24     */
25    #[Override]
26    public function connect(array $config): ConnectionInterface
27    {
28        $this->validConfig($config);
29
30        $path = $config['path'];
31
32        if (!is_string($path)) {
33            throw new \InvalidArgumentException(
34                'CSV connection requires a "path" string; got ' . get_debug_type($path) . '.'
35            );
36        }
37
38        return new CsvConnection($path, (bool) ($config['readonly'] ?? false));
39    }
40
41    /**
42     * Validate the shape of a CSV connection config.
43     *
44     * @param  array<string,mixed>  $config
45     * @throws \InvalidArgumentException
46     */
47    #[Override]
48    public function validConfig(array $config): void
49    {
50        if (!isset($config['path']) || !is_string($config['path']) || $config['path'] === '') {
51            throw new \InvalidArgumentException(
52                'CSV requires a non-empty "path" string; got '
53                    . (isset($config['path']) ? get_debug_type($config['path']) : 'nothing')
54                    . '.'
55            );
56        }
57
58        if (array_key_exists('readonly', $config) && !is_bool($config['readonly'])) {
59            throw new \InvalidArgumentException(
60                'CSV requires a "readonly" boolean; got '
61                    . get_debug_type($config['readonly'])
62                    . '.'
63            );
64        }
65    }
66}