public function DrupalGatherContentClient::downloadFiles in GatherContent 8.4
Same name and namespace in other branches
- 8.5 src/DrupalGatherContentClient.php \Drupal\gathercontent\DrupalGatherContentClient::downloadFiles()
Downloads all files asynchronously.
Parameters
array $files: Files object array.
string $directory: Destination directory.
string $language: Language string.
Return value
array Imported files array.
1 method overrides DrupalGatherContentClient::downloadFiles()
- MockDrupalGatherContentClient::downloadFiles in tests/
modules/ gathercontent_test/ src/ MockDrupalGatherContentClient.php - Mock download.
File
- src/
DrupalGatherContentClient.php, line 126
Class
- DrupalGatherContentClient
- Extends the GatherContentClient class with Drupal specific functionality.
Namespace
Drupal\gathercontentCode
public function downloadFiles(array $files, $directory, $language) {
/** @var \GuzzleHttp\Client $httpClient */
$httpClient = $this->client;
$options = [
'auth' => $this
->getRequestAuth(),
'headers' => [],
];
$options['headers'] += $this
->getRequestHeaders();
$files = array_values($files);
$importedFiles = [];
$requests = function () use ($httpClient, $files, $options) {
foreach ($files as $file) {
$url = $this
->getUri("files/{$file->id}/download");
(yield function () use ($httpClient, $url, $options) {
return $httpClient
->getAsync($url, $options);
});
}
};
$pool = new Pool($httpClient, $requests(), [
'fulfilled' => function ($response, $index) use ($files, $directory, $language, &$importedFiles) {
if ($response
->getStatusCode() === 200) {
$path = $directory . '/' . $files[$index]->fileName;
$importedFile = file_save_data($response
->getBody(), $path);
if ($importedFile) {
$importedFile
->set('gc_id', $files[$index]->id)
->set('langcode', $language)
->set('filesize', $files[$index]->size)
->save();
$importedFiles[$index] = $importedFile
->id();
}
}
},
]);
$promise = $pool
->promise();
$promise
->wait();
ksort($importedFiles);
return $importedFiles;
}