diff --git a/app/Console/Commands/CheckUuid.php b/app/Console/Commands/CheckUuid.php index c715045..34bfa06 100644 --- a/app/Console/Commands/CheckUuid.php +++ b/app/Console/Commands/CheckUuid.php @@ -10,9 +10,6 @@ use Minepic\Minecraft\MojangClient; use Minepic\Models\Account; -/** - * Class CleanAccountsTable. - */ class CheckUuid extends Command { /** @@ -32,26 +29,17 @@ class CheckUuid extends Command /** * Execute the console command. * - * @throws \Exception + * @param MojangClient $mojangClient + * + * @throws \Throwable + * + * @return int */ public function handle(MojangClient $mojangClient): int { $this->info('Selecting old uuid...'); - $timeCheck = Carbon::now()->subDays(28); - - $results = Account::query() - ->select(['id']) - ->whereDate('updated_at', '<', $timeCheck->toDateTimeString()) - ->orderBy('updated_at', 'ASC') - ->take(300) - ->get(); - - if ($results->count() === 0) { - $this->info('No old uuid found'); - - return 0; - } + $results = $this->getAccountsIds(); foreach ($results as $result) { /** @var \Minepic\Models\Account $account */ @@ -99,4 +87,14 @@ public function handle(MojangClient $mojangClient): int return 0; } + + private function getAccountsIds() + { + return Account::query() + ->select(['id']) + ->whereDate('updated_at', '<', Carbon::now()->subDays(28)->toDateTimeString()) + ->orderBy('updated_at', 'ASC') + ->take(300) + ->get(); + } } diff --git a/app/Console/Commands/CleanAccountsTable.php b/app/Console/Commands/CleanAccountsTable.php index e41da88..a6eea66 100644 --- a/app/Console/Commands/CleanAccountsTable.php +++ b/app/Console/Commands/CleanAccountsTable.php @@ -4,12 +4,9 @@ namespace Minepic\Console\Commands; -use Minepic\Models\Account; use Illuminate\Console\Command; +use Minepic\Models\Account; -/** - * Class CleanAccountsTable. - */ class CleanAccountsTable extends Command { /** diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index d52cd6c..c542b61 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -30,7 +30,9 @@ class Handler extends ExceptionHandler * Report or log an exception. * * This is a great spot to send exceptions to Sentry, Bugsnag, etc. + * * @param \Throwable $e + * * @throws \Exception */ public function report(\Throwable $e) @@ -42,7 +44,7 @@ public function report(\Throwable $e) * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request - * @param \Throwable $e + * @param \Throwable $e * * @throws \Throwable * diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php index 97185b2..0c30e2e 100644 --- a/app/Http/Controllers/JsonController.php +++ b/app/Http/Controllers/JsonController.php @@ -159,13 +159,13 @@ public function updateUser(string $uuid): JsonResponse /** * Username Typeahead. * - * @param $term + * @param $username * * @return JsonResponse */ - public function userTypeahead($term): JsonResponse + public function userTypeahead(string $username): JsonResponse { - $accountsPagination = $this->accountRepository->filterPaginate(['term' => $term], 15); + $accountsPagination = $this->accountRepository->filterPaginate(['term' => $username], 15); $resource = new Fractal\Resource\Collection( $accountsPagination->items(), diff --git a/app/Http/Controllers/WebsiteController.php b/app/Http/Controllers/WebsiteController.php index 83b5060..e2cb74c 100644 --- a/app/Http/Controllers/WebsiteController.php +++ b/app/Http/Controllers/WebsiteController.php @@ -4,17 +4,17 @@ namespace Minepic\Http\Controllers; -use Minepic\Misc\SplashMessage; -use Minepic\Models\AccountStats; -use Minepic\Resolvers\UsernameResolver; -use Minepic\Resolvers\UuidResolver; -use Minepic\Transformers\Account\AccountBasicDataTransformer; use Illuminate\Http\Response; use Laravel\Lumen\Http\ResponseFactory; use Laravel\Lumen\Routing\Controller as BaseController; use League\Fractal; use League\Fractal\Manager; use League\Fractal\Serializer\ArraySerializer; +use Minepic\Misc\SplashMessage; +use Minepic\Models\AccountStats; +use Minepic\Resolvers\UsernameResolver; +use Minepic\Resolvers\UuidResolver; +use Minepic\Transformers\Account\AccountBasicDataTransformer; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** diff --git a/app/Image/Sections/Avatar.php b/app/Image/Sections/Avatar.php index c9db842..99ead14 100644 --- a/app/Image/Sections/Avatar.php +++ b/app/Image/Sections/Avatar.php @@ -109,12 +109,17 @@ private function hasHelm($baseSkinImage, Point $helmCoordinates): bool $helmCheckImage = $this->createHelmCheckImage($baseSkinImage, $helmCoordinates); $this->calculateHelmStandardDeviation($helmCheckImage); - return ( - ($this->redStdDev > self::DEFAULT_STANDARD_DEVIATION && $this->greenStdDev > self::DEFAULT_STANDARD_DEVIATION) || - ($this->redStdDev > self::DEFAULT_STANDARD_DEVIATION && $this->blueStdDev > self::DEFAULT_STANDARD_DEVIATION) || - ($this->greenStdDev > self::DEFAULT_STANDARD_DEVIATION && $this->blueStdDev > self::DEFAULT_STANDARD_DEVIATION) - ) || - $this->meanAlpha === 127; + return $this->isValidHelmStandardDeviation() || $this->meanAlpha === 127; + } + + /** + * @return bool + */ + private function isValidHelmStandardDeviation(): bool + { + return ($this->redStdDev > self::DEFAULT_STANDARD_DEVIATION && $this->greenStdDev > self::DEFAULT_STANDARD_DEVIATION) || + ($this->redStdDev > self::DEFAULT_STANDARD_DEVIATION && $this->blueStdDev > self::DEFAULT_STANDARD_DEVIATION) || + ($this->greenStdDev > self::DEFAULT_STANDARD_DEVIATION && $this->blueStdDev > self::DEFAULT_STANDARD_DEVIATION); } /** @@ -123,12 +128,12 @@ private function hasHelm($baseSkinImage, Point $helmCoordinates): bool * @param int $size Avatar size * @param string $type Section rendered * - * @throws \Minepic\Image\Exceptions\InvalidSectionSpecifiedException|\Minepic\Image\Exceptions\ImageTrueColorCreationFailedException * @throws \Minepic\Image\Exceptions\ImageCreateFromPngFailedException + * @throws \Minepic\Image\Exceptions\ImageTrueColorCreationFailedException */ public function render(int $size = 0, string $type = self::FRONT): void { - if ($size <= 0 || $size > env('MAX_AVATAR_SIZE')) { + if ($size <= 0 || $size > (int) env('MAX_AVATAR_SIZE')) { $size = (int) env('DEFAULT_AVATAR_SIZE'); } // generate png from url/path diff --git a/app/Minecraft/MojangAccountFactory.php b/app/Minecraft/MojangAccountFactory.php index 30e692e..1b9effe 100644 --- a/app/Minecraft/MojangAccountFactory.php +++ b/app/Minecraft/MojangAccountFactory.php @@ -28,14 +28,10 @@ private static function extractTextureIdFromUrl(string $url): string * * @throws \JsonException * - * @return MojangAccount|null + * @return MojangAccount */ - public static function makeFromApiResponse(array $response): ?MojangAccount + public static function makeFromApiResponse(array $response): MojangAccount { - if (!\array_key_exists('id', $response) || !\array_key_exists('name', $response)) { - return null; - } - $uuid = $response['id']; $username = $response['name']; $skin = ''; diff --git a/app/Minecraft/MojangClient.php b/app/Minecraft/MojangClient.php index 690cef1..24630af 100644 --- a/app/Minecraft/MojangClient.php +++ b/app/Minecraft/MojangClient.php @@ -4,10 +4,10 @@ namespace Minepic\Minecraft; -use Minepic\Minecraft\Exceptions\UserNotFoundException; use GuzzleHttp\Client as HttpClient; use GuzzleHttp\Exception\BadResponseException; use Illuminate\Support\Facades\Log; +use Minepic\Minecraft\Exceptions\UserNotFoundException; use Psr\Http\Message\ResponseInterface; /** diff --git a/app/Repositories/AccountRepository.php b/app/Repositories/AccountRepository.php index c2dff21..37fd954 100644 --- a/app/Repositories/AccountRepository.php +++ b/app/Repositories/AccountRepository.php @@ -4,9 +4,9 @@ namespace Minepic\Repositories; -use Minepic\Models\Account; use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Database\Eloquent\Builder; +use Minepic\Models\Account; class AccountRepository { diff --git a/app/Resolvers/UuidResolver.php b/app/Resolvers/UuidResolver.php index 8580994..2bc7743 100644 --- a/app/Resolvers/UuidResolver.php +++ b/app/Resolvers/UuidResolver.php @@ -126,7 +126,7 @@ private function requestedUuidInDb(): bool */ public function insertNewUuid(): bool { - if (UserNotFoundCache::has($this->request)) { + if ($this->request === null || UserNotFoundCache::has($this->request)) { return false; } diff --git a/composer.json b/composer.json index c652edb..cd7c43a 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "ext-intl": "*", "ext-json": "*", "ext-mbstring": "*", - "guzzlehttp/guzzle": "^6.2", + "guzzlehttp/guzzle": "^6.5", "illuminate/redis": "^7.0", "laravel/lumen-framework": "^7.0", "league/flysystem": "^1.0", diff --git a/composer.lock b/composer.lock index b2c02bd..a479e0a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ced989254efde7ebfbdddad11db20909", + "content-hash": "a77bdcbbdc50f24456d47ac14c33f08d", "packages": [ { "name": "brick/math", diff --git a/tests/Http/Controllers/JsonControllerTest.php b/tests/Http/Controllers/JsonControllerTest.php index 5cc2c10..2f4b7ad 100644 --- a/tests/Http/Controllers/JsonControllerTest.php +++ b/tests/Http/Controllers/JsonControllerTest.php @@ -36,8 +36,10 @@ public function shouldReturnUserDataUsingUuid(): void { $this->get('/api/v1/user/d59dcabb30424b978f7201d1a076637f'); $responseContent = $this->response->content(); + $decodedData = \json_decode($responseContent, true); $this->assertJson($responseContent); - $this->seeJsonStructure(['ok', 'data'], $responseContent); + $this->assertArrayHasKey('ok', $decodedData); + $this->assertArrayHasKey('data', $decodedData); } /** @@ -47,9 +49,11 @@ public function shouldNotReturnUserDataUsingInvalidUuid(): void { $this->get('/api/v1/user/d59dcabb30424b978f7201ffffffffff'); $responseContent = $this->response->content(); + $decodedData = \json_decode($responseContent, true); $this->assertResponseStatus(404); $this->assertJson($responseContent); - $this->seeJsonStructure(['ok', 'message'], $responseContent); + $this->assertArrayHasKey('ok', $decodedData); + $this->assertArrayHasKey('message', $decodedData); } /** @@ -59,8 +63,10 @@ public function shouldReturnUserDataUsingUsername(): void { $this->get('/api/v1/user/_Cyb3r'); $responseContent = $this->response->content(); + $decodedData = \json_decode($responseContent, true); $this->assertJson($responseContent); - $this->seeJsonStructure(['ok', 'data'], $responseContent); + $this->assertArrayHasKey('ok', $decodedData); + $this->assertArrayHasKey('data', $decodedData); } /** @@ -70,7 +76,9 @@ public function shouldReturnMostWantedUser(): void { $this->get('/api/v1/stats/user/most-wanted'); $responseContent = $this->response->content(); + $decodedData = \json_decode($responseContent, true); $this->assertJson($responseContent); - $this->seeJsonStructure(['ok', 'data'], $responseContent); + $this->assertArrayHasKey('ok', $decodedData); + $this->assertArrayHasKey('data', $decodedData); } }