Completed
Pull Request — master (#23)
by
unknown
12:53
created

LinkedInResourceOwner::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php namespace League\OAuth2\Client\Provider;
2
3
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
4
5
class LinkedInResourceOwner implements ResourceOwnerInterface
6
{
7
    use ArrayAccessorTrait;
8
9
    /**
10
     * Raw response
11
     *
12
     * @var array
13
     */
14
    protected $response;
15
16
    /**
17
     * Creates new resource owner.
18
     *
19
     * @param array  $response
20
     */
21
    public function __construct(array $response = array())
22
    {
23
        $this->response = $response;
24
    }
25
26 6
    /**
27
     * Gets resource owner attribute by key. The key supports dot notation.
28 6
     *
29 6
     * @return mixed
30
     */
31
    public function getAttribute($key)
32
    {
33
        return $this->getValueByKey($this->response, (string) $key);
34
    }
35
36 6
    /**
37
     * Get user firstname
38 6
     *
39
     * @return string|null
40
     */
41
    public function getFirstName()
42
    {
43
        return $this->getAttribute('profile.localizedFirstName');
44
    }
45
46 6
    /**
47
     * Get user lastname
48 6
     *
49
     * @return string|null
50
     */
51
    public function getLastName()
52
    {
53
        return $this->getAttribute('profile.localizedLastName');
54
    }
55
56 6
    /**
57
     * Get user imageurl
58 6
     *
59
     * @return string|null
60
     */
61
    public function getImageUrl()
62
    {
63
        return $this->getBiggestProfilePictureUrl();
64
    }
65
66 6
    /**
67
     * @return string|null
68 6
     */
69
    public function getBiggestProfilePictureUrl()
70
    {
71
        $pictures = $this->getSortedProfilePictures();
72
        $picture = array_pop($pictures);
73
74
        return $picture ? $picture['url'] : null;
75
    }
76 6
77
    /**
78 6
     * Get user userId
79
     *
80
     * @return string|null
81
     */
82
    public function getId()
83
    {
84
        return $this->getAttribute('profile.id');
85
    }
86 6
87
    /**
88 6
     * @return string|null
89
     */
90
    public function getEmail()
91
    {
92
        $emailResponse = $this->getAttribute('email.elements');
93
94
        if (is_array($emailResponse)) {
95
            $emailResponse = array_filter($emailResponse, function ($element) {
96 6
                return
97
                    strtoupper($element['type']) === 'EMAIL'
98 6
                    && strtoupper($element['state']) === 'CONFIRMED'
99
                    && $element['primary'] === true
100
                    && isset($element['handle~']['emailAddress'])
101
                ;
102
            });
103
            $emailResponse = array_pop($emailResponse);
104
            $emailResponse = $emailResponse ? $emailResponse['handle~']['emailAddress'] : null;
105
        }
106 6
107
        return $emailResponse;
108 6
    }
109
110
    /**
111
     * Return all of the owner details available as an array.
112
     *
113
     * @return array
114
     */
115
    public function toArray()
116 6
    {
117
        return $this->response;
118 6
    }
119
120
    /**
121
     * @return array|mixed
122
     */
123
    private function getSortedProfilePictures()
124
    {
125
        $pictures = $this->getAttribute('profile.profilePicture.displayImage~.elements');
126 6
127
        if ($pictures) {
128 6
            $pictures = array_filter($pictures, function ($element) {
129
                // filter to public images only
130
                return
131
                    isset($element['data']['com.linkedin.digitalmedia.mediaartifact.StillImage'])
132
                    && strtoupper($element['authorizationMethod']) === 'PUBLIC'
133
                    && isset($element['identifiers'][0]['identifier'])
134
                ;
135
            });
136 6
137
            // order images by width, LinkedIn profile pictures are always squares, so that should be good enough
138 6
            usort($pictures, function ($elementA, $elementB) {
139
                $wA = $elementA['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'];
140
                $wB = $elementB['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'];
141
142
                return $wA - $wB;
143
            });
144
145
            $pictures = array_map(function ($element) {
146
                // this is an URL, no idea how many of identifiers there can be, so take the first one.
147
                $url = $element['identifiers'][0]['identifier'];
148
                $type = $element['identifiers'][0]['mediaType'];
149
                $width = $element['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'];
150
151
                return [
152
                    'width' => $width,
153
                    'url' => $url,
154
                    'contentType' => $type,
155
                ];
156
            }, $pictures);
157
        } else {
158
            $pictures = [];
159
        }
160
161
        return $pictures;
162
    }
163
}
164