• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / utils / 20230537565

15 Dec 2025 11:26AM UTC coverage: 87.561%. First build
20230537565

Pull #39

github

web-flow
Merge cc12366b1 into 1c9919282
Pull Request #39: Add File object

48 of 54 new or added lines in 5 files covered. (88.89%)

359 of 410 relevant lines covered (87.56%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.43
/src/Http/FileResponse.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Utils\Http;
4

5
use Contributte\Utils\Exception\Runtime\FileNotFoundException;
6

7
class FileResponse
8
{
9

10
        private string $file;
11

12
        private string $name;
13

14
        private string|null $contentType;
15

16
        private bool $forceDownload;
17

18
        /**
19
         * @throws FileNotFoundException
20
         */
21
        public function __construct(
1✔
22
                string $file,
23
                string|null $name = null,
24
                string|null $contentType = null,
25
                bool $forceDownload = true
26
        )
27
        {
28
                if (!file_exists($file) || !is_file($file)) {
1✔
29
                        throw new FileNotFoundException($file);
1✔
30
                }
31

32
                $this->file = $file;
1✔
33
                $this->name = $name ?? basename($file);
1✔
34
                $this->contentType = $contentType;
1✔
35
                $this->forceDownload = $forceDownload;
1✔
36
        }
1✔
37

38
        public function getFile(): string
39
        {
40
                return $this->file;
1✔
41
        }
42

43
        public function getName(): string
44
        {
45
                return $this->name;
1✔
46
        }
47

48
        public function getContentType(): string
49
        {
50
                if ($this->contentType !== null) {
1✔
51
                        return $this->contentType;
1✔
52
                }
53

54
                $mimeType = mime_content_type($this->file);
1✔
55

56
                return $mimeType !== false ? $mimeType : 'application/octet-stream';
1✔
57
        }
58

59
        public function isForceDownload(): bool
60
        {
61
                return $this->forceDownload;
1✔
62
        }
63

64
        public function send(): void
65
        {
NEW
66
                $name = $this->name;
×
NEW
67
                $contentType = $this->getContentType();
×
68

NEW
69
                header('Content-Type: ' . $contentType);
×
NEW
70
                header('Content-Disposition: ' . ($this->forceDownload ? 'attachment' : 'inline') . '; filename="' . $name . '"; filename*=utf-8\'\'' . rawurlencode($name));
×
NEW
71
                header('Content-Length: ' . filesize($this->file));
×
72

NEW
73
                readfile($this->file);
×
74
        }
75

76
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc