LogUserFromSocialNetwork   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 45
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 12 2
A checkProviderAllowed() 0 4 2
A __construct() 0 9 1
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);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $u of App\Src\UseCases\Domain\...eway\AuthGateway::log() does only seem to accept App\Src\UseCases\Domain\User, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        $this->authGateway->log(/** @scrutinizer ignore-type */ $user);
Loading history...
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