Passed
Push — master ( 10a915...0d23d6 )
by Mattia
08:28 queued 11s
created

CheckUuid::fire()   B

Complexity

Conditions 8
Paths 69

Size

Total Lines 57
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 43
c 2
b 0
f 0
nc 69
nop 0
dl 0
loc 57
rs 7.9875

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Console\Commands;
6
7
use App\Helpers\Storage\Files\SkinsStorage;
8
use App\Minecraft\MojangClient;
9
use App\Models\Account;
10
use Carbon\Carbon;
11
use Illuminate\Console\Command;
12
13
/**
14
 * Class CleanAccountsTable.
15
 */
16
class CheckUuid extends Command
17
{
18
    /**
19
     * The console command name.
20
     *
21
     * @var string
22
     */
23
    protected $name = 'minepic:check-uuid';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Check old uuid.';
31
32
    /**
33
     * Execute the console command.
34
     *
35
     * @throws \Exception
36
     */
37
    public function handle(MojangClient $mojangClient): int
38
    {
39
        $this->info('Selecting old uuid...');
40
41
        $timeCheck = Carbon::now()->subDays(28);
42
43
        $results = Account::query()
44
            ->select(['id'])
45
            ->whereDate('updated_at', '<', $timeCheck->toDateTimeString())
46
            ->orderBy('updated_at', 'ASC')
47
            ->take(300)
48
            ->get();
49
50
        if ($results->count() === 0) {
51
            $this->info('No old uuid found');
52
53
            return 0;
54
        }
55
56
        foreach ($results as $result) {
57
            /** @var \App\Models\Account $account */
58
            $account = Account::find($result->id);
59
            if ($account) {
60
                $this->info("Checking {$account->username} [{$account->uuid}]...");
61
                try {
62
                    $accountApiData = $mojangClient->getUuidInfo($account->uuid);
63
                    $this->info("\tUUID Valid");
64
65
                    // Update database
66
                    $account->update([
67
                        'username' => $accountApiData->username,
68
                        'skin' => $accountApiData->skin,
69
                        'cape' => $accountApiData->cape,
70
                        'fail_count' => 0,
71
                    ]);
72
                    $this->info("\tData updated");
73
74
                    try {
75
                        $skinData = $mojangClient->getSkin($account->uuid);
76
                        SkinsStorage::save($account->uuid, $skinData);
77
                        $this->info("\tSkin png updated");
78
                    } catch (\Exception $e) {
79
                        SkinsStorage::copyAsSteve($account->uuid);
80
                        $this->error("\tUsing Steve as skin");
81
                        $this->error("\t".$e->getMessage());
82
                    }
83
                } catch (\Exception $e) {
84
                    ++$account->fail_count;
85
                    $account->update([
86
                        'fail_count' => $account->fail_count,
87
                    ]);
88
                    $this->warn("\tFailed. Fail count: {$account->fail_count}");
89
                    if ($account->fail_count > 10) {
90
                        $account->delete();
91
                        $this->error("\tDELETED!");
92
                    } else {
93
                        $account->save();
94
                    }
95
                }
96
                $this->line('################################################');
97
            }
98
        }
99
100
        return 0;
101
    }
102
}
103