Passed
Push — master ( 9aa048...898b5e )
by Mattia
04:05
created

CheckUuid   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 47
c 2
b 0
f 0
dl 0
loc 85
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 64 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Minepic\Console\Commands;
6
7
use Carbon\Carbon;
8
use Illuminate\Console\Command;
9
use Minepic\Helpers\Storage\Files\SkinsStorage;
10
use Minepic\Minecraft\MojangClient;
11
use Minepic\Models\Account;
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 \Minepic\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->getUsername(),
68
                        'skin' => $accountApiData->getSkin(),
69
                        'cape' => $accountApiData->getCape(),
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