Lines
84.21%
16 / 19
Functions and Methods
50.00%
1 / 2
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| CsvConnector | 84.21% | 16 / 19 | 50.00% | 1 / 2 | 9.32 | 0.00% | 0 / 1 | |||
| connect | 57.14% | 4 / 7 | 0.00% | 0 / 1 | 2.31 | |||||
| validConfig | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 7 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace BlueprintAU\Radiant\Database\Connectors; | |
| 6 | ||
| 7 | use BlueprintAU\Radiant\Database\Connections\ConnectionInterface; | |
| 8 | use BlueprintAU\Radiant\Database\Connections\CsvConnection; | |
| 9 | use Override; | |
| 10 | ||
| 11 | /** | |
| 12 | * CSV connector — builds a {@see CsvConnection} from config. | |
| 13 | * | |
| 14 | * @see \BlueprintAU\Radiant\Database\Connections\CsvConnection | |
| 15 | */ | |
| 16 | final 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 | } |