Completed
Push — master ( d47dcb...df81e1 )
by Igor
03:40
created

SocialAuth::parseProfile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 14
nc 4
nop 0
1
<?php
2
3
namespace app\services;
4
5
use yii\authclient\ClientInterface;
6
use app\models\entity\User;
7
use app\models\entity\UserProvider;
8
9
/**
10
 * SocialAuth handles successful authentication
11
 */
12
class SocialAuth
13
{
14
    /**
15
     * @var int
16
     */
17
    private $provider;
18
    /**
19
     * @var int
20
     */
21
    private $providerId;
22
    /**
23
     * @var ClientInterface
24
     */
25
    private $client;
26
    /**
27
     * @var array
28
     */
29
    public $parsers;
30
31
    public function __construct(ClientInterface $client)
32
    {
33
        $this->client = $client;
34
        $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...
35
        $this->providerId = $client->getUserAttributes()['id'];
36
    }
37
38
    public function prepareUser(): ?User
39
    {
40
        $user = null;
41
        $parser = $this->parser();
42
43
        $profileData = $parser->profileData();
44
        $tokenData = ['type' => $this->provider] + $parser->tokenData();
45
46
        if ($provider = UserProvider::find()->provider($this->provider, $this->providerId)->one()) {
47
            $user = $provider->user;
48
49
            // if exist then update access tokens
50
            $provider->setAttributes($tokenData);
51
            $provider->save();
52
        }
53
54
        if (!is_object($user)) {
55
            $user = new User();
56
57
            $user->email = $parser->email();
58
            $user->setProfile($profileData);
59
            $user->setProviders($tokenData);
60
        }
61
62
        return $user;
63
    }
64
65
    private function parser()
66
    {
67
        $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...
68
        return new $parserClass($this->client);
69
    }
70
}
71