Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce rector to perform better laravel upgrades #657

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions app/Actions/Photos/DeleteTagsFromPhotoAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
namespace App\Actions\Photos;

use App\Models\Photo;
use Illuminate\Support\Collection;

class DeleteTagsFromPhotoAction
{
/**
* Clear all tags on an image
*
* Returns the total number of tags that were deleted, separated from brands
*
* @param Photo $photo
*
* @return array
*/
public function run(Photo $photo): array
{
Expand All @@ -33,14 +28,14 @@ private function deleteLitter(Photo $photo): int
{
$categories = collect($photo->categories())
->filter(function ($category) use ($photo) {
return $category !== 'brands' && !!$photo->$category;
return $category !== 'brands' && (bool) $photo->$category;
});

$total = $categories->sum(function ($category) use ($photo) {
return $photo->$category->total();
});

$categories->each(function ($category) use ($photo) {
$categories->each(function ($category) use ($photo): void {
$photo->$category->delete();
});

Expand All @@ -49,7 +44,7 @@ private function deleteLitter(Photo $photo): int

private function deleteBrands(Photo $photo): int
{
if (!$photo->brands) {
if (! $photo->brands) {
return 0;
}

Expand Down
23 changes: 9 additions & 14 deletions app/Actions/Photos/MakeImageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@ class MakeImageAction
/**
* Create an instance of Intervention Image using an UploadedFile
*
* @param UploadedFile $file
* @param bool $resize
*
* @return array
* @throws Exception
*/
public function run (UploadedFile $file, bool $resize = false): array
public function run(UploadedFile $file, bool $resize = false): array
{
$imageAndExifData = $this->getImageAndExifData($file);

if ($resize) {
$imageAndExifData['image']->resize(500, 500);

$imageAndExifData['image']->resize(500, 500, function ($constraint) {
$imageAndExifData['image']->resize(500, 500, function ($constraint): void {
$constraint->aspectRatio();
});
}
Expand All @@ -36,18 +33,16 @@ public function run (UploadedFile $file, bool $resize = false): array
}

/**
* @param UploadedFile $file
* @return array
* @throws Exception
*/
protected function getImageAndExifData (UploadedFile $file): array
protected function getImageAndExifData(UploadedFile $file): array
{
$extension = $file->getClientOriginalExtension();

// If the image is not type HEIC, HEIF
// We can assume its jpg, png, and can be handled by the default GD image library
// Otherwise, we are going to have to handle HEIC separately.
if (!in_array(strtolower($extension), ['heif', 'heic'])) {
if (! in_array(strtolower($extension), ['heif', 'heic'])) {
$image = Image::make($file)->orientate();
$exif = $image->exif();

Expand All @@ -61,21 +56,21 @@ protected function getImageAndExifData (UploadedFile $file): array

// Path for a temporary file from the upload -> storage/app/heic_images/sample1.heic
$tmpFilepath = storage_path(
self::TEMP_HEIC_STORAGE_DIR .
$randomFilename . ".$extension"
self::TEMP_HEIC_STORAGE_DIR.
$randomFilename.".$extension"
);

// Path for a converted temporary file -> storage/app/heic_images/sample1.jpg
$convertedFilepath = storage_path(
self::TEMP_HEIC_STORAGE_DIR .
$randomFilename . '.jpg'
self::TEMP_HEIC_STORAGE_DIR.
$randomFilename.'.jpg'
);

// Store the uploaded HEIC file on the server
File::put($tmpFilepath, $file->getContent());

// Run a shell command to execute ImageMagick conversion
exec('magick convert ' . $tmpFilepath . ' ' . $convertedFilepath);
exec('magick convert '.$tmpFilepath.' '.$convertedFilepath);

// Make the image from the new converted file
$image = Image::make($convertedFilepath)->orientate();
Expand Down
32 changes: 14 additions & 18 deletions app/Console/Commands/Clusters/GenerateClusters.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace App\Console\Commands\Clusters;

use App\Models\Photo;
use App\Models\Cluster;

use App\Models\Photo;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
Expand Down Expand Up @@ -35,14 +34,14 @@ public function handle()
$start = microtime(true);

foreach ($this->getYearsWithNewPhotos() as $year) {
$this->line("\nYear: " . ($year ?: 'All Time'));
$this->line("\nYear: ".($year ?: 'All Time'));
$this->generateFeatures($year);
$this->generateClusters($year);
}

$finish = microtime(true);
$this->newLine();
$this->info("Total Time: " . ($finish - $start) . "\n");
$this->info('Total Time: '.($finish - $start)."\n");

\Log::info('--- Clustering finished ---');
}
Expand All @@ -56,7 +55,7 @@ public function handle()
*
* We save this file to storage and use it to populate the clusters with a node script in the backend
*/
protected function generateFeatures (int $year = null): void
protected function generateFeatures(?int $year = null): void
{
$this->info('Generating features...');

Expand All @@ -74,14 +73,13 @@ protected function generateFeatures (int $year = null): void

$features = [];

foreach ($photos->cursor() as $photo)
{
foreach ($photos->cursor() as $photo) {
$feature = [
'type' => 'Feature',
'geometry' => [
'type' => 'Point',
'coordinates' => [$photo->lon, $photo->lat]
]
'coordinates' => [$photo->lon, $photo->lat],
],
];

$features[] = $feature;
Expand All @@ -102,11 +100,10 @@ protected function generateFeatures (int $year = null): void
* Using the features.json file, we cluster our data at various zoom levels.
*
* First, we need to delete all clusters. Then we re-create them from scratch.
*
*/
protected function generateClusters (int $year = null): void
protected function generateClusters(?int $year = null): void
{
$this->info("Generating clusters for each zoom level...");
$this->info('Generating clusters for each zoom level...');

// Delete all clusters for year
if ($year) {
Expand All @@ -123,11 +120,10 @@ protected function generateClusters (int $year = null): void
$zoomLevels = range(2, 16);

// For each zoom level, create clusters.
foreach ($zoomLevels as $zoomLevel)
{
foreach ($zoomLevels as $zoomLevel) {
// Supercluster is awesome open-source javascript code from MapBox that we made executable on the backend with php
// This file uses features.json to create clusters.json for a specific zoom level.
exec('node app/Node/supercluster-php ' . base_path() . ' ' . $zoomLevel);
exec('node app/Node/supercluster-php '.base_path().' '.$zoomLevel);

// We then use the clusters.json and save it to the clusters table
collect(json_decode(Storage::get('/data/clusters.json')))
Expand All @@ -142,11 +138,11 @@ protected function generateClusters (int $year = null): void
'point_count_abbreviated' => $cluster->properties->point_count_abbreviated,
'geohash' => \GeoHash::encode($cluster->geometry->coordinates[1], $cluster->geometry->coordinates[0]),
'zoom' => $zoomLevel,
'year' => $year
'year' => $year,
];
})
->chunk(1000)
->each(function ($chunk) {
->each(function ($chunk): void {
Cluster::insert($chunk->toArray());
});

Expand All @@ -171,7 +167,7 @@ private function getYearsWithNewPhotos(): array
foreach ($years as $year) {
$hasRecentPhotosForYear = Photo::query()->where([
['created_at', '>=', now()->subDay()->startOfDay()],
[DB::raw('year(datetime)'), '=', $year]
[DB::raw('year(datetime)'), '=', $year],
])->exists();

if ($hasRecentPhotosForYear) {
Expand Down
18 changes: 8 additions & 10 deletions app/Console/Commands/Clusters/GenerateTeamClusters.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class GenerateTeamClusters extends Command
protected $description = 'Generate all clusters for teams photos';

private $clustersDir = 'team-clusters.json';

private $featuresDir = 'team-features.json';

/**
Expand All @@ -48,7 +49,7 @@ public function handle()

$finish = microtime(true);
$this->newLine();
$this->info("Total Time: " . ($finish - $start) . "\n");
$this->info('Total Time: '.($finish - $start)."\n");

return 0;
}
Expand All @@ -58,7 +59,7 @@ public function handle()
*/
protected function generateFeatures(Team $team): void
{
$this->info("Generating features...");
$this->info('Generating features...');

$bar = $this->output->createProgressBar(
Photo::whereTeamId($team->id)->count()
Expand All @@ -78,8 +79,8 @@ protected function generateFeatures(Team $team): void
'type' => 'Feature',
'geometry' => [
'type' => 'Point',
'coordinates' => [$photo->lon, $photo->lat]
]
'coordinates' => [$photo->lon, $photo->lat],
],
];

$features[] = $feature;
Expand All @@ -100,7 +101,7 @@ protected function generateFeatures(Team $team): void
*/
protected function generateClusters(Team $team): void
{
$this->info("Generating clusters for each zoom level...");
$this->info('Generating clusters for each zoom level...');

$rootDir = base_path();
$zoomLevels = range(2, 16);
Expand Down Expand Up @@ -132,7 +133,7 @@ protected function generateClusters(Team $team): void
];
})
->chunk(1000)
->each(function ($chunk) {
->each(function ($chunk): void {
TeamCluster::insert($chunk->all());
});

Expand All @@ -144,12 +145,9 @@ protected function generateClusters(Team $team): void
$this->info("\nClusters finished...");
}

/**
* @param Team $team
*/
protected function deleteClusters(Team $team)
{
$this->info("Deleting clusters...");
$this->info('Deleting clusters...');

TeamCluster::whereTeamId($team->id)->delete();
}
Expand Down
10 changes: 4 additions & 6 deletions app/Console/Commands/Photos/Resize500x500.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ public function handle()
Photo::where([
['verified', '>=', 2],
['filename', '!=', '/assets/verified.jpg'],
'five_hundred_square_filepath' => null
])->chunk(500, function ($photos)
{
foreach ($photos as $photo)
{
'five_hundred_square_filepath' => null,
])->chunk(500, function ($photos): void {
foreach ($photos as $photo) {
echo "Photo id $photo->id \n";

// Create an image object
Expand All @@ -68,7 +66,7 @@ public function handle()
$x = explode('/', $photo->filename);

// Get the last element which is the filename with extension
$filename = $x[sizeof($x) -1];
$filename = $x[count($x) - 1];
$filepath = $year.'/'.$month.'/'.$day.'/'.$filename;

$s3 = \Storage::disk('bbox');
Expand Down
22 changes: 10 additions & 12 deletions app/Console/Commands/Twitter/DailyReportTweet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace App\Console\Commands\Twitter;

use App\Models\CustomTag;
use Carbon\Carbon;
use App\Models\Photo;
use Spatie\Emoji\Emoji;
use App\Helpers\Twitter;
use App\Models\User\User;
use App\Models\CustomTag;
use App\Models\Littercoin;
use App\Models\Location\Country;
use App\Models\Photo;
use App\Models\User\User;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Spatie\Emoji\Emoji;

class DailyReportTweet extends Command
{
protected $signature = 'twitter:daily-report';

protected $description = 'Send a daily report about OLM to Twitter OLM_bot account';

public function handle ()
public function handle()
{
$startOfYesterday = Carbon::yesterday()->startOfDay();
$endOfYesterday = Carbon::yesterday()->endOfDay();
Expand Down Expand Up @@ -58,18 +58,16 @@ public function handle ()
$photos = Photo::select('id', 'created_at', 'country_id', 'total_litter')
->whereDate('created_at', '>=', $startOfYesterday)
->whereDate('created_at', '<=', $endOfYesterday)
->orWhereHas('customTags', function ($query) use ($startOfYesterday, $endOfYesterday) {
->orWhereHas('customTags', function ($query) use ($startOfYesterday, $endOfYesterday): void {
$query->whereDate('created_at', '>=', $startOfYesterday)
->whereDate('created_at', '<=', $endOfYesterday);
})
->get();

$countryIds = [];

foreach ($photos as $photo)
{
if (!array_key_exists($photo->country_id, $countryIds))
{
foreach ($photos as $photo) {
if (! array_key_exists($photo->country_id, $countryIds)) {
$countryIds[$photo->country_id] = 0;
}

Expand Down Expand Up @@ -131,7 +129,7 @@ public function handle ()
}
}

$message .= " #openlittermap #OLMbot 🌍";
$message .= ' #openlittermap #OLMbot 🌍';

Twitter::sendTweet($message);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Console/Commands/Users/UpdateRedisBoundingBoxXp.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public function __construct(UpdateLeaderboardsXpAction $xpAction)
*/
public function handle()
{
$this->line("Updating XP from bounding boxes");
$this->line('Updating XP from bounding boxes');

$this->withProgressBar(User::all(), function (User $user) {
$this->withProgressBar(User::all(), function (User $user): void {
$addedBoxes = $user->boxes()->count();
$verifiedBoxes = $user->boxesVerified()->count();

Expand Down
Loading