1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Action; |
6
|
|
|
|
7
|
|
|
use Application\Model\AccountingDocument; |
8
|
|
|
use Application\Repository\AccountingDocumentRepository; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Zend\Diactoros\Response; |
13
|
|
|
|
14
|
|
|
class AccountingDocumentAction extends AbstractAction |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var AccountingDocumentRepository |
18
|
|
|
*/ |
19
|
|
|
private $accountingDocumentRepository; |
20
|
|
|
|
21
|
|
|
public function __construct(AccountingDocumentRepository $accountingDocumentRepository) |
22
|
|
|
{ |
23
|
|
|
$this->accountingDocumentRepository = $accountingDocumentRepository; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Serve a downloaded file from disk |
28
|
|
|
* |
29
|
|
|
* @param ServerRequestInterface $request |
30
|
|
|
* @param RequestHandlerInterface $handler |
31
|
|
|
* |
32
|
|
|
* @return ResponseInterface |
33
|
|
|
*/ |
34
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
35
|
|
|
{ |
36
|
|
|
$id = $request->getAttribute('id'); |
37
|
|
|
|
38
|
|
|
/** @var AccountingDocument $accountingDocument */ |
39
|
|
|
$accountingDocument = $this->accountingDocumentRepository->findOneById($id); |
|
|
|
|
40
|
|
|
if (!$accountingDocument) { |
41
|
|
|
return $this->createError("AccountingDocument $id not found in database"); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$path = $accountingDocument->getPath(); |
45
|
|
|
if (!is_readable($path)) { |
46
|
|
|
return $this->createError("File for Accounting document $id not found on disk, or not readable"); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$resource = fopen($path, 'r'); |
50
|
|
|
$size = filesize($path); |
51
|
|
|
$type = mime_content_type($path); |
52
|
|
|
$response = new Response($resource, 200, ['content-type' => $type, 'content-length' => $size]); |
|
|
|
|
53
|
|
|
|
54
|
|
|
return $response; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|