Completed
Push — 5.x ( 801190...049872 )
by Lars
05:29
created

Swift_Mime_Attachment::setDisposition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2004-2009 Chris Corbyn
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
/**
12
 * An attachment, in a multipart message.
13
 *
14
 * @author Chris Corbyn
15
 */
16
class Swift_Mime_Attachment extends Swift_Mime_SimpleMimeEntity
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    /**
19
     * Recognized MIME types
20
     *
21
     * @var array
22
     */
23
    private $_mimeTypes = array();
24
25
    /**
26
     * Create a new Attachment with $headers, $encoder and $cache.
27
     *
28
     * @param Swift_Mime_HeaderSet       $headers
29
     * @param Swift_Mime_ContentEncoder  $encoder
30
     * @param Swift_KeyCache             $cache
31
     * @param Swift_EmailValidatorBridge $emailValidator
32
     * @param array                      $mimeTypes optional
33
     */
34 153
    public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_EmailValidatorBridge $emailValidator, $mimeTypes = array())
35
    {
36 153
        parent::__construct($headers, $encoder, $cache, $emailValidator);
37 129
        $this->setDisposition('attachment');
38 129
        $this->setContentType('application/octet-stream');
39 129
        $this->_mimeTypes = $mimeTypes;
40 129
    }
41
42
    /**
43
     * Get the nesting level used for this attachment.
44
     *
45
     * Always returns {@link LEVEL_MIXED}.
46
     *
47
     * @return int
48
     */
49 1
    public function getNestingLevel()
50
    {
51 1
        return self::LEVEL_MIXED;
52
    }
53
54
    /**
55
     * Get the Content-Disposition of this attachment.
56
     *
57
     * By default attachments have a disposition of "attachment".
58
     *
59
     * @return string
60
     */
61 2
    public function getDisposition()
62
    {
63 2
        return $this->_getHeaderFieldModel('Content-Disposition');
64
    }
65
66
    /**
67
     * Set the Content-Disposition of this attachment.
68
     *
69
     * @param string $disposition
70
     *
71
     * @return Swift_Mime_Attachment
72
     */
73 129
    public function setDisposition($disposition)
74
    {
75 129
        if (!$this->_setHeaderFieldModel('Content-Disposition', $disposition)) {
76 113
            $this->getHeaders()->addParameterizedHeader('Content-Disposition', $disposition);
77
        }
78
79 129
        return $this;
80
    }
81
82
    /**
83
     * Get the filename of this attachment when downloaded.
84
     *
85
     * @return string
86
     */
87 2
    public function getFilename()
88
    {
89 2
        return $this->_getHeaderParameter('Content-Disposition', 'filename');
90
    }
91
92
    /**
93
     * Set the filename of this attachment.
94
     *
95
     * @param string $filename
96
     *
97
     * @return Swift_Mime_Attachment
98
     */
99 12
    public function setFilename($filename)
100
    {
101 12
        $this->_setHeaderParameter('Content-Disposition', 'filename', $filename);
102 12
        $this->_setHeaderParameter('Content-Type', 'name', $filename);
103
104 12
        return $this;
105
    }
106
107
    /**
108
     * Get the file size of this attachment.
109
     *
110
     * @return string
111
     */
112 2
    public function getSize()
113
    {
114 2
        return $this->_getHeaderParameter('Content-Disposition', 'size');
115
    }
116
117
    /**
118
     * Set the file size of this attachment.
119
     *
120
     * @param int $size
121
     *
122
     * @return Swift_Mime_Attachment
123
     */
124 4
    public function setSize($size)
125
    {
126 4
        $this->_setHeaderParameter('Content-Disposition', 'size', $size);
127
128 4
        return $this;
129
    }
130
131
    /**
132
     * Set the file that this attachment is for.
133
     *
134
     * @param Swift_FileStream $file
135
     * @param string           $contentType optional
136
     *
137
     * @return Swift_Mime_Attachment
138
     */
139 8
    public function setFile(Swift_FileStream $file, $contentType = null)
140
    {
141 8
        $this->setFilename(basename($file->getPath()));
142 8
        $this->setBody($file, $contentType);
143 8
        if (!isset($contentType)) {
144 6
            $extension = Swift::strtolowerWithStaticCache(
145 6
                substr($file->getPath(), strrpos($file->getPath(), '.') + 1)
146
            );
147
148 6
            if (isset($this->_mimeTypes[$extension])) {
149
                $this->setContentType($this->_mimeTypes[$extension]);
150
            }
151
        }
152
153 8
        return $this;
154
    }
155
}
156