Completed
Pull Request — 5.x (#24)
by Lars
06:58
created

Swift_Mime_HeaderEncoder_Base64HeaderEncoder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 35.7%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 76
ccs 10
cts 28
cp 0.357
rs 10
wmc 9
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
C encodeString() 0 35 7
A maxChunkLength() 0 4 1
1
<?php
2
3
use voku\helper\UTF8;
4
5
/*
6
 * This file is part of SwiftMailer.
7
 * (c) 2004-2009 Chris Corbyn
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
/**
14
 * Handles Base64 (B) Header Encoding in Swift Mailer.
15
 *
16
 * @author Chris Corbyn
17
 */
18
class Swift_Mime_HeaderEncoder_Base64HeaderEncoder extends Swift_Encoder_Base64Encoder implements Swift_Mime_HeaderEncoder
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...
19
{
20
    /**
21
     * Get the name of this encoding scheme.
22
     * Returns the string 'B'.
23
     *
24
     * @return string
25
     */
26 2
    public function getName()
27
    {
28 2
        return 'B';
29
    }
30
31
    /**
32
     * Takes an unencoded string and produces a Base64 encoded string from it.
33
     *
34
     * If the charset is iso-2022-jp, it uses mb_encode_mimeheader instead of
35
     * default encodeString, otherwise pass to the parent method.
36
     *
37
     * @param string $string        string to encode
38
     * @param int    $firstLineOffset
39
     * @param int    $maxLineLength optional, 0 indicates the default of 76 bytes
40
     * @param string $charset
41
     *
42
     * @return string
43
     */
44 1
    public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0, $charset = 'utf-8')
45
    {
46 1
        if (Swift::strtolowerWithStaticCache($charset) === 'iso-2022-jp') {
47 1
            UTF8::checkForSupport();
48
49 1
            $old = mb_internal_encoding();
50
51 1
            mb_internal_encoding('utf-8');
52 1
            $newString = mb_encode_mimeheader($string, $charset, $this->getName(), "\r\n");
53
54 1
            mb_internal_encoding($old);
55
56 1
            return $newString;
57
        }
58
59
        // safety measure copy-pasted from parent method
60
        if (0 >= $maxLineLength || 76 < $maxLineLength) {
61
            $maxLineLength = 76;
62
        }
63
64
        $cursorPosition = 0;
65
        $encoded = '';
66
        while ($cursorPosition < strlen($string)) {
67
            $maxChunkLength = $this->maxChunkLength($firstLineOffset, $maxLineLength);
68
            if ($cursorPosition > 0 || $firstLineOffset > $maxChunkLength) {
69
                $encoded .= "\r\n";
70
                $maxChunkLength = $this->maxChunkLength(0, $maxLineLength);
71
            }
72
            $chunk = mb_strcut($string, $cursorPosition, $maxChunkLength);
73
            $encoded .= base64_encode($chunk);
74
            $cursorPosition += strlen($chunk);
75
        }
76
77
        return $encoded;
78
    }
79
80
    /**
81
     * Returns maximum number of bytes that can fit in a line with given
82
     * offset and maximum length if encoded with base64
83
     *
84
     * @param int $firstLineOffset
85
     * @param int $maxLineLength
86
     *
87
     * @return int
88
     */
89
    private function maxChunkLength($firstLineOffset, $maxLineLength)
90
    {
91
        return floor(($maxLineLength - $firstLineOffset) / 4) * 3;
92
    }
93
}
94