Passed
Push — master ( 7ce657...eef289 )
by Jeroen
28:05 queued 06:52
created

Attachment::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Elgg\Email;
4
5
use Elgg\Filesystem\MimeTypeDetector;
6
use Zend\Mime\Part;
7
8
/**
9
 * Email attachment
10
 */
11
class Attachment extends Part {
12
	
13
	/**
14
	 * create a new Mime Part.
15
	 * The (unencoded) content of the Part as passed
16
	 * as a string or stream
17
	 *
18
	 * @param mixed $content String or Stream containing the content
19
	 *
20
	 * @throws \Zend\Mime\Exception\InvalidArgumentException
21
	 *
22
	 * @see Part::__construct()
23
	 */
24 8
	public function __construct($content = '') {
25 8
		parent::__construct($content);
26
		
27 8
		$this->disposition = 'attachment';
28 8
	}
29
	
30
	/**
31
	 * Create an attachment
32
	 *
33
	 * @param mixed $options an array or an ElggFile, supported array keys are:
34
	 * 		content:     (string) the file contents of the attachment
35
	 *		filepath:    (string) if content isn't provided, a filepath can be given to fetch the content from
36
	 *		filename:    (string) the name of the attachment
37
	 *		type:        (string) the mimetype
38
	 *		encoding:    (string) the content encoding
39
	 *		disposition: (string) the attachment disposition (default: attachment)
40
	 *		charset:     (string) the charset
41
	 *
42
	 * @see \Zend\Mime\Part
43
	 *
44
	 * @return false|\Elgg\Email\Attachment return the attachment or false on error
45
	 */
46 10
	public static function factory($options) {
47
		
48 10
		if ($options instanceof \ElggFile) {
49 1
			return self::fromElggFile($options);
50
		}
51
		
52 10
		if (!is_array($options)) {
53
			elgg_log(__METHOD__ . ': $options needs to be an array', 'ERROR');
54
			return false;
55
		}
56
		
57 10 View Code Duplication
		if (!isset($options['content']) && !isset($options['filepath'])) {
58 2
			elgg_log(__METHOD__ . ': $options "content" or "filepath" is required', 'ERROR');
59 2
			return false;
60
		}
61
		
62 8
		$content = elgg_extract('content', $options);
63 8
		unset($options['content']);
64 8
		if (!isset($content)) {
65
			$filepath = elgg_extract('filepath', $options);
66
			if (empty($filepath) || !is_file($filepath)) {
67
				elgg_log(__METHOD__ . ': $options[filepath] didn\'t result in a valid file', 'ERROR');
68
				return false;
69
			}
70
			
71
			$content = file_get_contents($filepath);
72
			
73
			if (!isset($options['filename'])) {
74
				$options['filename'] = basename($filepath);
75
			}
76
			
77
			if (!isset($options['type'])) {
78
				$options['type'] = (new MimeTypeDetector())->tryStrategies($filepath);
79
			}
80
		}
81
		
82 8
		unset($options['filepath']);
83
		
84 8
		$attachment = new self($content);
85
		
86 8
		foreach ($options as $key => $value) {
87 8
			$attachment->$key = $value;
88
		}
89
		
90 8
		return $attachment;
91
	}
92
	
93
	/**
94
	 * Create an attachment from an ElggFile
95
	 *
96
	 * @param \ElggFile $file the file
97
	 *
98
	 * @return false|\Elgg\Email\Attachment
99
	 */
100 6
	public static function fromElggFile(\ElggFile $file) {
101
		
102 6
		if (!$file instanceof \ElggFile || !$file->exists()) {
103 1
			return false;
104
		}
105
		
106
		$options = [
107 5
			'content' => $file->grabFile(),
108 5
			'type' => $file->getMimeType(),
109 5
			'filename' => basename($file->getFilename()),
110
		];
111
		
112 5
		return self::factory($options);
113
	}
114
}
115