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 $syncableSites; |
16
|
|
|
|
17
|
|
|
protected $signature = 'ohdear:forge-sync {--ohDearKey=} {--forgeKey=} {--dry-run}'; |
18
|
|
|
|
19
|
|
|
protected $description = 'Sync existing Laravel Forge Sites to Oh Dear!'; |
20
|
|
|
|
21
|
|
|
public function handle() |
22
|
|
|
{ |
23
|
|
|
$ohDearTeamId = $this->askOhDearTeamId(); |
24
|
|
|
|
25
|
|
|
$this->info('Scan your Forge sites... (this could need a little bit of time).'); |
26
|
|
|
|
27
|
|
|
$this->sync = new ForgeSync($ohDearTeamId, $this->option('ohDearKey'), $this->option('forgeKey')); |
|
|
|
|
28
|
|
|
|
29
|
|
|
$this->syncableSites = $this->sync->sites(); |
30
|
|
|
|
31
|
|
|
if ($this->syncableSites->count() === 0) { |
32
|
|
|
$this->warn("You don't have any sites that can be synced!"); |
33
|
|
|
|
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($this->option('dry-run') == true) { |
38
|
|
|
$this->warn("Dry-Run Mode: We don't create any Site at Oh Dear."); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$choice = $this->choice('Which Forge sites should be synced with Oh Dear?', $this->siteChoices()); |
42
|
|
|
|
43
|
|
|
$this->syncSites($choice); |
44
|
|
|
|
45
|
|
|
$this->info('All done'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function askOhDearTeamId(): int |
49
|
|
|
{ |
50
|
|
|
$choice = $this->choice( |
51
|
|
|
'Please select the Oh Dear! team that should be synced with your forge account.', |
52
|
|
|
$this->teamChoices() |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
//TO DO: $team seems to be a null value |
56
|
|
|
preg_match('/ID: ([0-9]+)/', $choice, $team); |
57
|
|
|
|
58
|
|
|
return last($team); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function syncSites(string $siteChoice) |
62
|
|
|
{ |
63
|
|
|
if (str_is('<comment>All Sites</comment>', $siteChoice)) { |
64
|
|
|
$this->info('Syncing all sites...'); |
65
|
|
|
|
66
|
|
|
$this->syncableSites |
67
|
|
|
->filter(function (Site $site) use ($siteChoice) { |
68
|
|
|
if ($siteChoice === '<comment>All Sites</comment>') { |
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$url = str_replace(['<comment>', '</comment>'], '', $siteChoice); |
73
|
|
|
|
74
|
|
|
return $site->url() === $url; |
75
|
|
|
}) |
76
|
|
|
->each(function (Site $site) { |
77
|
|
|
try { |
78
|
|
|
if ($this->option('dry-run') == null) { |
79
|
|
|
$this->sync->addToOhDear($site->url()); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->comment("Added site `{$site->url()}`"); |
83
|
|
|
} catch (Exception $exception) { |
84
|
|
|
$this->error("Could not add site `{$site->url()}` because {$exception->getMessage()}"); |
85
|
|
|
} |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function teamChoices(): array |
91
|
|
|
{ |
92
|
|
|
$ohDear = new OhDear($this->option('ohDearKey') ?? config('forge-sync.ohdear_api_token')); |
93
|
|
|
|
94
|
|
|
return collect($ohDear->me()->teams['data']->attributes)->map(function ($team) { |
95
|
|
|
return "<comment>{$team['name']}</comment>"; |
96
|
|
|
})->toArray(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function siteChoices(): array |
100
|
|
|
{ |
101
|
|
|
return $this->syncableSites->map(function (Site $site) { |
102
|
|
|
return "<comment>{$site->url()}</comment>"; |
103
|
|
|
})->merge(['<comment>All Sites</comment>'])->toArray(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.