2020-06-27 00:32:33 +01:00
< ? php
2020-10-10 16:30:26 +00:00
/*
* Copyright © 2020 Miguel Nogueira
*
* This file is part of Raspberry Staff Manager .
*
* Raspberry Staff Manager is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* Raspberry Staff Manager is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with Raspberry Staff Manager . If not , see < https :// www . gnu . org / licenses />.
*/
2020-06-27 00:32:33 +01:00
namespace App\UUID ;
use Illuminate\Support\Facades\Cache ;
2020-10-10 16:30:26 +00:00
use Illuminate\Support\Facades\Http ;
2020-06-27 00:32:33 +01:00
use Illuminate\Support\Facades\Log ;
class UUID
{
// Caching would not be needed here since this method won't be used in pages that loop over a collection of usernames.
2021-10-20 03:43:58 +01:00
public function toUUID ( string $username )
2020-06-27 00:32:33 +01:00
{
2020-10-10 16:30:26 +00:00
$response = json_decode ( Http :: post ( trim ( config ( 'general.urls.mojang.api' )) . '/profiles/minecraft' , [
$username ,
2020-06-27 00:32:33 +01:00
]) -> body (), true );
2020-10-10 16:30:26 +00:00
if ( isset ( $response [ 0 ])) {
return $response [ 0 ][ 'id' ];
2020-06-28 04:51:32 +01:00
}
2020-10-10 16:30:26 +00:00
throw new \InvalidArgumentException ( 'You must supply a valid, premium Minecraft account to sign up.' );
2020-06-27 00:32:33 +01:00
}
// Note: Caching could simply be assigning the username to it's UUID, however, to make this work, we'd need to loop over all cache items, which would be slighly ineffective
2021-10-20 03:43:58 +01:00
public function toUsername ( string $uuid )
2020-06-27 00:32:33 +01:00
{
2020-10-10 16:30:26 +00:00
$shortUUID = substr ( $uuid , 0 , 8 );
$username = Cache :: remember ( 'uuid_' . $shortUUID , now () -> addDays ( 30 ), function () use ( $shortUUID , $uuid ) {
$response = json_decode ( Http :: get ( trim ( config ( 'general.urls.mojang.session' )) . '/session/minecraft/profile/' . $uuid ) -> body (), true );
2020-06-27 00:32:33 +01:00
2020-10-10 16:30:26 +00:00
Log :: debug ( 'Caching ' . $shortUUID . 'for thirty days' );
2020-06-27 00:32:33 +01:00
return $response [ 'name' ];
2020-10-10 16:30:26 +00:00
});
2020-06-27 00:32:33 +01:00
2020-10-10 16:30:26 +00:00
return $username ;
2020-06-27 00:32:33 +01:00
}
}