Completed
Push — master ( 3f6851...6e71e0 )
by Milan
03:43
created

Download::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace h4kuna\Upload;
4
5
use Nette\Application,
6
	Nette\Http;
7
8
class Download
9
{
10
	/** @var IDriver */
11
	private $driver;
12
13
	/** @var Http\Request */
14
	private $request;
15
16
	/** @var Http\Response */
17
	private $response;
18
19
	public function __construct(IDriver $driver, Http\Request $request, Http\Response $response)
20
	{
21
		$this->driver = $driver;
22
		$this->request = $request;
23
		$this->response = $response;
24
	}
25
26
	/**
27
	 * @param IStoreFile $file
28
	 * @param bool $forceDownload
29
	 * @return Application\Responses\FileResponse
30
	 */
31
	public function createFileResponse(IStoreFile $file, $forceDownload = TRUE)
32
	{
33
		return new Application\Responses\FileResponse(
34
			$this->driver->createURI($file),
35
			$file->getName(), $file->getContentType(), $forceDownload);
36
	}
37
38
	/**
39
	 * @param IStoreFile $file
40
	 * @param bool $forceDownload
41
	 * @throws FileDownloadFailedException
42
	 */
43
	public function send(IStoreFile $file, $forceDownload = TRUE)
44
	{
45
		try {
46
			$this->createFileResponse($file, $forceDownload)->send($this->request, $this->response);
47
		} catch (Application\BadRequestException $e) {
48
			throw new FileDownloadFailedException($e->getMessage(), NULL, $e);
49
		}
50
	}
51
}
52