This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RREST\Response; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\ResponseHeaderBag; | ||
|
||
final class FileConfiguration | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $headerContentDisposition; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $headerContentLength; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $deleteFileAfterSend; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $filePath; | ||
|
||
public function __construct(string $filePath, bool $deleteFileAfterSend = false) | ||
{ | ||
$this->buildHeaderContentDisposition(pathinfo($filePath, PATHINFO_BASENAME)); | ||
$this->headerContentLength = filesize($filePath); | ||
$this->deleteFileAfterSend = $deleteFileAfterSend; | ||
$this->filePath = $filePath; | ||
} | ||
|
||
private function buildHeaderContentDisposition(string $filename) | ||
{ | ||
$symfonyResponse = new Response(); | ||
$this->headerContentDisposition = $symfonyResponse->headers->makeDisposition( | ||
ResponseHeaderBag::DISPOSITION_ATTACHMENT, | ||
$filename | ||
); | ||
} | ||
|
||
public function setFile(string $filePath) | ||
{ | ||
if (!is_readable($filePath)) { | ||
throw new \RuntimeException('Provided file is not readable or doesn\'t exists.'); | ||
} | ||
if (!is_file($filePath)) { | ||
throw new \RuntimeException('Provided file is not a file'); | ||
} | ||
$this->filePath = $filePath; | ||
} | ||
|
||
public function getFilePath(): string | ||
{ | ||
return $this->filePath; | ||
} | ||
|
||
public function getDeleteFileAfterSend(): bool | ||
{ | ||
return $this->deleteFileAfterSend; | ||
} | ||
|
||
public function getHttpHeadersForFileResponse(): array | ||
{ | ||
return [ | ||
'Content-disposition' => $this->headerContentDisposition, | ||
'Content-Length' => $this->headerContentLength, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters