AttachmentResponse::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 16
ccs 0
cts 4
cp 0
crap 2
rs 9.9666
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Response;
13
14
use Laminas\Diactoros\Response as DiactorsResponse;
0 ignored issues
show
Bug introduced by
The type Laminas\Diactoros\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Laminas\Diactoros\Response\InjectContentTypeTrait;
0 ignored issues
show
Bug introduced by
The type Laminas\Diactoros\Response\InjectContentTypeTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Laminas\Diactoros\Stream;
0 ignored issues
show
Bug introduced by
The type Laminas\Diactoros\Stream was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
/**
19
 * Class AttachmentResponse
20
 *
21
 * @package Bluz\Response
22
 * @link    http://www.marco-bunge.com/2016/09/01/file-downloads-with-Laminas-diactoros/
23
 */
24
class AttachmentResponse extends DiactorsResponse
25
{
26
    use InjectContentTypeTrait;
27
28
    /**
29
     * Create a file attachment response.
30
     *
31
     * Produces a text response with a Content-Type of given file mime type and a default
32
     * status of 200.
33
     *
34
     * @param string $file    Valid file path
35
     * @param int $status  Integer status code for the response; 200 by default.
36
     * @param array  $headers Array of headers to use at initialization.
37
     *
38
     * @throws \InvalidArgumentException
39
     *@internal param StreamInterface|string $text String or stream for the message body.
40
     */
41
    public function __construct($file, int $status = 200, array $headers = [])
42
    {
43
        $fileInfo = new \SplFileInfo($file);
44
45
        $headers = array_replace(
46
            $headers,
47
            [
48
                'content-length' => $fileInfo->getSize(),
49
                'content-disposition' => sprintf('attachment; filename=%s', $fileInfo->getFilename()),
50
            ]
51
        );
52
53
        parent::__construct(
54
            new Stream($fileInfo->getRealPath(), 'r'),
55
            $status,
56
            $this->injectContentType((new \finfo(FILEINFO_MIME_TYPE))->file($fileInfo->getRealPath()), $headers)
57
        );
58
    }
59
}
60