1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\ControlDB\Export; |
4
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Group; |
6
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Position; |
7
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Role; |
8
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\User; |
9
|
|
|
use Illuminate\Console\Command; |
10
|
|
|
use Symfony\Component\Console\Exception\InvalidArgumentException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Seed the database |
14
|
|
|
*/ |
|
|
|
|
15
|
|
|
class ExportControlCommand extends Command |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Signature for the command |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $signature = 'control:export |
23
|
|
|
{type : The type of resource to export} |
24
|
|
|
{--exporter= : The name of the exporter to use}'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Name for the commmand |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $name = 'Export Control'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Description of the command |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $description = 'Export control to an external service'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Handle the command execution |
42
|
|
|
* |
43
|
|
|
* Seed the database with fake data. |
44
|
|
|
*/ |
|
|
|
|
45
|
5 |
|
public function handle() |
46
|
|
|
{ |
47
|
5 |
|
$time=-hrtime(true); |
48
|
5 |
|
Exporter::driver($this->option('exporter'))->export($this->exportData()); |
|
|
|
|
49
|
5 |
|
$this->info('Export complete'); |
50
|
5 |
|
$time+=hrtime(true); |
51
|
5 |
|
$this->info(sprintf('Export took %.2f s to run', $time / 1e+9)); |
52
|
5 |
|
} |
53
|
|
|
|
54
|
5 |
|
private function exportData() |
|
|
|
|
55
|
|
|
{ |
56
|
5 |
|
switch($this->argument('type')) { |
57
|
5 |
|
case 'user': |
|
|
|
|
58
|
2 |
|
return app(User::class)->all(); |
59
|
|
|
break; |
|
|
|
|
60
|
3 |
|
case 'group': |
|
|
|
|
61
|
1 |
|
return app(Group::class)->all(); |
62
|
|
|
break; |
63
|
2 |
|
case 'role': |
|
|
|
|
64
|
1 |
|
return app(Role::class)->all(); |
65
|
|
|
break; |
66
|
1 |
|
case 'position': |
|
|
|
|
67
|
1 |
|
return app(Position::class)->all(); |
68
|
|
|
break; |
69
|
|
|
default: |
|
|
|
|
70
|
|
|
throw new InvalidArgumentException(sprintf('The type option %s is not allowed.', $this->argument('type'))); |
|
|
|
|
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |