bristol-su /
control
| 1 | <?php |
||
| 2 | |||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 3 | namespace BristolSU\ControlDB\Export; |
||
| 4 | |||
| 5 | use BristolSU\ControlDB\Export\Handler\Airtable\AirtableHandler; |
||
|
0 ignored issues
–
show
The type
BristolSU\ControlDB\Expo...irtable\AirtableHandler was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 6 | use BristolSU\ControlDB\Export\Handler\DumpHandler; |
||
| 7 | use BristolSU\ControlDB\Export\Handler\Handler; |
||
| 8 | use BristolSU\ControlDB\Export\Handler\SaveCsvHandler; |
||
| 9 | use Closure; |
||
| 10 | use Illuminate\Contracts\Container\Container; |
||
| 11 | use Illuminate\Support\Str; |
||
| 12 | use InvalidArgumentException; |
||
| 13 | |||
| 14 | class ExportManager |
||
|
0 ignored issues
–
show
|
|||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The container instance. |
||
| 19 | * |
||
| 20 | * @var Container |
||
| 21 | */ |
||
| 22 | protected $container; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The registered custom driver creators. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $customCreators = []; |
||
| 30 | |||
| 31 | protected $formatters = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create a new Export manager instance. |
||
| 35 | * |
||
| 36 | * @param Container $container |
||
|
0 ignored issues
–
show
|
|||
| 37 | * @return void |
||
|
0 ignored issues
–
show
|
|||
| 38 | */ |
||
| 39 | 15 | public function __construct(Container $container) |
|
| 40 | { |
||
| 41 | 15 | $this->container = $container; |
|
| 42 | 15 | } |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Get a export driver instance. |
||
| 46 | * |
||
| 47 | * @param string|null $driver |
||
|
0 ignored issues
–
show
|
|||
| 48 | * @return mixed |
||
|
0 ignored issues
–
show
|
|||
| 49 | */ |
||
| 50 | 15 | public function driver($driver = null) |
|
| 51 | { |
||
| 52 | 15 | return $this->resolve($driver ?? $this->getDefaultDriver()); |
|
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Resolve the given export instance by name. |
||
| 57 | * |
||
| 58 | * @param string $name |
||
|
0 ignored issues
–
show
|
|||
| 59 | * @return Handler |
||
|
0 ignored issues
–
show
|
|||
| 60 | * |
||
| 61 | * @throws \InvalidArgumentException |
||
| 62 | */ |
||
| 63 | 15 | protected function resolve($name) |
|
| 64 | { |
||
| 65 | 15 | $config = $this->configurationFor($name); |
|
| 66 | |||
| 67 | 15 | if (is_null($config)) { |
|
|
0 ignored issues
–
show
|
|||
| 68 | 1 | throw new InvalidArgumentException("Exporter [{$name}] is not defined."); |
|
| 69 | } |
||
| 70 | 14 | if (isset($this->customCreators[$config['driver']])) { |
|
| 71 | 8 | return $this->callCustomCreator($config); |
|
| 72 | } |
||
| 73 | |||
| 74 | 6 | $driverMethod = 'create'.Str::studly($config['driver']).'Driver'; |
|
| 75 | |||
| 76 | 6 | if (method_exists($this, $driverMethod)) { |
|
| 77 | 5 | return $this->{$driverMethod}($config); |
|
| 78 | } |
||
| 79 | |||
| 80 | 1 | throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Call a custom driver creator. |
||
| 85 | * |
||
| 86 | * @param array $config |
||
|
0 ignored issues
–
show
|
|||
| 87 | * @return mixed |
||
|
0 ignored issues
–
show
|
|||
| 88 | */ |
||
| 89 | 8 | protected function callCustomCreator(array $config) |
|
| 90 | { |
||
| 91 | 8 | return $this->customCreators[$config['driver']]($this->container, $config); |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get the export connection configuration. |
||
| 96 | * |
||
| 97 | * @param string $name |
||
|
0 ignored issues
–
show
|
|||
| 98 | * @return array |
||
|
0 ignored issues
–
show
|
|||
| 99 | */ |
||
| 100 | 15 | protected function configurationFor($name) |
|
| 101 | { |
||
| 102 | 15 | $config = $this->container['config']["control.export.{$name}"]; |
|
| 103 | 15 | if(is_null($config)) { |
|
|
0 ignored issues
–
show
|
|||
| 104 | 1 | return null; |
|
| 105 | } |
||
| 106 | 14 | if(!isset($config['formatters'])) { |
|
|
0 ignored issues
–
show
|
|||
| 107 | 13 | $config['formatters'] = []; |
|
| 108 | } |
||
| 109 | 14 | foreach($this->formatters() as $formatter => $formatterConfig) { |
|
|
0 ignored issues
–
show
|
|||
| 110 | 2 | $config['formatters'][$formatter] = $formatterConfig; |
|
| 111 | } |
||
| 112 | |||
| 113 | 14 | return $config; |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the default export driver name. |
||
| 118 | * |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | 6 | public function getDefaultDriver() |
|
| 122 | { |
||
| 123 | 6 | return $this->container['config']['control.export.default']; |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Register a custom driver creator Closure. |
||
| 128 | * |
||
| 129 | * @param string $driver |
||
|
0 ignored issues
–
show
|
|||
| 130 | * @param \Closure $callback |
||
|
0 ignored issues
–
show
|
|||
| 131 | * @return $this |
||
|
0 ignored issues
–
show
|
|||
| 132 | */ |
||
| 133 | 8 | public function extend($driver, Closure $callback) |
|
| 134 | { |
||
| 135 | 8 | $this->customCreators[$driver] = $callback->bindTo($this, $this); |
|
| 136 | |||
| 137 | 8 | return $this; |
|
| 138 | } |
||
| 139 | |||
| 140 | 2 | public function withFormatter(string $formatter, array $config = []) |
|
|
0 ignored issues
–
show
|
|||
| 141 | { |
||
| 142 | 2 | $this->formatters[$formatter] = $config; |
|
| 143 | |||
| 144 | 2 | return $this; |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Dynamically call the default driver instance. |
||
| 149 | * |
||
| 150 | * @param string $method |
||
|
0 ignored issues
–
show
|
|||
| 151 | * @param array $parameters |
||
|
0 ignored issues
–
show
|
|||
| 152 | * @return mixed |
||
|
0 ignored issues
–
show
|
|||
| 153 | */ |
||
| 154 | 1 | public function __call($method, $parameters) |
|
| 155 | { |
||
| 156 | 1 | return $this->driver()->$method(...$parameters); |
|
| 157 | } |
||
| 158 | |||
| 159 | 1 | public function createDumpDriver(array $config) |
|
|
0 ignored issues
–
show
|
|||
| 160 | { |
||
| 161 | 1 | return new DumpHandler($config); |
|
| 162 | } |
||
| 163 | |||
| 164 | 1 | public function createSaveCsvDriver(array $config) |
|
|
0 ignored issues
–
show
|
|||
| 165 | { |
||
| 166 | 1 | return new SaveCsvHandler($config); |
|
| 167 | } |
||
| 168 | |||
| 169 | 14 | protected function formatters() |
|
|
0 ignored issues
–
show
|
|||
| 170 | { |
||
| 171 | 14 | return $this->formatters; |
|
| 172 | } |
||
| 173 | |||
| 174 | } |