Skip to content

Commit

Permalink
Refactor Downloader classes to improve code structure and readability
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Jan 18, 2024
1 parent 0490b4a commit 52df444
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
18 changes: 8 additions & 10 deletions src/Utils/Downloader/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace Kiwilan\Steward\Utils\Downloader;

/**
* Downloader to download a file or a stream of zip files.
*
* - Use `Downloader::direct()` to download a file directly.
* - Use `Downloader::stream()` to download a stream of zip files.
*/
class Downloader
{
protected function __construct(
Expand All @@ -12,23 +18,15 @@ protected function __construct(
) {
}

/**
* Initialize the downloader.
*/
public static function make(): self
{
return new self();
}

public function direct(string $path): DownloaderDirect
public static function direct(string $path): DownloaderDirect
{
$download = new DownloaderDirect($path);
$download->filename = basename($path);

return $download;
}

public function stream(string $filename): DownloaderZipStream
public static function stream(string $filename): DownloaderZipStream
{
$zip = new DownloaderZipStream();
$zip->filename = "{$filename}.zip";
Expand Down
9 changes: 9 additions & 0 deletions src/Utils/Downloader/DownloaderDirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Kiwilan\Steward\Utils\Downloader;

/**
* Downloader to download a file.
*/
class DownloaderDirect extends Downloader
{
protected function __construct(
Expand All @@ -10,13 +13,19 @@ protected function __construct(
parent::__construct(basename($path));
}

/**
* Set the value of mimeType. If null, it will be automatically determined from the filename.
*/
public function autoMimeType(): self
{
$this->mimeType = mime_content_type($this->path);

return $this;
}

/**
* Trigger the download.
*/
public function get(): void
{
$this->size = filesize($this->path);
Expand Down
8 changes: 7 additions & 1 deletion src/Utils/Downloader/DownloaderZipStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Kiwilan\Steward\Utils\Downloader;

/**
* Downloader to download a stream of zip files.
*/
class DownloaderZipStream extends Downloader
{
/**
Expand All @@ -14,7 +17,7 @@ protected function __construct(
}

/**
* Set the value of files.
* Set files to be downloaded, use `DownloaderZipStreamItem` to create a file.
*
* @param DownloaderZipStreamItem[] $files
*/
Expand All @@ -25,6 +28,9 @@ public function files(array $files): self
return $this;
}

/**
* Trigger the download.
*/
public function get(): void
{
ini_set('max_execution_time', $this->maxExecutionTime);
Expand Down

0 comments on commit 52df444

Please sign in to comment.