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