neayi /
insights
| 1 | <?php |
||
| 2 | |||
| 3 | |||
| 4 | namespace App\Src\UseCases\Domain\Auth\Services; |
||
| 5 | |||
| 6 | |||
| 7 | use App\Src\UseCases\Domain\Auth\SocialiteUser; |
||
| 8 | use App\Src\UseCases\Domain\Ports\UserRepository; |
||
| 9 | use App\Src\UseCases\Domain\Shared\Gateway\FileStorage; |
||
| 10 | use App\Src\UseCases\Domain\Shared\Model\Picture; |
||
| 11 | use App\Src\UseCases\Domain\User; |
||
| 12 | use Illuminate\Support\Facades\Validator; |
||
| 13 | use Ramsey\Uuid\Uuid; |
||
| 14 | |||
| 15 | class RegisterUserFromSocialNetworkService |
||
| 16 | { |
||
| 17 | private $userRepository; |
||
| 18 | private $fileStorage; |
||
| 19 | |||
| 20 | public function __construct( |
||
| 21 | UserRepository $userRepository, |
||
| 22 | FileStorage $fileStorage |
||
| 23 | ) |
||
| 24 | { |
||
| 25 | $this->userRepository = $userRepository; |
||
| 26 | $this->fileStorage = $fileStorage; |
||
| 27 | } |
||
| 28 | |||
| 29 | public function register(string $provider, SocialiteUser $socialiteUser) |
||
| 30 | { |
||
| 31 | $user = $this->userRepository->getByProvider($provider, $socialiteUser->providerId()); |
||
| 32 | if(isset($user)){ |
||
| 33 | return [ |
||
| 34 | 'user_id' => $user->id(), |
||
| 35 | 'provider_id' => $socialiteUser->providerId(), |
||
| 36 | 'state' => 'user_already_exist' |
||
| 37 | ]; |
||
| 38 | } |
||
| 39 | |||
| 40 | if($socialiteUser->email() !== null && $socialiteUser->email() != "") { |
||
| 41 | $user = $this->userRepository->getByEmail($socialiteUser->email()); |
||
| 42 | } |
||
| 43 | if(isset($user)){ |
||
| 44 | $user->addProvider($provider, $socialiteUser->providerId()); |
||
| 45 | return [ |
||
| 46 | 'user_id' => $user->id(), |
||
| 47 | 'provider_id' => $socialiteUser->providerId(), |
||
| 48 | ]; |
||
| 49 | } |
||
| 50 | |||
| 51 | $this->validateData($socialiteUser); |
||
| 52 | return $this->createUser($socialiteUser, $provider); |
||
| 53 | } |
||
| 54 | |||
| 55 | private function validateData(SocialiteUser $socialiteUser): void |
||
| 56 | { |
||
| 57 | $rules = [ |
||
| 58 | 'email' => 'string|email|min:2|max:255|nullable', |
||
| 59 | 'firstname' => 'string|min:2|max:255', |
||
| 60 | 'lastname' => 'string|min:2|max:255' |
||
| 61 | ]; |
||
| 62 | |||
| 63 | $data = [ |
||
| 64 | 'email' => $socialiteUser->email(), |
||
| 65 | 'firstname' => $socialiteUser->firstname(), |
||
| 66 | 'lastname' => $socialiteUser->lastname(), |
||
| 67 | 'provider_id' => $socialiteUser->providerId(), |
||
| 68 | 'picture_url' => $socialiteUser->pictureUrl() |
||
| 69 | ]; |
||
| 70 | $validator = Validator::make($data, $rules); |
||
| 71 | $validator->validate(); |
||
| 72 | } |
||
| 73 | |||
| 74 | private function handlePicture(SocialiteUser $socialiteUser): ?Picture |
||
| 75 | { |
||
| 76 | return $socialiteUser->pictureUrl() !== null ? $this->fileStorage->uriToTmpPicture($socialiteUser->pictureUrl()) : null; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param SocialiteUser $socialiteUser |
||
| 81 | * @param string $provider |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | private function createUser(SocialiteUser $socialiteUser, string $provider): array |
||
| 85 | { |
||
| 86 | $user = new User($id = Uuid::uuid4(), $socialiteUser->email(), $socialiteUser->firstname(), $socialiteUser->lastname(), null, '', [], [$provider => $socialiteUser->providerId()]); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 87 | $picture = $this->handlePicture($socialiteUser); |
||
| 88 | $user->create(null, $picture); |
||
| 89 | |||
| 90 | // If the user has an email, we take it for granted that Google or Facebook has already verified it: |
||
| 91 | $email = $socialiteUser->email(); |
||
| 92 | if (!empty($email)) |
||
| 93 | $this->userRepository->verifyEmail($id); |
||
| 94 | |||
| 95 | return [ |
||
| 96 | 'user_id' => $id, |
||
| 97 | 'provider_id' => $socialiteUser->providerId(), |
||
| 98 | ]; |
||
| 99 | |||
| 100 | } |
||
| 101 | } |
||
| 102 |