Passed
Push — master ( 5264e0...a3503b )
by Radu
07:32
created

AbstractFile   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
eloc 34
c 2
b 0
f 1
dl 0
loc 69
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getOutputResponse() 0 12 1
A getFileName() 0 3 1
A getFileData() 0 3 1
A getContentType() 0 3 1
A __construct() 0 5 1
A getDownloadResponse() 0 15 1
A setFileName() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Files;
6
7
use WebServCo\Framework\Http\Response;
8
9
abstract class AbstractFile
10
{
11
    public const CONTENT_TYPE = 'application/octet-stream';
12
13
    protected string $fileName;
14
    protected string $fileData;
15
    protected string $contentType;
16
17
    public function __construct(string $fileName, string $fileData, string $contentType = self::CONTENT_TYPE)
18
    {
19
        $this->fileName = $fileName;
20
        $this->fileData = $fileData;
21
        $this->contentType = $contentType;
22
    }
23
24
    public function getContentType(): string
25
    {
26
        return $this->contentType;
27
    }
28
29
    public function getDownloadResponse(): Response
30
    {
31
        return new Response(
32
            $this->fileData,
33
            200,
34
            [
35
                'Last-Modified' => [\gmdate('D, d M Y H:i:s') . ' GMT'],
36
                'ETag' => [\md5($this->fileData)],
37
                'Accept-Ranges' => ['bytes'],
38
                'Cache-Control' => ['public'],
39
                'Content-Description' => ['File Transfer'],
40
                'Content-Disposition' => [\sprintf('attachment; filename="%s"', $this->fileName)],
41
                'Content-Type' => [$this->contentType],
42
                'Content-Transfer-Encoding' => ['binary'],
43
                'Connection' => ['close'],
44
            ],
45
        );
46
    }
47
48
    public function getFileData(): string
49
    {
50
        return $this->fileData;
51
    }
52
53
    public function getFileName(): string
54
    {
55
        return $this->fileName;
56
    }
57
58
    public function getOutputResponse(): Response
59
    {
60
        return new Response(
61
            $this->fileData,
62
            200,
63
            [
64
                'Last-Modified' => [\gmdate('D, d M Y H:i:s') . ' GMT'],
65
                'ETag' => [\md5($this->fileData)],
66
                'Accept-Ranges' => ['bytes'],
67
                'Cache-Control' => ['public'],
68
                'Content-Type' => [$this->contentType],
69
                'Content-Transfer-Encoding' => ['binary'],
70
            ],
71
        );
72
    }
73
74
    public function setFileName(string $fileName): bool
75
    {
76
        $this->fileName = $fileName;
77
        return true;
78
    }
79
}
80