Attachment::getFilename()   A
last analyzed

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 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kodus\Mail;
4
5
class Attachment
6
{
7
    /**
8
     * Default MIME-type for Attachments
9
     */
10
    const DEFAULT_MIME_TYPE = "application/octet-stream";
11
12
    /**
13
     * @var string|resource
14
     */
15
    private $content;
16
17
    /**
18
     * @var string
19
     */
20
    private $filename;
21
22
    /**
23
     * @var string
24
     */
25
    private $mime_type;
26
27
    /**
28
     * @param string|resource $content   attachment content (or stream handle)
29
     * @param string          $filename  logical filename
30
     * @param string          $mime_type MIME-type
31
     */
32 9
    public function __construct($content, string $filename, string $mime_type = self::DEFAULT_MIME_TYPE)
33
    {
34 9
        $this->content = $content;
35 9
        $this->filename = $filename;
36 9
        $this->mime_type = $mime_type;
37 9
    }
38
39
    /**
40
     * Create an attachment from a physical file (or any other supported stream protocol)
41
     *
42
     * @param string      $path      absolute path to physical file (or any other supported stream protocol)
43
     * @param string|null $filename  optional logical filename (defaults to base filename of the specified physical file)
44
     * @param string      $mime_type MIME-type
45
     *
46
     * @return self
47
     */
48 7
    public static function fromFile(string $path, ?string $filename = null, string $mime_type = self::DEFAULT_MIME_TYPE): self
49
    {
50 7
        return new self(
51 7
            fopen($path, "r"),
52 7
            $filename ?: basename($path),
53
            $mime_type
54 7
        );
55
    }
56
57
    /**
58
     * @return string|resource
59
     */
60 9
    public function getContent()
61
    {
62 9
        return $this->content;
63
    }
64
65
    public function getFilename(): string
66
    {
67
        return $this->filename;
68 9
    }
69
70 9
    public function getMIMEType(): string
71
    {
72
        return $this->mime_type;
73
    }
74
}
75