Skip to content
Merged
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
59 changes: 29 additions & 30 deletions app/Console/Commands/LocationCreateOrUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

use App\Models\Country;
use App\Models\State;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

class LocationCreateOrUpdateCommand extends Command
final class LocationCreateOrUpdateCommand extends Command
{
/**
* The name and signature of the console command.
Expand All @@ -29,10 +30,8 @@ class LocationCreateOrUpdateCommand extends Command

/**
* Base API URL for the countries and states data.
*
* @var string
*/
protected $baseApiUrl = 'https://countriesnow.space/api/v0.1';
private string $baseApiUrl = 'https://countriesnow.space/api/v0.1';

/**
* Execute the console command.
Expand All @@ -57,29 +56,31 @@ public function handle(): void
}

DB::commit();
} catch (\Exception $e) {
} catch (Exception $exception) {
DB::rollBack();
$this->error('Command failed: ' . $e->getMessage());
$this->error('Command failed: '.$exception->getMessage());
}
}

/**
* Get the countries data from the API.
*
* @return list<array<string, string>>
*/
private function getCountries(): array
private function getCountries()
{
try {
$response = Http::get("{$this->baseApiUrl}/countries/iso");
$response = Http::get($this->baseApiUrl.'/countries/iso');

if ($response->successful()) {
return $response->json('data');
}

$this->error('Failed to fetch countries data from API and the error is: ' . ($response->json('msg') ?? 'unknown error'));
$this->error('Failed to fetch countries data from API and the error is: '.($response->json('msg') ?? 'unknown error'));

return [];
} catch (\Exception $e) {
$this->error('Error fetching countries: ' . $e->getMessage());
} catch (Exception $exception) {
$this->error('Error fetching countries: '.$exception->getMessage());

return [];
}
Expand All @@ -92,13 +93,11 @@ private function createOrUpdateCountries(array $countries): void
{
$this->info('Creating or updating country data...');

$countryData = array_map(function ($country) {
return [
'name' => Str::lower($country['name']),
'iso2' => $country['Iso2'],
'iso3' => $country['Iso3'],
];
}, $countries);
$countryData = array_map(fn (array $country): array => [
'name' => Str::lower($country['name']),
'iso2' => $country['Iso2'],
'iso3' => $country['Iso3'],
], $countries);

Country::upsert(
$countryData,
Expand All @@ -114,21 +113,23 @@ private function createOrUpdateCountries(array $countries): void

/**
* Get the states data from the API.
*
* @return list<array<string, string|list<array<string, string>>>>
*/
private function getStates(): array
private function getStates()
{
try {
$response = Http::get("{$this->baseApiUrl}/countries/states");
$response = Http::get($this->baseApiUrl.'/countries/states');

if ($response->successful()) {
return $response->json('data');
}

$this->error('Failed to fetch states data from API and the error is: ' . ($response->json('msg') ?? 'unknown error'));
$this->error('Failed to fetch states data from API and the error is: '.($response->json('msg') ?? 'unknown error'));

return [];
} catch (\Exception $e) {
$this->error('Error fetching states: ' . $e->getMessage());
} catch (Exception $exception) {
$this->error('Error fetching states: '.$exception->getMessage());

return [];
}
Expand All @@ -152,13 +153,11 @@ private function createOrUpdateStates(array $states): void
continue;
}

$stateData = array_map(function ($state) use ($countries, $countryIsoCode) {
return [
'name' => Str::lower($state['name']),
'code' => $state['state_code'],
'country_id' => $countries[$countryIsoCode],
];
}, $stateData);
$stateData = array_map(fn (array $state): array => [
'name' => Str::lower($state['name']),
'code' => $state['state_code'],
'country_id' => $countries[$countryIsoCode],
], $stateData);

State::upsert(
$stateData,
Expand Down
4 changes: 2 additions & 2 deletions app/Console/Commands/MakeAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;

class MakeAdmin extends Command
final class MakeAdmin extends Command
{
/**
* The name and signature of the console command.
Expand All @@ -26,7 +26,7 @@ class MakeAdmin extends Command
/**
* Execute the console command.
*/
public function handle()
public function handle(): void
{
$email = $this->option('email');
$password = $this->option('password');
Expand Down
2 changes: 1 addition & 1 deletion app/Helpers/GeneralHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
function getVoterNumber()
{
$voterNumber = 'VO' . str_pad((string) rand(0, 99999999), 8, '0', STR_PAD_LEFT);
$voterNumber = 'VO'.mb_str_pad((string) random_int(0, 99999999), 8, '0', STR_PAD_LEFT);
$existingVoter = Voter::where('voter_number', $voterNumber)->first();

if ($existingVoter) {
Expand Down
12 changes: 4 additions & 8 deletions app/Http/Controllers/Api/LocationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@

use App\Http\Controllers\Controller;
use App\Services\LocationService;
use Exception;
use Illuminate\Http\JsonResponse;

class LocationController extends Controller
final class LocationController extends Controller
{
protected LocationService $locationService;

public function __construct(LocationService $locationService)
{
$this->locationService = $locationService;
}
public function __construct(private readonly LocationService $locationService) {}

/**
* Get all countries
Expand All @@ -34,7 +30,7 @@ public function statesByCountry(string $countryName): JsonResponse
{
try {
$states = $this->locationService->getStatesByCountry($countryName);
} catch (\Exception $e) {
} catch (Exception) {
return response()->json(['error' => 'Country not found'], 404);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Inertia\Inertia;
use Inertia\Response;

class AuthenticatedSessionController extends Controller
final class AuthenticatedSessionController extends Controller
{
/**
* Display the login view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Inertia\Inertia;
use Inertia\Response;

class ConfirmablePasswordController extends Controller
final class ConfirmablePasswordController extends Controller
{
/**
* Show the confirm password view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class EmailVerificationNotificationController extends Controller
final class EmailVerificationNotificationController extends Controller
{
/**
* Send a new email verification notification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Inertia\Inertia;
use Inertia\Response;

class EmailVerificationPromptController extends Controller
final class EmailVerificationPromptController extends Controller
{
/**
* Display the email verification prompt.
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Auth/NewPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Inertia\Inertia;
use Inertia\Response;

class NewPasswordController extends Controller
final class NewPasswordController extends Controller
{
/**
* Display the password reset view.
Expand Down Expand Up @@ -47,7 +47,7 @@ public function store(Request $request): RedirectResponse
// database. Otherwise we will parse the error and return the response.
$status = Password::reset(
$request->only('email', 'password', 'password_confirmation', 'token'),
function ($user) use ($request) {
function ($user) use ($request): void {
$user->forceFill([
'password' => Hash::make($request->password),
'remember_token' => Str::random(60),
Expand All @@ -60,7 +60,7 @@ function ($user) use ($request) {
// If the password was successfully reset, we will redirect the user back to
// the application's home authenticated view. If there is an error we can
// redirect them back to where they came from with their error message.
if ($status == Password::PASSWORD_RESET) {
if ($status === Password::PASSWORD_RESET) {
return redirect()->route('login')->with('status', __($status));
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;

class PasswordController extends Controller
final class PasswordController extends Controller
{
/**
* Update the user's password.
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Auth/PasswordResetLinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Inertia\Inertia;
use Inertia\Response;

class PasswordResetLinkController extends Controller
final class PasswordResetLinkController extends Controller
{
/**
* Display the password reset link request view.
Expand Down Expand Up @@ -42,7 +42,7 @@ public function store(Request $request): RedirectResponse
$request->only('email')
);

if ($status == Password::RESET_LINK_SENT) {
if ($status === Password::RESET_LINK_SENT) {
return back()->with('status', __($status));
}

Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Inertia\Inertia;
use Inertia\Response;

class RegisteredUserController extends Controller
final class RegisteredUserController extends Controller
{
/**
* Display the registration view.
Expand All @@ -34,7 +34,7 @@ public function store(Request $request): RedirectResponse
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|lowercase|email|max:255|unique:' . User::class,
'email' => 'required|string|lowercase|email|max:255|unique:'.User::class,
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);

Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Auth/VerifyEmailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
use Illuminate\Foundation\Auth\EmailVerificationRequest;
use Illuminate\Http\RedirectResponse;

class VerifyEmailController extends Controller
final class VerifyEmailController extends Controller
{
/**
* Mark the authenticated user's email address as verified.
*/
public function __invoke(EmailVerificationRequest $request): RedirectResponse
{
if ($request->user()->hasVerifiedEmail()) {
return redirect()->intended(route('dashboard', absolute: false) . '?verified=1');
return redirect()->intended(route('dashboard', absolute: false).'?verified=1');
}

if ($request->user()->markEmailAsVerified()) {
event(new Verified($request->user()));
}

return redirect()->intended(route('dashboard', absolute: false) . '?verified=1');
return redirect()->intended(route('dashboard', absolute: false).'?verified=1');
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/CandidateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use App\Models\Election;
use Inertia\Inertia;

class CandidateController extends Controller
final class CandidateController extends Controller
{
/**
* Display the specified candidate.
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/CandidateRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Illuminate\Http\Request;
use Inertia\Inertia;

class CandidateRequestController extends Controller
final class CandidateRequestController extends Controller
{
/**
* Show the form for creating a candidate request.
Expand All @@ -26,6 +26,7 @@ public function create(Election $election, Request $request)
// Check if the user is an admin
abort(403, 'Unauthorized action.');
}

if ($candidate && $candidate->user_id !== $user->id) {
// Check if the user is authorized to create the request
abort(403, 'Unauthorized action.');
Expand Down Expand Up @@ -60,6 +61,7 @@ public function store(Election $election, StoreCandidateRequest $request)
// Check if the user is an admin
abort(403, 'Unauthorized action.');
}

if ($candidate && $candidate->user_id !== $user->id) {
// Check if the user is authorized to create the request
abort(403, 'Unauthorized action.');
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Illuminate\Http\Request;
use Inertia\Inertia;

class DashboardController extends Controller
final class DashboardController extends Controller
{
/**
* Handle the incoming request.
Expand Down
6 changes: 4 additions & 2 deletions app/Http/Controllers/ElectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Illuminate\Http\Request;
use Inertia\Inertia;

class ElectionController extends Controller
final class ElectionController extends Controller
{
/**
* Display a listing of the election.
Expand All @@ -26,9 +26,10 @@ public function index(Request $request)
$query->whereAny(
['name', 'description'],
'like',
'%' . $request->search . '%'
'%'.$request->search.'%'
);
}

$elections = $query->latest()->paginate(15)->onEachSide(1);

return Inertia::render('Elections/Index', [
Expand Down Expand Up @@ -85,6 +86,7 @@ public function show(Election $election)
$query->withCount('votes');
$election->loadCount('votes');
}

// dd($election, $query->get());
$candidates = $query->oldest()->paginate(15)->onEachSide(1);

Expand Down
Loading
Loading