Completed
Push — 5.x ( e60e0a...9fe877 )
by Lars
05:10
created

Swift_Mime_Attachment   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
dl 0
loc 157
ccs 38
cts 38
cp 1
rs 10
c 3
b 1
f 1
wmc 14
lcom 1
cbo 4

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getNestingLevel() 0 4 1
A getDisposition() 0 4 1
A getFilename() 0 4 1
A setFilename() 0 7 1
A getSize() 0 4 1
A setSize() 0 6 1
A setDisposition() 0 8 2
A setFile() 0 15 3
A getFileExtension() 0 10 2
A __construct() 0 7 1
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 179
    public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_EmailValidatorBridge $emailValidator, $mimeTypes = array())
35
    {
36 179
        parent::__construct($headers, $encoder, $cache, $emailValidator);
37 179
        $this->setDisposition('attachment');
38 179
        $this->setContentType('application/octet-stream');
39 179
        $this->_mimeTypes = $mimeTypes;
40 179
    }
41
42
    /**
43
     * Get the nesting level used for this attachment.
44
     *
45
     * Always returns {@link LEVEL_MIXED}.
46
     *
47
     * @return int
48
     */
49 21
    public function getNestingLevel()
50
    {
51 21
        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 179
    public function setDisposition($disposition)
74
    {
75 179
        if (!$this->_setHeaderFieldModel('Content-Disposition', $disposition)) {
76 163
            $this->getHeaders()->addParameterizedHeader('Content-Disposition', $disposition);
77
        }
78
79 179
        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 55
    public function setFilename($filename)
100
    {
101 55
        $this->_setHeaderParameter('Content-Disposition', 'filename', $filename);
102 55
        $this->_setHeaderParameter('Content-Type', 'name', $filename);
103
104 55
        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 16
    public function setSize($size)
125
    {
126 16
        $this->_setHeaderParameter('Content-Disposition', 'size', $size);
127
128 16
        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 18
    public function setFile(Swift_FileStream $file, $contentType = null)
140
    {
141 18
        $this->setFilename(basename(parse_url($file->getPath(), PHP_URL_PATH)));
142 18
        $this->setBody($file, $contentType);
143 18
        if (!isset($contentType)) {
144
145 16
            $extension = $this->getFileExtension($file->getPath());
146
147 16
            if (isset($this->_mimeTypes[$extension])) {
148 10
                $this->setContentType($this->_mimeTypes[$extension]);
149
            }
150
        }
151
152 18
        return $this;
153
    }
154
155
    /**
156
     * get the file-extension e.g. from "http://foo.bar/image.jpg?md5=123456"
157
     *
158
     * @param $str
159
     *
160
     * @return string
161
     */
162 16
    private function getFileExtension($str)
163
    {
164 16
        $extension = substr($str, strrpos($str, '.') + 1);
165 16
        if (strpos($extension, '?') !== false) {
166
            $extension = preg_replace("/(\?.*)/", '', $extension);
167
        }
168 16
        $extension = Swift::strtolowerWithStaticCache($extension);
169
170 16
        return (string)$extension;
171
    }
172
}
173