Skip to content

Commit

Permalink
cleanup/fix after upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiabasone committed Sep 9, 2020
1 parent 1654256 commit 518f3e4
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 54 deletions.
34 changes: 16 additions & 18 deletions app/Console/Commands/CheckUuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
use Minepic\Minecraft\MojangClient;
use Minepic\Models\Account;

/**
* Class CleanAccountsTable.
*/
class CheckUuid extends Command
{
/**
Expand All @@ -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 */
Expand Down Expand Up @@ -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();
}
}
5 changes: 1 addition & 4 deletions app/Console/Commands/CleanAccountsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
4 changes: 3 additions & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
*
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
10 changes: 5 additions & 5 deletions app/Http/Controllers/WebsiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
21 changes: 13 additions & 8 deletions app/Image/Sections/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions app/Minecraft/MojangAccountFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
2 changes: 1 addition & 1 deletion app/Minecraft/MojangClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Repositories/AccountRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion app/Resolvers/UuidResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions tests/Http/Controllers/JsonControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
}

0 comments on commit 518f3e4

Please sign in to comment.