Failed Conditions
Push — master ( b41068...5651e0 )
by
unknown
06:58
created

AccountingDocumentAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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);
1 ignored issue
show
Bug introduced by
The method findOneById() does not exist on Application\Repository\A...ntingDocumentRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        /** @scrutinizer ignore-call */ 
40
        $accountingDocument = $this->accountingDocumentRepository->findOneById($id);
Loading history...
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]);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $body of Zend\Diactoros\Response::__construct() does only seem to accept Psr\Http\Message\StreamInterface|resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        $response = new Response(/** @scrutinizer ignore-type */ $resource, 200, ['content-type' => $type, 'content-length' => $size]);
Loading history...
53
54
        return $response;
55
    }
56
}
57