encodeString()   C
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 23.4991

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 7
nop 4
dl 0
loc 33
ccs 7
cts 23
cp 0.3043
crap 23.4991
rs 6.7272
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
 * Handles Base64 (B) Header Encoding in Swift Mailer.
13
 *
14
 * @author Chris Corbyn
15
 */
16
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...
17
{
18
    /**
19
     * Get the name of this encoding scheme.
20
     * Returns the string 'B'.
21
     *
22
     * @return string
23
     */
24 2
    public function getName()
25
    {
26 2
        return 'B';
27
    }
28
29
    /**
30
     * Takes an unencoded string and produces a Base64 encoded string from it.
31
     *
32
     * If the charset is iso-2022-jp, it uses mb_encode_mimeheader instead of
33
     * default encodeString, otherwise pass to the parent method.
34
     *
35
     * @param string $string        string to encode
36
     * @param int    $firstLineOffset
37
     * @param int    $maxLineLength optional, 0 indicates the default of 76 bytes
38
     * @param string $charset
39
     *
40
     * @return string
41
     */
42 1
    public function encodeString(string $string, int $firstLineOffset = 0, int $maxLineLength = 0, string $charset = 'utf-8')
43
    {
44 1
        if (Swift::strtolowerWithStaticCache($charset) === 'iso-2022-jp') {
45 1
            $old = \mb_internal_encoding();
46
47 1
            \mb_internal_encoding('utf-8');
48 1
            $newString = \mb_encode_mimeheader($string, $charset, $this->getName(), "\r\n");
49
50 1
            \mb_internal_encoding($old);
51
52 1
            return $newString;
53
        }
54
55
        // safety measure copy-pasted from parent method
56
        if (0 >= $maxLineLength || 76 < $maxLineLength) {
57
            $maxLineLength = 76;
58
        }
59
60
        $cursorPosition = 0;
61
        $encoded = '';
62
        while ($cursorPosition < strlen($string)) {
63
            $maxChunkLength = $this->maxChunkLength($firstLineOffset, $maxLineLength);
64
            if ($cursorPosition > 0 || $firstLineOffset > $maxChunkLength) {
65
                $encoded .= "\r\n";
66
                $maxChunkLength = $this->maxChunkLength(0, $maxLineLength);
67
            }
68
            $chunk = mb_strcut($string, $cursorPosition, $maxChunkLength);
69
            $encoded .= base64_encode($chunk);
70
            $cursorPosition += strlen($chunk);
71
        }
72
73
        return $encoded;
74
    }
75
76
    /**
77
     * Returns maximum number of bytes that can fit in a line with given
78
     * offset and maximum length if encoded with base64
79
     *
80
     * @param int $firstLineOffset
81
     * @param int $maxLineLength
82
     *
83
     * @return int
84
     */
85
    private function maxChunkLength($firstLineOffset, $maxLineLength)
86
    {
87
        return floor(($maxLineLength - $firstLineOffset) / 4) * 3;
88
    }
89
}
90