LinkedInResourceOwner::setSortedProfilePictures()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 22
cts 22
cp 1
rs 9.36
c 0
b 0
f 0
cc 4
nc 2
nop 0
crap 4
1
<?php namespace League\OAuth2\Client\Provider;
2
3
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
4
5
/**
6
 * @property array $response
7
 * @property string $uid
8
 */
9
class LinkedInResourceOwner extends GenericResourceOwner
10
{
11
12
    use ArrayAccessorTrait;
13
14
    /**
15
     * Raw response
16
     *
17
     * @var array
18
     */
19
    protected $response = [];
20
21
    /**
22
     * Sorted profile pictures
23
     *
24
     * @var array
25
     */
26
    protected $sortedProfilePictures = [];
27
28
    /**
29
     * @var string|null
30
     */
31
    private $email;
32
33
    /**
34
     * Creates new resource owner.
35
     *
36
     * @param array  $response
37
     */
38 3
    public function __construct(array $response = array())
39
    {
40 3
        $this->response = $response;
41 3
        $this->setSortedProfilePictures();
42 3
    }
43
44
    /**
45
     * Gets resource owner attribute by key. The key supports dot notation.
46
     *
47
     * @return mixed
48
     */
49 3
    public function getAttribute($key)
50
    {
51 3
        return $this->getValueByKey($this->response, (string) $key);
52
    }
53
54
    /**
55
     * Get user first name
56
     *
57
     * @return string|null
58
     */
59 3
    public function getFirstName()
60
    {
61 3
        return $this->getAttribute('localizedFirstName');
62
    }
63
64
    /**
65
     * Get user user id
66
     *
67
     * @return string|null
68
     */
69 3
    public function getId()
70
    {
71 3
        return $this->getAttribute('id');
72
    }
73
74
    /**
75
     * Get specific image by size
76
     *
77
     * @param integer $size
78
     * @return array|null
79
     */
80 1
    public function getImageBySize($size)
81
    {
82
        $pictures = array_filter($this->sortedProfilePictures, function ($picture) use ($size) {
83 1
            return isset($picture['width']) && $picture['width'] == $size;
84 1
        });
85
86 1
        return count($pictures) ? $pictures[0] : null;
87
    }
88
89
    /**
90
     * Get available user image sizes
91
     *
92
     * @return array
93
     */
94 1
    public function getImageSizes()
95
    {
96
        return array_map(function ($picture) {
97 1
            return $this->getValueByKey($picture, 'width');
98 1
        }, $this->sortedProfilePictures);
99
    }
100
101
    /**
102
     * Get user image url
103
     *
104
     * @return string|null
105
     */
106 3
    public function getImageUrl()
107
    {
108 3
        $pictures = $this->getSortedProfilePictures();
109 3
        $picture = array_pop($pictures);
110
111 3
        return $picture ? $this->getValueByKey($picture, 'url') : null;
112
    }
113
114
    /**
115
     * Get user last name
116
     *
117
     * @return string|null
118
     */
119 3
    public function getLastName()
120
    {
121 3
        return $this->getAttribute('localizedLastName');
122
    }
123
124
    /**
125
     * Returns the sorted collection of profile pictures.
126
     *
127
     * @return array
128
     */
129 3
    public function getSortedProfilePictures()
130
    {
131 3
        return $this->sortedProfilePictures;
132
    }
133
134
    /**
135
     * Get user url
136
     *
137
     * @return string|null
138
     */
139 3
    public function getUrl()
140
    {
141 3
        $vanityName = $this->getAttribute('vanityName');
142
143 3
        return $vanityName ? sprintf('https://www.linkedin.com/in/%s', $vanityName) : null;
144
    }
145
146
    /**
147
     * Get user email, if available
148
     *
149
     * @return string|null
150
     */
151 2
    public function getEmail()
152
    {
153 2
        return $this->getAttribute('email');
154
    }
155
156
    /**
157
     * Attempts to sort the collection of profile pictures included in the profile
158
     * before caching them in the resource owner instance.
159
     *
160
     * @return void
161
     */
162 3
    private function setSortedProfilePictures()
163
    {
164 3
        $pictures = $this->getAttribute('profilePicture.displayImage~.elements');
165 3
        if (is_array($pictures)) {
166
            $pictures = array_filter($pictures, function ($element) {
167
                // filter to public images only
168
                return
169 2
                    isset($element['data']['com.linkedin.digitalmedia.mediaartifact.StillImage'])
170 2
                    && strtoupper($element['authorizationMethod']) === 'PUBLIC'
171 2
                    && isset($element['identifiers'][0]['identifier'])
172
                ;
173 2
            });
174
            // order images by width, LinkedIn profile pictures are always squares, so that should be good enough
175
            usort($pictures, function ($elementA, $elementB) {
176 2
                $wA = $elementA['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'];
177 2
                $wB = $elementB['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'];
178 2
                return $wA - $wB;
179 2
            });
180 2
            $pictures = array_map(function ($element) {
181
                // this is an URL, no idea how many of identifiers there can be, so take the first one.
182 2
                $url = $element['identifiers'][0]['identifier'];
183 2
                $type = $element['identifiers'][0]['mediaType'];
184 2
                $width = $element['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'];
185
                return [
186 2
                    'width' => $width,
187 2
                    'url' => $url,
188 2
                    'contentType' => $type,
189
                ];
190 2
            }, $pictures);
191
        } else {
192 1
            $pictures = [];
193
        }
194
195 3
        $this->sortedProfilePictures = $pictures;
196 3
    }
197
198
    /**
199
     * Return all of the owner details available as an array.
200
     *
201
     * @return array
202
     */
203 2
    public function toArray()
204
    {
205 2
        return $this->response;
206
    }
207
}
208