1 <?php
2 /* --------------------------------------------------------------
3 EmailAttachment.inc.php 2015-01-29 gm
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2015 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('EmailAttachmentInterface');
13
14 /**
15 * Class EmailAttachment
16 *
17 * Email attachment will serve as the path to the file that needs to be attached
18 * (located on the server).
19 *
20 * @category System
21 * @package Email
22 * @subpackage Entities
23 */
24 class EmailAttachment implements EmailAttachmentInterface
25 {
26 /**
27 * E-Mail attachment path.
28 * @var string
29 */
30 protected $path;
31
32 /**
33 * E-Mail attachment name.
34 * @var string
35 */
36 protected $name;
37
38
39 /**
40 * Constructor
41 *
42 * Entity representing an email attachment.
43 *
44 * @param AttachmentPathInterface $path (optional) Attachment path.
45 * @param AttachmentNameInterface $name (optional) Attachment name.
46 */
47 public function __construct(AttachmentPathInterface $path = null, AttachmentNameInterface $name = null)
48 {
49 $this->path = $this->_convertToRelativePath($path);
50 $this->name = (string)$name;
51 }
52
53
54 /**
55 * Sets the path of an email attachment.
56 *
57 * @param AttachmentPathInterface $path Attachment path.
58 */
59 public function setPath(AttachmentPathInterface $path)
60 {
61 $this->path = $this->_convertToRelativePath($path);
62 }
63
64
65 /**
66 * Returns the path of an email attachment.
67 *
68 * @param bool $absolutePath (optional) Whether to return the absolute path or the relative one.
69 *
70 * @return AttachmentPathInterface Attachment path.
71 */
72 public function getPath($absolutePath = true)
73 {
74 $path = ($absolutePath) ? DIR_FS_CATALOG . $this->path : $this->path;
75
76 return ($path !== DIR_FS_CATALOG) ? MainFactory::create('AttachmentPath', $path) : null;
77 }
78
79
80 /**
81 * Sets the name of an email attachment.
82 *
83 * @param AttachmentNameInterface $name Attachment name.
84 */
85 public function setName(AttachmentNameInterface $name)
86 {
87 $this->name = (string)$name;
88 }
89
90
91 /**
92 * Returns the name of an email attachment.
93 *
94 * @return AttachmentNameInterface Attachment name.
95 */
96 public function getName()
97 {
98 return MainFactory::create('AttachmentName', $this->name);
99 }
100
101
102 /**
103 * Convert a path to relative.
104 *
105 * Due to different server setups this process can be tedious and hard to foresee. The
106 * following method contains the conversion logic and must be used in any setter of the class.
107 *
108 * @param AttachmentPathInterface $path Attachment path.
109 *
110 * @return string Returns the converted path.
111 */
112 protected function _convertToRelativePath(AttachmentPathInterface $path = null)
113 {
114 if(DIR_FS_CATALOG === '/' && substr((string)$path, 0, 1) === '/')
115 {
116 // Remove the initial slash.
117 $relativePath = substr((string)$path, 1);
118 }
119 else if(DIR_FS_CATALOG !== '/')
120 {
121 // Remove the entire DIR_FS_CATALOG from the path.
122 $relativePath = str_replace(DIR_FS_CATALOG, '', (string)$path);
123 }
124 else
125 {
126 // Path is already relative.
127 $relativePath = (string)$path;
128 }
129
130 return $relativePath;
131 }
132 }