Passed
Push — master ( fc4e87...bbd3e2 )
by Blizzz
17:23 queued 18s
created

Attachment::attach()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
7
 *
8
 * @author Joas Schilling <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
namespace OC\Mail;
28
29
use OCP\Mail\IAttachment;
30
use Symfony\Component\Mime\Email;
31
32
/**
33
 * Class Attachment
34
 *
35
 * @package OC\Mail
36
 * @since 13.0.0
37
 */
38
class Attachment implements IAttachment {
39
	private ?string $body;
40
	private ?string $name;
41
	private ?string $contentType;
42
	private ?string $path;
43
44
	public function __construct(
45
		?string $body,
46
		?string $name,
47
		?string $contentType,
48
		?string $path = null
49
	) {
50
		$this->body = $body;
51
		$this->name = $name;
52
		$this->contentType = $contentType;
53
		$this->path = $path;
54
	}
55
56
	/**
57
	 * @param string $filename
58
	 * @return $this
59
	 * @since 13.0.0
60
	 */
61
	public function setFilename(string $filename): IAttachment {
62
		$this->name = $filename;
63
		return $this;
64
	}
65
66
	/**
67
	 * @param string $contentType
68
	 * @return $this
69
	 * @since 13.0.0
70
	 */
71
	public function setContentType(string $contentType): IAttachment {
72
		$this->contentType = $contentType;
73
		return $this;
74
	}
75
76
	/**
77
	 * @param string $body
78
	 * @return $this
79
	 * @since 13.0.0
80
	 */
81
	public function setBody(string $body): IAttachment {
82
		$this->body = $body;
83
		return $this;
84
	}
85
86
	public function attach(Email $symfonyEmail): void {
87
		if ($this->path !== null) {
88
			$symfonyEmail->attachFromPath($this->path, $this->name, $this->contentType);
89
		} else {
90
			$symfonyEmail->attach($this->body, $this->name, $this->contentType);
91
		}
92
	}
93
}
94