packthub_ebook_integration-1.0.0/src/Plugin/ProductsAssets.php
src/Plugin/ProductsAssets.php
<?php
namespace Drupal\packt\Plugin;
class ProductsAssets {
private string $product;
private string $file_name;
public function getFileName(): string {
return $this->file_name;
}
/**
* @param string $product Give product id or isbn.
*
* @return void
*/
public function setProduct(string $product): void {
$this->product = $product;
}
/**
* @param string $file_name Filename of the file if we have multiple files uploaded for single distribution type.
*
* @return void
*/
public function setFileName(string $file_name): void {
$this->file_name = $file_name;
}
/**
* @param bool $resolveType True will return array of mime_type and data. False
* will return data stream.
*
* @return mixed
*/
public function getAssets(bool $resolveType = true):mixed {
$token = packt_api_token();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.packt.com/api/v2/products/$this->product/$this->file_name?token=$token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
curl_close($curl);
if(!empty($response) && $resolveType) {
// Create a FileInfo object
$info = finfo_open(FILEINFO_MIME_TYPE);
// Get the MIME type directly from the binary data
$mime_type = finfo_buffer($info, $response);
// Close the FileInfo object
finfo_close($info);
return ['mime_type' => $mime_type, 'data'=>$response];
}
return $response;
}
}
