Passed
Push — master ( a046ac...48018f )
by Aleksei
01:52
created

FileBucket::__construct()   A

Complexity

Conditions 6
Paths 13

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 6
eloc 12
c 1
b 1
f 1
nc 13
nop 3
dl 0
loc 17
rs 9.2222
1
<?php
2
3
namespace roxblnfk\SmartStream\Data;
4
5
use SplFileInfo;
6
7
class FileBucket extends DataBucket
8
{
9
    public const DISPOSITION_INLINE = 'inline';
10
    public const DISPOSITION_ATTACHMENT = 'attachment';
11
12
    public const TYPE_OCTET_STREAM = 'application/octet-stream';
13
14
    protected const IS_FORMATTABLE = false;
15
    protected ?string $contentType = null;
16
    protected ?string $contentDisposition = null;
17
    protected ?string $fileName = null;
18
19
    public static function createFromFile(string $filePath): self
20
    {
21
        $finfo = new SplFileInfo($filePath);
22
        $type = (function_exists('mime_content_type') && mime_content_type($filePath)) ?: null;
23
        return new static($finfo, $type, $finfo->getFilename());
24
    }
25
26
    /**
27
     * FileBucket constructor.
28
     * @param string|resource|SplFileInfo $data
29
     * @param null|string $contentType
30
     * @param null|string $filename
31
     * @throws \Exception
32
     */
33
    public function __construct($data, string $contentType = null, string $filename = null)
34
    {
35
        switch (true) {
36
            case is_string($data):
37
            case is_resource($data):
38
            case $data instanceof SplFileInfo:
39
                parent::__construct($data);
40
                break;
41
            default:
42
                throw new \Exception('The $data parameter must be a resource, a string or an instance of SplFileInfo');
43
        }
44
45
        if ($contentType !== null) {
46
            $this->setContentType($contentType);
47
        }
48
        if ($filename !== null) {
49
            $this->setAttachment($filename);
50
        }
51
    }
52
53
    public function getContentType(): ?string
54
    {
55
        return $this->contentType;
56
    }
57
    public function setContentType(?string $contentType): self
58
    {
59
        $this->contentType = $contentType;
60
        $this->setHeader('Content-Type', $contentType);
0 ignored issues
show
Bug introduced by
It seems like $contentType can also be of type null; however, parameter $value of roxblnfk\SmartStream\Data\DataBucket::setHeader() does only seem to accept 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

60
        $this->setHeader('Content-Type', /** @scrutinizer ignore-type */ $contentType);
Loading history...
61
        return $this;
62
    }
63
    public function getFileName(): ?string
64
    {
65
        return $this->fileName;
66
    }
67
68
    public function getContentDisposition(): ?string
69
    {
70
        return $this->contentDisposition;
71
    }
72
    public function setInline(): void
73
    {
74
        $this->contentDisposition = self::DISPOSITION_INLINE;
75
        $this->setDispositionHeader();
76
    }
77
    public function setAttachment(?string $filename): void
78
    {
79
        $this->contentDisposition = self::DISPOSITION_ATTACHMENT;
80
        $this->fileName = $filename;
81
        $this->setDispositionHeader();
82
    }
83
    private function setDispositionHeader(): void
84
    {
85
        if ($this->contentDisposition === null) {
86
            return;
87
        }
88
        $headerBody = ($this->contentDisposition === self::DISPOSITION_ATTACHMENT && $this->fileName !== null)
89
            ? sprintf(
90
                '%s; filename="%s"; filename*=UTF-8\'\'%s',
91
                $this->contentDisposition,
92
                preg_replace('/[\x00-\x1F\x7F\"]/', ' ', $this->fileName),
93
                rawurlencode($this->fileName)
94
            )
95
            : $this->contentDisposition;
96
        $this->setHeader('Content-Disposition', $headerBody);
97
    }
98
}
99