1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace App\Src\UseCases\Domain\Auth; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use App\Exceptions\Domain\ProviderNotSupported; |
8
|
|
|
use App\Src\UseCases\Domain\Auth\Services\RegisterUserFromSocialNetworkService; |
9
|
|
|
use App\Src\UseCases\Domain\Ports\UserRepository; |
10
|
|
|
use App\Src\UseCases\Domain\Shared\Gateway\AuthGateway; |
11
|
|
|
use App\Src\UseCases\Domain\Shared\Gateway\SocialiteGateway; |
12
|
|
|
|
13
|
|
|
class LogUserFromSocialNetwork |
14
|
|
|
{ |
15
|
|
|
private $allowedProviders = ['facebook', 'twitter', 'google']; |
16
|
|
|
|
17
|
|
|
private $userRepository; |
18
|
|
|
private $socialiteGateway; |
19
|
|
|
private $authGateway; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
UserRepository $userRepository, |
23
|
|
|
SocialiteGateway $socialiteGateway, |
24
|
|
|
AuthGateway $authGateway |
25
|
|
|
) |
26
|
|
|
{ |
27
|
|
|
$this->userRepository = $userRepository; |
28
|
|
|
$this->socialiteGateway = $socialiteGateway; |
29
|
|
|
$this->authGateway = $authGateway; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $provider |
34
|
|
|
* @throws ProviderNotSupported |
35
|
|
|
*/ |
36
|
|
|
public function log(string $provider) |
37
|
|
|
{ |
38
|
|
|
$this->checkProviderAllowed($provider); |
39
|
|
|
|
40
|
|
|
$socialiteUser = $this->socialiteGateway->user($provider); |
41
|
|
|
|
42
|
|
|
$user = $this->userRepository->getByProvider($provider, $socialiteUser->providerId()); |
43
|
|
|
if($user === null){ |
44
|
|
|
app(RegisterUserFromSocialNetworkService::class)->register($provider, $socialiteUser); |
45
|
|
|
$user = $this->userRepository->getByProvider($provider, $socialiteUser->providerId()); |
46
|
|
|
} |
47
|
|
|
$this->authGateway->log($user); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $provider |
52
|
|
|
* @throws ProviderNotSupported |
53
|
|
|
*/ |
54
|
|
|
private function checkProviderAllowed(string $provider): void |
55
|
|
|
{ |
56
|
|
|
if (!in_array($provider, $this->allowedProviders)) { |
57
|
|
|
throw new ProviderNotSupported(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|