init commit
This commit is contained in:
127
app/Services/ProfileCompletionService.php
Normal file
127
app/Services/ProfileCompletionService.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
/**
|
||||
* Profile Completion Service
|
||||
*
|
||||
* Handles user profile completion calculations and analytics.
|
||||
*
|
||||
* @author David Valera Melendez <david@valera-melendez.de>
|
||||
* @since February 2025
|
||||
*/
|
||||
class ProfileCompletionService
|
||||
{
|
||||
/**
|
||||
* Required fields for profile completion
|
||||
*/
|
||||
private const REQUIRED_FIELDS = [
|
||||
'first_name' => 10,
|
||||
'last_name' => 10,
|
||||
'email' => 15,
|
||||
'phone' => 10,
|
||||
'bio' => 20,
|
||||
'website' => 10,
|
||||
'linkedin' => 15,
|
||||
'github' => 10,
|
||||
];
|
||||
|
||||
/**
|
||||
* Calculate profile completion percentage
|
||||
*/
|
||||
public function calculateCompletion(User $user): int
|
||||
{
|
||||
$totalWeight = array_sum(self::REQUIRED_FIELDS);
|
||||
$completedWeight = 0;
|
||||
|
||||
foreach (self::REQUIRED_FIELDS as $field => $weight) {
|
||||
if (!empty($user->$field)) {
|
||||
$completedWeight += $weight;
|
||||
}
|
||||
}
|
||||
|
||||
return (int) (($completedWeight / $totalWeight) * 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get missing profile fields
|
||||
*/
|
||||
public function getMissingFields(User $user): array
|
||||
{
|
||||
$missing = [];
|
||||
|
||||
foreach (self::REQUIRED_FIELDS as $field => $weight) {
|
||||
if (empty($user->$field)) {
|
||||
$missing[] = [
|
||||
'field' => $field,
|
||||
'label' => $this->getFieldLabel($field),
|
||||
'weight' => $weight,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $missing;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user-friendly field labels
|
||||
*/
|
||||
private function getFieldLabel(string $field): string
|
||||
{
|
||||
$labels = [
|
||||
'first_name' => 'First Name',
|
||||
'last_name' => 'Last Name',
|
||||
'email' => 'Email Address',
|
||||
'phone' => 'Phone Number',
|
||||
'bio' => 'Professional Bio',
|
||||
'website' => 'Website',
|
||||
'linkedin' => 'LinkedIn Profile',
|
||||
'github' => 'GitHub Profile',
|
||||
];
|
||||
|
||||
return $labels[$field] ?? ucfirst(str_replace('_', ' ', $field));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if profile is considered complete
|
||||
*/
|
||||
public function isProfileComplete(User $user): bool
|
||||
{
|
||||
return $this->calculateCompletion($user) >= 80;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get profile completion statistics
|
||||
*/
|
||||
public function getCompletionStats(User $user): array
|
||||
{
|
||||
$completion = $this->calculateCompletion($user);
|
||||
$missingFields = $this->getMissingFields($user);
|
||||
|
||||
return [
|
||||
'completion_percentage' => $completion,
|
||||
'is_complete' => $this->isProfileComplete($user),
|
||||
'missing_fields' => $missingFields,
|
||||
'missing_count' => count($missingFields),
|
||||
'status' => $this->getCompletionStatus($completion),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get completion status message
|
||||
*/
|
||||
private function getCompletionStatus(int $completion): string
|
||||
{
|
||||
if ($completion >= 90) {
|
||||
return 'excellent';
|
||||
} elseif ($completion >= 70) {
|
||||
return 'good';
|
||||
} elseif ($completion >= 50) {
|
||||
return 'fair';
|
||||
} else {
|
||||
return 'incomplete';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user