Completed
Push — master ( 89be15...80fd60 )
by Igor
11:15
created

SocialAuth::prepareUser()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 4
nop 0
1
<?php
2
3
namespace app\modules\auth\services;
4
5
use yii\authclient\ClientInterface;
6
use app\models\entity\{User, UserProvider};
7
8
/**
9
 * SocialAuth handles successful authentication
10
 */
11
class SocialAuth
12
{
13
    /**
14
     * @var int
15
     */
16
    private $provider;
17
    /**
18
     * @var int
19
     */
20
    private $providerId;
21
    /**
22
     * @var ClientInterface
23
     */
24
    private $client;
25
    /**
26
     * @var array
27
     */
28
    public $parsers;
29
30
    public function __construct(ClientInterface $client)
31
    {
32
        $this->client = $client;
33
        $this->provider = UserProvider::getTypeByName($client->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface yii\authclient\ClientInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
34
        $this->providerId = $client->getUserAttributes()['id'];
35
    }
36
37
    public function prepareUser(): ?User
38
    {
39
        $user = null;
40
        $parser = $this->parser();
41
42
        $profileData = $parser->profileData();
43
        $tokenData = ['type' => $this->provider] + $parser->tokenData();
44
45
        if ($provider = UserProvider::find()->provider($this->provider, $this->providerId)->one()) {
46
            $user = $provider->user;
47
48
            // if exist then update access tokens
49
            $provider->setAttributes($tokenData);
50
            $provider->save();
51
        }
52
53
        if (!is_object($user)) {
54
            $user = new User();
55
56
            $user->email = $parser->email();
57
            $user->setProfile($profileData);
58
            $user->setProviders($tokenData);
59
        }
60
61
        return $user;
62
    }
63
64
    private function parser()
65
    {
66
        $parserClass = $this->parsers[$this->client->id];
0 ignored issues
show
Bug introduced by
Accessing id on the interface yii\authclient\ClientInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
67
        return new $parserClass($this->client);
68
    }
69
}
70