1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OhDear\ForgeSync; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use OhDear\PhpSdk\OhDear; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
|
9
|
|
|
class SyncSitesCommand extends Command |
10
|
|
|
{ |
11
|
|
|
/** @var \OhDear\ForgeSync\ForgeSync */ |
12
|
|
|
protected $sync; |
13
|
|
|
|
14
|
|
|
/** @var \Illuminate\Support\Collection */ |
15
|
|
|
protected $availableTeams; |
16
|
|
|
|
17
|
|
|
/** @var \Illuminate\Support\Collection */ |
18
|
|
|
protected $syncableSites; |
19
|
|
|
|
20
|
|
|
protected $signature = 'ohdear:forge-sync {--ohDearKey=} {--forgeKey=} {--dry-run}'; |
21
|
|
|
|
22
|
|
|
protected $description = 'Import existing Laravel Forge sites to Oh Dear!'; |
23
|
|
|
|
24
|
|
|
public function handle() |
25
|
|
|
{ |
26
|
|
|
$ohDearTeamId = $this->askOhDearTeamId(); |
27
|
|
|
|
28
|
|
|
$this->info('Scanning your Forge sites... (this could need a little bit of time).'); |
29
|
|
|
|
30
|
|
|
$this->sync = new ForgeSync($ohDearTeamId, $this->option('ohDearKey'), $this->option('forgeKey')); |
31
|
|
|
|
32
|
|
|
$this->syncableSites = $this->sync->sites(); |
33
|
|
|
|
34
|
|
|
if ($this->syncableSites->count() === 0) { |
35
|
|
|
$this->warn("You don't have any sites that can be imported!"); |
36
|
|
|
|
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
if ($this->option('dry-run')) { |
40
|
|
|
$this->warn('Running in dry-mode: we will not make any changes to your Oh Dear! account.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$choice = $this->choice('Which Forge sites should be imported into Oh Dear?', $this->siteChoices()); |
44
|
|
|
|
45
|
|
|
$this->syncSites($choice); |
46
|
|
|
|
47
|
|
|
$this->info('All done'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function askOhDearTeamId(): int |
51
|
|
|
{ |
52
|
|
|
$choice = $this->choice( |
53
|
|
|
'Please select the Oh Dear! team into which you would like to import these sites.', |
54
|
|
|
$this->teamChoices() |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
return $this->availableTeams->filter(function ($team) use ($choice) { |
58
|
|
|
return "<comment>{$team['name']}</comment>" === $choice; |
59
|
|
|
})->first()['id']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function syncSites(string $siteChoice) |
63
|
|
|
{ |
64
|
|
|
if (str_is('<comment>All Sites</comment>', $siteChoice)) { |
65
|
|
|
$this->info('Importing all sites...'); |
66
|
|
|
|
67
|
|
|
$this->syncableSites |
68
|
|
|
->filter(function (Site $site) use ($siteChoice) { |
69
|
|
|
if ($siteChoice === '<comment>All Sites</comment>') { |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$url = str_replace(['<comment>', '</comment>'], '', $siteChoice); |
74
|
|
|
|
75
|
|
|
return $site->url() === $url; |
76
|
|
|
}) |
77
|
|
|
->each(function (Site $site) { |
78
|
|
|
if ($this->option('dry-run')) { |
79
|
|
|
$this->comment("Would have added site `{$site->url()}` if not running in dry-mode."); |
80
|
|
|
|
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$this->sync->addToOhDear($site); |
86
|
|
|
|
87
|
|
|
$this->comment("Added site `{$site->url()}`"); |
88
|
|
|
} catch (Exception $exception) { |
89
|
|
|
$this->error("Could not add site `{$site->url()}` because {$exception->getMessage()}"); |
90
|
|
|
} |
91
|
|
|
}); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function teamChoices(): array |
96
|
|
|
{ |
97
|
|
|
$ohDear = new OhDear($this->option('ohDearKey') ?? config('forge-sync.ohdear_api_token')); |
98
|
|
|
|
99
|
|
|
$this->availableTeams = collect($ohDear->me()->teams['data']->attributes); |
100
|
|
|
|
101
|
|
|
return $this->availableTeams->map(function ($team) { |
102
|
|
|
return "<comment>{$team['name']}</comment>"; |
103
|
|
|
})->toArray(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function siteChoices(): array |
107
|
|
|
{ |
108
|
|
|
return $this->syncableSites->map(function (Site $site) { |
109
|
|
|
return "<comment>{$site->url()}</comment>"; |
110
|
|
|
})->merge(['<comment>All Sites</comment>'])->toArray(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|