| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sentry\Attachment; |
| 5 |
|
| 6 |
abstract class Attachment |
| 7 |
{ |
| 8 |
private const DEFAULT_CONTENT_TYPE = 'application/octet-stream'; |
| 9 |
/** |
| 10 |
* @var string |
| 11 |
*/ |
| 12 |
private $filename; |
| 13 |
/** |
| 14 |
* @var string |
| 15 |
*/ |
| 16 |
private $contentType; |
| 17 |
public function __construct(string $filename, string $contentType) |
| 18 |
{ |
| 19 |
$this->filename = $filename; |
| 20 |
$this->contentType = $contentType; |
| 21 |
} |
| 22 |
public function getFilename() : string |
| 23 |
{ |
| 24 |
return $this->filename; |
| 25 |
} |
| 26 |
public function getContentType() : string |
| 27 |
{ |
| 28 |
return $this->contentType; |
| 29 |
} |
| 30 |
/** |
| 31 |
* Returns the size in bytes for the attachment. This method should aim to use a low overhead |
| 32 |
* way of determining the size because it will be called more than once. |
| 33 |
* For example, for file attachments it should read the file size from the filesystem instead of |
| 34 |
* reading the file in memory and then calculating the length. |
| 35 |
* If no low overhead way exists, then the result should be cached so that calling it multiple times |
| 36 |
* does not decrease performance. |
| 37 |
* |
| 38 |
* @return int the size in bytes or null if the length could not be determined, for example if the file |
| 39 |
* does not exist |
| 40 |
*/ |
| 41 |
public abstract function getSize() : ?int; |
| 42 |
/** |
| 43 |
* Fetches and returns the data. Calling this can have a non-trivial impact on memory usage, depending |
| 44 |
* on the type and size of attachment. |
| 45 |
* |
| 46 |
* @return string the content as bytes or null if the content could not be retrieved, for example if the file |
| 47 |
* does not exist |
| 48 |
*/ |
| 49 |
public abstract function getData() : ?string; |
| 50 |
/** |
| 51 |
* Creates a new attachment representing a file referenced by a path. |
| 52 |
* The file is not validated and the content is not read when creating the attachment. |
| 53 |
*/ |
| 54 |
public static function fromFile(string $path, string $contentType = self::DEFAULT_CONTENT_TYPE) : Attachment |
| 55 |
{ |
| 56 |
return new FileAttachment($path, $contentType); |
| 57 |
} |
| 58 |
/** |
| 59 |
* Creates a new attachment representing a slice of bytes that lives in memory. |
| 60 |
*/ |
| 61 |
public static function fromBytes(string $filename, string $data, string $contentType = self::DEFAULT_CONTENT_TYPE) : Attachment |
| 62 |
{ |
| 63 |
return new ByteAttachment($filename, $contentType, $data); |
| 64 |
} |
| 65 |
} |
| 66 |
|