Skip to content

Commit

Permalink
refactor: use scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Feb 25, 2024
1 parent 972989d commit b719f7e
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 39 deletions.
6 changes: 1 addition & 5 deletions app/Console/Commands/FetchMetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ class FetchMetaData extends Command
*/
public function handle(): void
{
foreach (Name::orderBy('total', 'desc')->lazy() as $name) {
if ($name->name === '_PRENOMS_RARES') {
continue;
}

foreach (Name::nonRares()->orderBy('total', 'desc')->lazy() as $name) {
if (is_null($name->origins)) {
ProcessOrigins::dispatch($name);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/GenerateSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private function sitemap_root(SitemapIndex $sitemapIndex): void
*/
private function sitemap_names(SitemapIndex $sitemapIndex): void
{
Name::where('name', '!=', '_PRENOMS_RARES')
Name::nonRares()
->chunkById(2000, function (Collection $names, int $key) use ($sitemapIndex) {

$file = $this->file('sitemap_' . Str::padLeft("$key", 2, '0') . '.xml');
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/FemaleNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function index(Request $request): View

$letters = Cache::remember('all-letters-female', 604800, fn () => FemaleNamesViewModel::index());

$namesPagination = Cache::remember('all-names-female-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->where('gender', 'female')
$namesPagination = Cache::remember('all-names-female-page-' . $requestedPage, 604800, fn () => Name::nonRares()

Check failure on line 24 in app/Http/Controllers/FemaleNameController.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHPStan

Call to an undefined method Illuminate\Database\Eloquent\Builder<App\Models\Name>::female().
->female()
->orderBy('total', 'desc')
->paginate(40)
);
Expand All @@ -49,8 +49,8 @@ public function letter(Request $request): View

$letters = Cache::remember('all-letters-female', 604800, fn () => FemaleNamesViewModel::index());

$namesPagination = Cache::remember('female-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->where('gender', 'female')
$namesPagination = Cache::remember('female-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::nonRares()

Check failure on line 52 in app/Http/Controllers/FemaleNameController.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHPStan

Call to an undefined method Illuminate\Database\Eloquent\Builder<App\Models\Name>::female().
->female()
->where('name', 'like', Str::upper($requestedLetter) . '%')
->orderBy('total', 'desc')
->paginate(40)
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/MaleNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function index(Request $request): View

$letters = Cache::remember('all-letters-male', 604800, fn () => MaleNamesViewModel::index());

$namesPagination = Cache::remember('all-names-male-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->where('gender', 'male')
$namesPagination = Cache::remember('all-names-male-page-' . $requestedPage, 604800, fn () => Name::nonRares()

Check failure on line 24 in app/Http/Controllers/MaleNameController.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHPStan

Call to an undefined method Illuminate\Database\Eloquent\Builder<App\Models\Name>::male().
->male()
->orderBy('total', 'desc')
->paginate(40)
);
Expand All @@ -49,8 +49,8 @@ public function letter(Request $request): View

$letters = Cache::remember('all-letters-male', 604800, fn () => MaleNamesViewModel::index());

$namesPagination = Cache::remember('male-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->where('gender', 'male')
$namesPagination = Cache::remember('male-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::nonRares()

Check failure on line 52 in app/Http/Controllers/MaleNameController.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHPStan

Call to an undefined method Illuminate\Database\Eloquent\Builder<App\Models\Name>::male().
->male()
->where('name', 'like', Str::upper($requestedLetter) . '%')
->orderBy('total', 'desc')
->paginate(40)
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/MixteNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function index(Request $request): View

$letters = Cache::remember('all-letters-mixte', 604800, fn () => MixteNamesViewModel::index());

$namesPagination = Cache::remember('all-names-mixte-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->where('unisex', 1)
$namesPagination = Cache::remember('all-names-mixte-page-' . $requestedPage, 604800, fn () => Name::nonRares()

Check failure on line 24 in app/Http/Controllers/MixteNameController.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHPStan

Call to an undefined method Illuminate\Database\Eloquent\Builder<App\Models\Name>::unisex().
->unisex()
->orderBy('total', 'desc')
->paginate(40)
);
Expand All @@ -49,8 +49,8 @@ public function letter(Request $request): View

$letters = Cache::remember('all-letters-mixte', 604800, fn () => MixteNamesViewModel::index());

$namesPagination = Cache::remember('mixte-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->where('unisex', 1)
$namesPagination = Cache::remember('mixte-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::nonRares()

Check failure on line 52 in app/Http/Controllers/MixteNameController.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHPStan

Call to an undefined method Illuminate\Database\Eloquent\Builder<App\Models\Name>::unisex().
->unisex()
->where('name', 'like', Str::upper($requestedLetter) . '%')
->orderBy('total', 'desc')
->paginate(40)
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/NameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function index(Request $request): View

$letters = Cache::remember('all-letters', 604800, fn () => AllNamesViewModel::index());

$namesPagination = Cache::remember('all-names-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
$namesPagination = Cache::remember('all-names-page-' . $requestedPage, 604800, fn () => Name::nonRares()
->orderBy('total', 'desc')
->paginate(40)
);
Expand Down Expand Up @@ -95,7 +95,7 @@ public function letter(Request $request): View

$letters = Cache::remember('all-letters', 604800, fn () => AllNamesViewModel::index());

$namesPagination = Cache::remember('letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
$namesPagination = Cache::remember('letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::nonRares()
->where('name', 'like', Str::upper($requestedLetter) . '%')
->orderBy('total', 'desc')
->paginate(40)
Expand Down
16 changes: 8 additions & 8 deletions app/Http/ViewModels/Home/HomeViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@ class HomeViewModel
{
public static function twentyMostPopularNames(): array
{
$maleNames = Name::where('gender', 'male')
->where('name', '!=', '_PRENOMS_RARES')
$maleNames = Name::male()

Check failure on line 18 in app/Http/ViewModels/Home/HomeViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHPStan

Cannot call method nonRares() on null.

Check failure on line 18 in app/Http/ViewModels/Home/HomeViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHPStan

Method App\Models\Name::male() invoked with 0 parameters, 1 required.

Check failure on line 18 in app/Http/ViewModels/Home/HomeViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHPStan

Result of method App\Models\Name::male() (void) is used.

Check failure on line 18 in app/Http/ViewModels/Home/HomeViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHPStan

Static call to instance method App\Models\Name::male().

Check failure on line 18 in app/Http/ViewModels/Home/HomeViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / Psalm

InvalidStaticInvocation

app/Http/ViewModels/Home/HomeViewModel.php:18:22: InvalidStaticInvocation: Method App\Models\Name::male is not static, but is called statically (see https://psalm.dev/014)

Check failure on line 18 in app/Http/ViewModels/Home/HomeViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / Psalm

TooFewArguments

app/Http/ViewModels/Home/HomeViewModel.php:18:22: TooFewArguments: Too few arguments for App\Models\Name::male - expecting query to be passed (see https://psalm.dev/025)
->nonRares()

Check failure on line 19 in app/Http/ViewModels/Home/HomeViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / Psalm

NullReference

app/Http/ViewModels/Home/HomeViewModel.php:19:15: NullReference: Cannot call method nonRares on null value (see https://psalm.dev/016)
->orderBy('total', 'desc')
->take(10)
->get()
->map(fn (Name $name) => NameViewModel::summary($name));

$femaleNames = Name::where('gender', 'female')
->where('name', '!=', '_PRENOMS_RARES')
$femaleNames = Name::female()
->nonRares()
->orderBy('total', 'desc')
->take(10)
->get()
->map(fn (Name $name) => NameViewModel::summary($name));

$mixtedNames = Name::where('unisex', 1)
->where('name', '!=', '_PRENOMS_RARES')
$mixtedNames = Name::unisex()
->nonRares()
->orderBy('total', 'desc')
->take(10)
->get()
->map(fn (Name $name) => NameViewModel::summary($name));

$randomIds = Name::select('id')
->where('name', '!=', '_PRENOMS_RARES')
->nonRares()
->inRandomOrder()
->take(10)
->get();
Expand Down Expand Up @@ -79,7 +79,7 @@ public static function nameSpotlight(): array

public static function serverStats(): array
{
$totalNames = Name::where('name', '!=', '_PRENOMS_RARES')->count();
$totalNames = Name::nonRares()->count();

return [
'total_names' => Number::format($totalNames),
Expand Down
2 changes: 1 addition & 1 deletion app/Http/ViewModels/Names/AllNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AllNamesViewModel
public static function index(): Collection
{
$letters = collect();
$allNames = Name::where('name', '!=', '_PRENOMS_RARES')->count();
$allNames = Name::nonRares()->count();
$letters->push([
'letter' => 'Tous',
'count' => Number::format($allNames),
Expand Down
6 changes: 3 additions & 3 deletions app/Http/ViewModels/Names/FemaleNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public static function index(): Collection
// iterate over the alphabet
$alphabet = range('A', 'Z');

$total = Name::where('gender', 'female')
->where('name', '!=', '_PRENOMS_RARES')->count();
$total = Name::female()

Check failure on line 17 in app/Http/ViewModels/Names/FemaleNamesViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / Psalm

InvalidStaticInvocation

app/Http/ViewModels/Names/FemaleNamesViewModel.php:17:18: InvalidStaticInvocation: Method App\Models\Name::female is not static, but is called statically (see https://psalm.dev/014)

Check failure on line 17 in app/Http/ViewModels/Names/FemaleNamesViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / Psalm

TooFewArguments

app/Http/ViewModels/Names/FemaleNamesViewModel.php:17:18: TooFewArguments: Too few arguments for App\Models\Name::female - expecting query to be passed (see https://psalm.dev/025)
->nonRares()->count();

Check failure on line 18 in app/Http/ViewModels/Names/FemaleNamesViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / Psalm

NullReference

app/Http/ViewModels/Names/FemaleNamesViewModel.php:18:15: NullReference: Cannot call method nonRares on null value (see https://psalm.dev/016)

$letters = collect();
$letters->push([
Expand All @@ -25,7 +25,7 @@ public static function index(): Collection
]);

foreach ($alphabet as $letter) {
$total = Name::where('gender', 'female')
$total = Name::female()
->where('name', 'like', $letter . '%')->count();
$letters->push([
'letter' => $letter,
Expand Down
6 changes: 3 additions & 3 deletions app/Http/ViewModels/Names/MaleNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public static function index(): Collection
// iterate over the alphabet
$alphabet = range('A', 'Z');

$total = Name::where('gender', 'male')
->where('name', '!=', '_PRENOMS_RARES')->count();
$total = Name::male()

Check failure on line 17 in app/Http/ViewModels/Names/MaleNamesViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / Psalm

InvalidStaticInvocation

app/Http/ViewModels/Names/MaleNamesViewModel.php:17:18: InvalidStaticInvocation: Method App\Models\Name::male is not static, but is called statically (see https://psalm.dev/014)

Check failure on line 17 in app/Http/ViewModels/Names/MaleNamesViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / Psalm

TooFewArguments

app/Http/ViewModels/Names/MaleNamesViewModel.php:17:18: TooFewArguments: Too few arguments for App\Models\Name::male - expecting query to be passed (see https://psalm.dev/025)
->nonRares()->count();

Check failure on line 18 in app/Http/ViewModels/Names/MaleNamesViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / Psalm

NullReference

app/Http/ViewModels/Names/MaleNamesViewModel.php:18:15: NullReference: Cannot call method nonRares on null value (see https://psalm.dev/016)

$letters = collect();
$letters->push([
Expand All @@ -25,7 +25,7 @@ public static function index(): Collection
]);

foreach ($alphabet as $letter) {
$total = Name::where('gender', 'male')
$total = Name::male()
->where('name', 'like', $letter . '%')->count();
$letters->push([
'letter' => $letter,
Expand Down
6 changes: 3 additions & 3 deletions app/Http/ViewModels/Names/MixteNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public static function index(): Collection
// iterate over the alphabet
$alphabet = range('A', 'Z');

$total = Name::where('unisex', 1)
->where('name', '!=', '_PRENOMS_RARES')->count();
$total = Name::unisex()

Check failure on line 17 in app/Http/ViewModels/Names/MixteNamesViewModel.php

View workflow job for this annotation

GitHub Actions / Static analysis / Psalm

InvalidStaticInvocation

app/Http/ViewModels/Names/MixteNamesViewModel.php:17:18: InvalidStaticInvocation: Method App\Models\Name::unisex is not static, but is called statically (see https://psalm.dev/014)
->nonRares()->count();

$letters = collect();
$letters->push([
Expand All @@ -25,7 +25,7 @@ public static function index(): Collection
]);

foreach ($alphabet as $letter) {
$total = Name::where('unisex', 1)
$total = Name::unisex()
->where('name', 'like', $letter . '%')->count();
$letters->push([
'letter' => $letter,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/ViewModels/Names/NameViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static function jsonLdSchema(Name $name): array
public static function relatedNames(Name $name): Collection
{
$ids = Name::select('id')
->where('name', '!=', '_PRENOMS_RARES')
->nonRares()
->where('id', '!=', $name->id)
->where('gender', $name->gender)
->inRandomOrder()
Expand Down
33 changes: 33 additions & 0 deletions app/Models/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
Expand Down Expand Up @@ -97,4 +98,36 @@ public function getNoteForUser(): ?string

return $note->content;
}

/**
* Scope a query to only include non rares names.
*/
public function scopeNonRares(Builder $query): void
{
$query->where('name', '!=', '_PRENOMS_RARES');
}

/**
* Scope a query to only include male names.
*/
public function male(Builder $query): void
{
$query->where('gender', 'male');
}

/**
* Scope a query to only include female names.
*/
public function female(Builder $query): void
{
$query->where('gender', 'female');
}

/**
* Scope a query to only include unisex names.
*/
public function unisex(Builder $query): void
{
$query->where('unisex', 1);
}
}

0 comments on commit b719f7e

Please sign in to comment.