Attachment   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 68
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilename() 0 3 1
A __construct() 0 5 1
A fromFile() 0 6 2
A getMIMEType() 0 3 1
A getContent() 0 3 1
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