Completed
Push — master ( 8bc3d2...029cd5 )
by Freek
9s
created

SyncSitesCommand::handle()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 13
nc 3
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 $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'));
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...
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());
0 ignored issues
show
Documentation introduced by
$site->url() is of type string, but the function expects a object<OhDear\ForgeSync\Site>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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