1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\ControlDB\Export\Handler; |
4
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Export\FormattedItem; |
6
|
|
|
use BristolSU\ControlDB\Export\Formatter\Formatter; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Facades\Log; |
9
|
|
|
|
10
|
|
|
abstract class Handler |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
/** |
|
|
|
|
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $config; |
|
|
|
|
16
|
|
|
|
17
|
13 |
|
public function __construct(array $config) |
|
|
|
|
18
|
|
|
{ |
19
|
13 |
|
$this->config = $config; |
20
|
13 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Prepare items by transforming them to formattable items |
24
|
|
|
* |
25
|
|
|
* @param $items |
|
|
|
|
26
|
|
|
* @return FormattedItem[] |
|
|
|
|
27
|
|
|
*/ |
28
|
6 |
|
protected function prepareItems($items) |
29
|
|
|
{ |
30
|
6 |
|
$formattedItems = []; |
31
|
6 |
|
foreach($items as $item) { |
|
|
|
|
32
|
6 |
|
$formattedItems[] = FormattedItem::create($item); |
33
|
|
|
} |
34
|
6 |
|
return $formattedItems; |
35
|
|
|
} |
36
|
|
|
|
37
|
6 |
|
public function export($items = []) |
|
|
|
|
38
|
|
|
{ |
39
|
6 |
|
if($items instanceof Collection) { |
|
|
|
|
40
|
1 |
|
$items = $items->all(); |
41
|
|
|
} |
42
|
6 |
|
$formattedItems = $this->prepareItems($items); |
43
|
6 |
|
foreach($this->getFormatters() as $formatter) { |
|
|
|
|
44
|
5 |
|
$time=-hrtime(true); |
45
|
5 |
|
$formattedItems = $formatter->format($formattedItems); |
46
|
5 |
|
$time+=hrtime(true); |
47
|
5 |
|
$this->logTime(class_basename($formatter), $time / 1e+9); |
48
|
|
|
} |
49
|
5 |
|
$this->save($formattedItems); |
50
|
5 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
|
|
|
|
53
|
|
|
* @return Formatter[] |
54
|
|
|
*/ |
55
|
6 |
|
protected function getFormatters() |
56
|
|
|
{ |
57
|
|
|
return array_map(function($className) { |
|
|
|
|
58
|
6 |
|
if(class_exists($className)) { |
|
|
|
|
59
|
5 |
|
return new $className($this->config('formatters')[$className]); |
60
|
|
|
} |
61
|
1 |
|
throw new \Exception(sprintf('Formatter %s does not exist', $className)); |
62
|
6 |
|
}, array_keys($this->config('formatters', []))); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
|
|
|
|
66
|
|
|
* @param FormattedItem[] $items |
|
|
|
|
67
|
|
|
* @return mixed |
|
|
|
|
68
|
|
|
*/ |
69
|
|
|
abstract protected function save(array $items); |
70
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* @param string $key |
|
|
|
|
73
|
|
|
* @param null $default |
|
|
|
|
74
|
|
|
* |
75
|
|
|
* @return mixed|null |
76
|
|
|
*/ |
77
|
11 |
|
protected function config(string $key, $default = null) |
78
|
|
|
{ |
79
|
11 |
|
if(array_key_exists($key, $this->config)) { |
|
|
|
|
80
|
11 |
|
return $this->config[$key]; |
81
|
|
|
} |
82
|
3 |
|
return $default; |
83
|
|
|
} |
84
|
|
|
|
85
|
5 |
|
private function logTime(string $formatter, float $time) |
|
|
|
|
86
|
|
|
{ |
87
|
5 |
|
if(config('control.log-formatters')) { |
|
|
|
|
88
|
|
|
Log::info(sprintf('Formatter [%s] took %.2f s to run', $formatter, $time)); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |