Completed
Push — 5.x ( e27a21...0514df )
by Lars
05:02
created

createParameterizedHeader()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4
Metric Value
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 8.9197
cc 4
eloc 12
nc 8
nop 3
crap 4
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
 * Creates MIME headers.
13
 *
14
 * @author Chris Corbyn
15
 */
16
class Swift_Mime_SimpleHeaderFactory implements Swift_Mime_HeaderFactory
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
     * The HeaderEncoder used by these headers
20
     *
21
     * @var Swift_Mime_HeaderEncoder
22
     */
23
    private $_encoder;
24
25
    /**
26
     * The Encoder used by parameters
27
     *
28
     * @var Swift_Encoder
29
     */
30
    private $_paramEncoder;
31
32
    /**
33
     * The Email Validator
34
     *
35
     * @var Swift_EmailValidatorBridge
36
     */
37
    private $_emailValidator;
38
39
    /**
40
     * The charset of created Headers
41
     *
42
     * @var null|string
43
     */
44
    private $_charset;
45
46
    /**
47
     * Creates a new SimpleHeaderFactory using $encoder and $paramEncoder.
48
     *
49
     * @param Swift_Mime_HeaderEncoder   $encoder
50
     * @param Swift_Encoder              $paramEncoder
51
     * @param Swift_EmailValidatorBridge $emailValidator
52
     * @param string|null                $charset
53
     */
54 170
    public function __construct(Swift_Mime_HeaderEncoder $encoder, Swift_Encoder $paramEncoder, Swift_EmailValidatorBridge $emailValidator, $charset = null)
55
    {
56 170
        $this->_encoder = $encoder;
57 170
        $this->_paramEncoder = $paramEncoder;
58 170
        $this->_emailValidator = $emailValidator;
59 170
        $this->_charset = $charset;
60 170
    }
61
62
    /**
63
     * Create a new Mailbox Header with a list of $addresses.
64
     *
65
     * @param string            $name
66
     * @param array|string|null $addresses
67
     *
68
     * @return Swift_Mime_Header
69
     */
70 114
    public function createMailboxHeader($name, $addresses = null)
71
    {
72 114
        $header = new Swift_Mime_Headers_MailboxHeader($name, $this->_encoder, $this->_emailValidator);
73 114
        if (isset($addresses)) {
74 50
            $header->setFieldBodyModel($addresses);
75
        }
76 114
        $this->_setHeaderCharset($header);
77
78 114
        return $header;
79
    }
80
81
    /**
82
     * Create a new Date header using $timestamp (UNIX time).
83
     *
84
     * @param string   $name
85
     * @param int|null $timestamp
86
     *
87
     * @return Swift_Mime_Header
88
     */
89 112
    public function createDateHeader($name, $timestamp = null)
90
    {
91 112
        $header = new Swift_Mime_Headers_DateHeader($name);
92 112
        if (isset($timestamp)) {
93 110
            $header->setFieldBodyModel($timestamp);
94
        }
95 112
        $this->_setHeaderCharset($header);
96
97 112
        return $header;
98
    }
99
100
    /**
101
     * Create a new basic text header with $name and $value.
102
     *
103
     * @param string $name
104
     * @param string $value
105
     *
106
     * @return Swift_Mime_Header
107
     */
108 152
    public function createTextHeader($name, $value = null)
109
    {
110 152
        $header = new Swift_Mime_Headers_UnstructuredHeader($name, $this->_encoder);
111 152
        if (isset($value)) {
112 150
            $header->setFieldBodyModel($value);
113
        }
114 152
        $this->_setHeaderCharset($header);
115
116 152
        return $header;
117
    }
118
119
    /**
120
     * Create a new ParameterizedHeader with $name, $value and $params.
121
     *
122
     * @param string $name
123
     * @param string $value
124
     * @param array  $params
125
     *
126
     * @return Swift_Mime_ParameterizedHeader
127
     */
128 147
    public function createParameterizedHeader($name, $value = null, $params = array())
129
    {
130 147
        if (strtolower($name) == 'content-disposition') {
131 50
            $parameterEncoding = $this->_paramEncoder;
132
        } else {
133 147
            $parameterEncoding = null;
134
        }
135
136 147
        $header = new Swift_Mime_Headers_ParameterizedHeader($name, $this->_encoder, $parameterEncoding);
137
138 147
        if (isset($value)) {
139 145
            $header->setFieldBodyModel($value);
140
        }
141
142 147
        foreach ($params as $k => $v) {
143 1
            $header->setParameter($k, $v);
144
        }
145
146 147
        $this->_setHeaderCharset($header);
147
148 147
        return $header;
149
    }
150
151
    /**
152
     * Create a new ID header for Message-ID or Content-ID.
153
     *
154
     * @param string       $name
155
     * @param string|array $ids
156
     *
157
     * @return Swift_Mime_Header
158
     */
159 124 View Code Duplication
    public function createIdHeader($name, $ids = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161 124
        $header = new Swift_Mime_Headers_IdentificationHeader($name, $this->_emailValidator);
162
163 124
        if (isset($ids)) {
164 122
            $header->setFieldBodyModel($ids);
165
        }
166
167 124
        $this->_setHeaderCharset($header);
168
169 124
        return $header;
170
    }
171
172
    /**
173
     * Create a new Path header with an address (path) in it.
174
     *
175
     * @param string $name
176
     * @param string $path
177
     *
178
     * @return Swift_Mime_Header
179
     */
180 33 View Code Duplication
    public function createPathHeader($name, $path = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
    {
182 33
        $header = new Swift_Mime_Headers_PathHeader($name, $this->_emailValidator);
183 33
        if (isset($path)) {
184 31
            $header->setFieldBodyModel($path);
185
        }
186 33
        $this->_setHeaderCharset($header);
187
188 33
        return $header;
189
    }
190
191
    /**
192
     * Notify this observer that the entity's charset has changed.
193
     *
194
     * @param string $charset
195
     */
196 130
    public function charsetChanged($charset)
197
    {
198 130
        $this->_charset = $charset;
199 130
        $this->_encoder->charsetChanged($charset);
200 130
        $this->_paramEncoder->charsetChanged($charset);
201 130
    }
202
203
    /**
204
     * Make a deep copy of object.
205
     */
206 5
    public function __clone()
207
    {
208 5
        $this->_encoder = clone $this->_encoder;
209 5
        $this->_paramEncoder = clone $this->_paramEncoder;
210 5
    }
211
212
    /**
213
     * Apply the charset to the Header
214
     *
215
     * @param Swift_Mime_Header $header
216
     */
217 169
    private function _setHeaderCharset(Swift_Mime_Header $header)
218
    {
219 169
        if (isset($this->_charset)) {
220 46
            $header->setCharset($this->_charset);
221
        }
222 169
    }
223
}
224