Passed
Push — master ( 165425...e22481 )
by Mattia
04:28
created

CheckUuid::getAccountsIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
class CheckUuid extends Command
14
{
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'minepic:check-uuid';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Check old uuid.';
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @param MojangClient $mojangClient
33
     *
34
     * @throws \Throwable
35
     *
36
     * @return int
37
     */
38
    public function handle(MojangClient $mojangClient): int
39
    {
40
        $this->info('Selecting old uuid...');
41
42
        $results = $this->getAccountsIds();
43
44
        foreach ($results as $result) {
45
            /** @var \Minepic\Models\Account $account */
46
            $account = Account::find($result->id);
47
            if ($account) {
48
                $this->info("Checking {$account->username} [{$account->uuid}]...");
49
                try {
50
                    $accountApiData = $mojangClient->getUuidInfo($account->uuid);
51
                    $this->info("\tUUID Valid");
52
53
                    // Update database
54
                    $account->update([
55
                        'username' => $accountApiData->getUsername(),
56
                        'skin' => $accountApiData->getSkin(),
57
                        'cape' => $accountApiData->getCape(),
58
                        'fail_count' => 0,
59
                    ]);
60
                    $this->info("\tData updated");
61
62
                    try {
63
                        $skinData = $mojangClient->getSkin($account->uuid);
64
                        SkinsStorage::save($account->uuid, $skinData);
65
                        $this->info("\tSkin png updated");
66
                    } catch (\Exception $e) {
67
                        SkinsStorage::copyAsSteve($account->uuid);
68
                        $this->error("\tUsing Steve as skin");
69
                        $this->error("\t".$e->getMessage());
70
                    }
71
                } catch (\Exception $e) {
72
                    ++$account->fail_count;
73
                    $account->update([
74
                        'fail_count' => $account->fail_count,
75
                    ]);
76
                    $this->warn("\tFailed. Fail count: {$account->fail_count}");
77
                    if ($account->fail_count > 10) {
78
                        $account->delete();
79
                        $this->error("\tDELETED!");
80
                    } else {
81
                        $account->save();
82
                    }
83
                }
84
                $this->line('################################################');
85
            }
86
        }
87
88
        return 0;
89
    }
90
91
    private function getAccountsIds()
92
    {
93
        return Account::query()
94
            ->select(['id'])
95
            ->whereDate('updated_at', '<', Carbon::now()->subDays(28)->toDateTimeString())
96
            ->orderBy('updated_at', 'ASC')
97
            ->take(300)
98
            ->get();
99
    }
100
}
101