| Total Complexity | 6 |
| Total Lines | 68 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 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 |
||
| 68 | 9 | } |
|
| 69 | |||
| 70 | 9 | public function getMIMEType(): string |
|
| 73 | } |
||
| 74 | } |
||
| 75 |