Completed
Push — 3.x ( 487293...957ed2 )
by Дмитрий
03:25
created

LinkedIn::getIdentity()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 53
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 11.2523

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 8
eloc 33
c 4
b 0
f 0
nc 2
nop 1
dl 0
loc 53
ccs 17
cts 27
cp 0.6296
crap 11.2523
rs 8.1475

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 * @author: Bogdan Popa https://github.com/icex <[email protected]>
6
 */
7
declare(strict_types=1);
8
9
namespace SocialConnect\OAuth2\Provider;
10
11
use SocialConnect\Common\ArrayHydrator;
12
use SocialConnect\Provider\AccessTokenInterface;
13
use SocialConnect\Common\Entity\User;
14
15
class LinkedIn extends \SocialConnect\OAuth2\AbstractProvider
16
{
17
    const NAME = 'linkedin';
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 4
    public function getBaseUri()
23
    {
24 4
        return 'https://api.linkedin.com/v2/';
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 2
    public function getAuthorizeUri()
31
    {
32 2
        return 'https://www.linkedin.com/oauth/v2/authorization';
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 2
    public function getRequestTokenUri()
39
    {
40 2
        return 'https://www.linkedin.com/oauth/v2/accessToken';
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 3
    public function getName()
47
    {
48 3
        return self::NAME;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54 3
    public function prepareRequest(string $method, string $uri, array &$headers, array &$query, AccessTokenInterface $accessToken = null): void
55
    {
56 3
        $headers['Content-Type'] = 'application/json';
57
58 3
        if ($accessToken) {
59 3
            $headers['Authorization'] = "Bearer {$accessToken->getToken()}";
60
        }
61 3
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 3
    public function getIdentity(AccessTokenInterface $accessToken)
67
    {
68 3
        $query = [];
69
70 3
        $fields = $this->getArrayOption(
71 3
            'identity.fields',
72
            [
73 3
                'id',
74
                'firstName',
75
                'lastName',
76
                'emailAddress',
77
                'profilePicture(displayImage~:playableStreams)',
78
                'location'
79
            ]
80
        );
81 3
        if ($fields) {
82 3
            $query['projection'] = '(' . implode(',', $fields) . ')';
83
        }
84
85 3
        $response = $this->request(
86 3
            'GET',
87 3
            'me',
88
            $query,
89
            $accessToken
90
        );
91
92 1
        $hydrator = new ArrayHydrator([
93 1
            'id'           => 'id',
94 1
            'emailAddress' => 'email',
95
            'firstName'    => static function ($value, User $user) {
96
                if ($value['localized']) {
97
                    $user->firstname = array_pop($value['localized']);
98
                }
99 1
            },
100
            'lastName'     => static function ($value, User $user) {
101
                if ($value['localized']) {
102
                    $user->lastname = array_pop($value['localized']);
103
                }
104 1
            },
105
            'profilePicture'     => static function ($value, User $user) {
106
                if (isset($value['displayImage~']) && isset($value['displayImage~']['elements'])) {
107
                    $biggestElement = array_shift($value['displayImage~']['elements']);
108
                    if (isset($biggestElement['identifiers'])) {
109
                        $biggestElementIdentifier = array_pop($biggestElement['identifiers']);
110
                        if (isset($biggestElementIdentifier['identifier'])) {
111
                            $user->pictureURL = $biggestElementIdentifier['identifier'];
112
                        }
113
                    }
114
                }
115 1
            },
116
        ]);
117
118 1
        return $hydrator->hydrate(new User(), $response);
119
    }
120
}
121