Completed
Pull Request — master (#6)
by Freek
03:29
created

SyncSitesCommand::siteChoices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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 = 'Sync existing Laravel Forge Sites to Oh Dear!';
23
24
    public function handle()
25
    {
26
        $ohDearTeamId = $this->askOhDearTeamId();
27
28
        $this->info('Scan your Forge sites... (this could need a little bit of time).');
29
30
        $this->sync = new ForgeSync($ohDearTeamId, $this->option('ohDearKey'), $this->option('forgeKey'));
0 ignored issues
show
Bug introduced by
It seems like $this->option('ohDearKey') targeting Illuminate\Console\Command::option() can also be of type array; however, OhDear\ForgeSync\ForgeSync::__construct() does only seem to accept null|string, maybe add an additional type check?

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.

Loading history...
Bug introduced by
It seems like $this->option('forgeKey') targeting Illuminate\Console\Command::option() can also be of type array; however, OhDear\ForgeSync\ForgeSync::__construct() does only seem to accept null|string, maybe add an additional type check?

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.

Loading history...
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 synced!");
36
37
            return;
38
        }
39
40
        if ($this->option('dry-run') == true) {
41
            $this->warn("Dry-Run Mode: We don't create any Site at Oh Dear.");
42
        }
43
44
        $choice = $this->choice('Which Forge sites should be synced with Oh Dear?', $this->siteChoices());
45
46
        $this->syncSites($choice);
47
48
        $this->info('All done');
49
    }
50
51
    protected function askOhDearTeamId(): int
52
    {
53
        $choice = $this->choice(
54
            'Please select the Oh Dear! team that should be synced with your forge account.',
55
            $this->teamChoices()
56
        );
57
58
        return $this->availableTeams->filter(function ($team) use ($choice) {
59
            return "<comment>{$team['name']}</comment>" == $choice;
60
        })->first()['id'];
61
    }
62
63
    protected function syncSites(string $siteChoice)
64
    {
65
        if (str_is('<comment>All Sites</comment>', $siteChoice)) {
66
            $this->info('Syncing all sites...');
67
68
            $this->syncableSites
69
                ->filter(function (Site $site) use ($siteChoice) {
70
                    if ($siteChoice === '<comment>All Sites</comment>') {
71
                        return true;
72
                    }
73
74
                    $url = str_replace(['<comment>', '</comment>'], '', $siteChoice);
75
76
                    return $site->url() === $url;
77
                })
78
                ->each(function (Site $site) {
79
                    try {
80
                        if ($this->option('dry-run') == null) {
81
                            $this->sync->addToOhDear($site);
82
                        }
83
84
                        $this->comment("Added site `{$site->url()}`");
85
                    } catch (Exception $exception) {
86
                        $this->error("Could not add site `{$site->url()}` because {$exception->getMessage()}");
87
                    }
88
                });
89
        }
90
    }
91
92
    protected function teamChoices(): array
93
    {
94
        $ohDear = new OhDear($this->option('ohDearKey') ?? config('forge-sync.ohdear_api_token'));
95
        $this->availableTeams = collect($ohDear->me()->teams['data']->attributes);
96
97
        return $this->availableTeams->map(function ($team) {
98
            return "<comment>{$team['name']}</comment>";
99
        })->toArray();
100
    }
101
102
    protected function siteChoices(): array
103
    {
104
        return $this->syncableSites->map(function (Site $site) {
105
            return "<comment>{$site->url()}</comment>";
106
        })->merge(['<comment>All Sites</comment>'])->toArray();
107
    }
108
}
109