Completed
Push — 5.x ( 546220...bd8586 )
by Lars
16:02
created

createParameterizedHeader()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 4
eloc 11
c 2
b 1
f 0
nc 8
nop 3
dl 0
loc 21
ccs 7
cts 7
cp 1
crap 4
rs 9.0534
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
60 170
        $this->charsetChanged($charset);
61
    }
62
63
    /**
64
     * Create a new Mailbox Header with a list of $addresses.
65
     *
66
     * @param string            $name
67
     * @param array|string|null $addresses
68
     *
69
     * @return Swift_Mime_Header
70 114
     */
71 View Code Duplication
    public function createMailboxHeader($name, $addresses = 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...
72 114
    {
73 114
        $header = new Swift_Mime_Headers_MailboxHeader($name, $this->_encoder, $this->_emailValidator);
74 50
        
75
        if ($addresses) {
76 114
            $header->setFieldBodyModel($addresses);
77
        }
78 114
        
79
        $this->_setHeaderCharset($header);
80
81
        return $header;
82
    }
83
84
    /**
85
     * Create a new Date header using $timestamp (UNIX time).
86
     *
87
     * @param string   $name
88
     * @param int|null $timestamp
89 112
     *
90
     * @return Swift_Mime_Header
91 112
     */
92 112
    public function createDateHeader($name, $timestamp = null)
93 110
    {
94
        $header = new Swift_Mime_Headers_DateHeader($name);
95 112
        
96
        if ($timestamp) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $timestamp of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
97 112
            $header->setFieldBodyModel($timestamp);
98
        }
99
        
100
        $this->_setHeaderCharset($header);
101
102
        return $header;
103
    }
104
105
    /**
106
     * Create a new basic text header with $name and $value.
107
     *
108 152
     * @param string $name
109
     * @param string $value
110 152
     *
111 152
     * @return Swift_Mime_Header
112 150
     */
113
    public function createTextHeader($name, $value = null)
114 152
    {
115
        $header = new Swift_Mime_Headers_UnstructuredHeader($name, $this->_encoder);
116 152
        
117
        if ($value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
118
            $header->setFieldBodyModel($value);
119
        }
120
        
121
        $this->_setHeaderCharset($header);
122
123
        return $header;
124
    }
125
126
    /**
127
     * Create a new ParameterizedHeader with $name, $value and $params.
128 147
     *
129
     * @param string $name
130 147
     * @param string $value
131 50
     * @param array  $params
132
     *
133 147
     * @return Swift_Mime_ParameterizedHeader
134
     */
135
    public function createParameterizedHeader($name, $value = null, $params = array())
136 147
    {
137
        $parameterEncoding = null;
138 147
        if (strtolower($name) === 'content-disposition') {
139 145
            $parameterEncoding = $this->_paramEncoder;
140
        }
141
142 147
        $header = new Swift_Mime_Headers_ParameterizedHeader($name, $this->_encoder, $parameterEncoding);
143 1
144
        if ($value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
145
            $header->setFieldBodyModel($value);
146 147
        }
147
148 147
        foreach ($params as $k => $v) {
149
            $header->setParameter($k, $v);
150
        }
151
152
        $this->_setHeaderCharset($header);
153
154
        return $header;
155
    }
156
157
    /**
158
     * Create a new ID header for Message-ID or Content-ID.
159 124
     *
160
     * @param string            $name
161 124
     * @param string|array|null $ids
162
     *
163 124
     * @return Swift_Mime_Header
164 122
     */
165 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...
166
    {
167 124
        $header = new Swift_Mime_Headers_IdentificationHeader($name, $this->_emailValidator);
168
169 124
        if (!empty($ids)) {
170
            $header->setFieldBodyModel($ids);
171
        }
172
173
        $this->_setHeaderCharset($header);
174
175
        return $header;
176
    }
177
178
    /**
179
     * Create a new Path header with an address (path) in it.
180 33
     *
181
     * @param string $name
182 33
     * @param string $path
183 33
     *
184 31
     * @return Swift_Mime_Header
185
     */
186 33
    public function createPathHeader($name, $path = null)
187
    {
188 33
        $header = new Swift_Mime_Headers_PathHeader($name, $this->_emailValidator);
189
        
190
        if ($path) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $path of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
191
            $header->setFieldBodyModel($path);
192
        }
193
        
194
        $this->_setHeaderCharset($header);
195
196 130
        return $header;
197
    }
198 130
199 130
    /**
200 130
     * Notify this observer that the entity's charset has changed.
201 130
     *
202
     * @param string $charset
203
     * 
204
     * @return bool
205
     */
206 5
    public function charsetChanged($charset)
207
    {
208 5
        if ($charset) {
209 5
            $this->_charset = $charset;
210 5
            $this->_encoder->charsetChanged($charset);
211
            $this->_paramEncoder->charsetChanged($charset);
212
            
213
            return true;
214
        }
215
        
216
        return false;
217 169
    }
218
219 169
    /**
220 46
     * Make a deep copy of object.
221
     */
222 169
    public function __clone()
223
    {
224
        $this->_encoder = clone $this->_encoder;
225
        $this->_paramEncoder = clone $this->_paramEncoder;
226
    }
227
228
    /**
229
     * Apply the charset to the Header
230
     *
231
     * @param Swift_Mime_Header $header
232
     */
233
    private function _setHeaderCharset(Swift_Mime_Header $header)
234
    {
235
        if (null !== $this->_charset) {
236
            $header->setCharset($this->_charset);
237
        }
238
    }
239
}
240