Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
31 / 31 |
Attachment | |
100.00% |
1 / 1 |
|
100.00% |
7 / 7 |
18 | |
100.00% |
31 / 31 |
__construct | |
100.00% |
1 / 1 |
12 | |
100.00% |
20 / 20 |
|||
getFullPath | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getFilename | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getContentType | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getDisposition | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getAttachmentData | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
__set_state | |
100.00% |
1 / 1 |
1 | |
100.00% |
6 / 6 |
1 | <?php |
2 | |
3 | namespace Miniframe\Mailer\Model; |
4 | |
5 | class Attachment |
6 | { |
7 | /** |
8 | * Disposition values |
9 | */ |
10 | public const |
11 | DISPOSITION_INLINE = 1, |
12 | DISPOSITION_ATTACHMENT = 2; |
13 | |
14 | /** |
15 | * Full path to the file. |
16 | * |
17 | * @var string|null |
18 | */ |
19 | private $fullPath; |
20 | |
21 | /** |
22 | * Name for within the attachment |
23 | * |
24 | * @var string |
25 | */ |
26 | private $filename; |
27 | |
28 | /** |
29 | * Type of the file. |
30 | * |
31 | * @var string|null |
32 | */ |
33 | private $contentType = null; |
34 | /** |
35 | * Disposition for the attachment. |
36 | * |
37 | * @var int |
38 | */ |
39 | private $disposition; |
40 | |
41 | /** |
42 | * Attachment data. |
43 | * |
44 | * @var string|null |
45 | */ |
46 | private $attachmentData; |
47 | |
48 | /** |
49 | * Attachment data model |
50 | * |
51 | * @param string|null $fullPath Path to the source file. When null, attachmentData and filename must be set. |
52 | * @param string|null $filename Name for within the attachment. |
53 | * @param string|null $contentType Type of the file. |
54 | * @param integer $disposition One of the DISPOSITION_INLINE or DISPOSITION_ATTACHMENT constants. |
55 | * @param string|null $attachmentData Instead of a fullPath, you can also add the data with this parameter. |
56 | */ |
57 | public function __construct( |
58 | string $fullPath = null, |
59 | string $filename = null, |
60 | string $contentType = null, |
61 | int $disposition = self::DISPOSITION_ATTACHMENT, |
62 | string $attachmentData = null |
63 | ) { |
64 | if ($fullPath !== null && !file_exists($fullPath)) { |
65 | throw new \RuntimeException('File does not exist: ' . $fullPath); |
66 | } |
67 | if ($filename === null) { |
68 | $filename = pathinfo($fullPath, PATHINFO_BASENAME); |
69 | } |
70 | if (!in_array($disposition, [self::DISPOSITION_INLINE, self::DISPOSITION_ATTACHMENT])) { |
71 | throw new \InvalidArgumentException( |
72 | 'Disposition must be one of DISPOSITION_INLINE or DISPOSITION_ATTACHMENT' |
73 | ); |
74 | } |
75 | |
76 | if ($fullPath === null && (!$filename || !$attachmentData)) { |
77 | throw new \InvalidArgumentException('When no fullPath is given, filename and attachmentData are required.'); |
78 | } |
79 | if ($fullPath !== null && $attachmentData) { |
80 | throw new \InvalidArgumentException('When attachmentData is specified, filename must be null.'); |
81 | } |
82 | |
83 | $this->fullPath = $fullPath; |
84 | $this->filename = $filename; |
85 | $this->disposition = $disposition; |
86 | $this->attachmentData = $attachmentData; |
87 | if ($contentType) { |
88 | $this->contentType = $contentType; |
89 | } elseif (function_exists('mime_content_type')) { // ext-fileinfo |
90 | $this->contentType = mime_content_type($fullPath) ?? null; |
91 | } |
92 | } |
93 | |
94 | /** |
95 | * Returns the full path to the source file. |
96 | * |
97 | * @return string|null |
98 | */ |
99 | public function getFullPath(): ?string |
100 | { |
101 | return $this->fullPath; |
102 | } |
103 | |
104 | /** |
105 | * Returns the name of the attachment. |
106 | * |
107 | * @return string |
108 | */ |
109 | public function getFilename(): string |
110 | { |
111 | return $this->filename; |
112 | } |
113 | |
114 | /** |
115 | * Returns the type of the file. |
116 | * |
117 | * @return string|null |
118 | */ |
119 | public function getContentType(): ?string |
120 | { |
121 | return $this->contentType; |
122 | } |
123 | |
124 | /** |
125 | * Returns the disposition for the attachment. |
126 | * |
127 | * @return integer |
128 | */ |
129 | public function getDisposition(): int |
130 | { |
131 | return $this->disposition; |
132 | } |
133 | |
134 | /** |
135 | * Returns the attachment data. |
136 | * |
137 | * @return string|null |
138 | */ |
139 | public function getAttachmentData(): ?string |
140 | { |
141 | return $this->attachmentData; |
142 | } |
143 | |
144 | /** |
145 | * Magic method; sets this model based on a specific state |
146 | * |
147 | * @param array $properties The actual state. |
148 | * |
149 | * @return Attachment |
150 | * @see var_export() |
151 | */ |
152 | public static function __set_state(array $properties): self |
153 | { |
154 | return new self( |
155 | $properties['fullPath'], |
156 | $properties['filename'], |
157 | $properties['contentType'], |
158 | $properties['disposition'], |
159 | $properties['attachmentData'] |
160 | ); |
161 | } |
162 | } |