apiRequest('api/users.identity');
$data = new Data\Collection($response);
if (!$data->exists('ok') || !$data->get('ok')) {
throw new UnexpectedApiResponseException('Provider API returned an unexpected response.');
}
$userProfile = new User\Profile();
$userProfile->identifier = $data->filter('user')->get('id');
$userProfile->displayName = $data->filter('user')->get('name');
$userProfile->email = $data->filter('user')->get('email');
$userProfile->photoURL = $this->findLargestImage($data);
return $userProfile;
}
/**
* Returns the url of the image with the highest resolution in the user
* object.
*
* Slack sends multiple image urls with different resolutions. As they make
* no guarantees which resolutions will be included we have to search all
* image_* properties for the one with the highest resolution.
* The resolution is attached to the property name such as
* image_32 or image_192.
*
* @param Data\Collection $data response object as returned by
* api/users.identity
*
* @return string|null the value of the image_* property with
* the highest resolution.
*/
private function findLargestImage(Data\Collection $data)
{
$maxSize = 0;
foreach ($data->filter('user')->properties() as $property) {
if (preg_match('/^image_(\d+)$/', $property, $matches) === 1) {
$availableSize = (int)$matches[1];
if ($maxSize < $availableSize) {
$maxSize = $availableSize;
}
}
}
if ($maxSize > 0) {
return $data->filter('user')->get('image_' . $maxSize);
}
return null;
}
}