Completed
Pull Request — master (#28)
by Frederik
01:37
created

OptimalEncodedHeaderValue::__toString()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 12
nc 6
nop 0
crap 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Header;
5
6
use Genkgo\Mail\Stream\OptimalTransferEncodedPhraseStream;
7
use Genkgo\Mail\Stream\OptimalTransferEncodedTextStream;
8
9
/**
10
 * Class OptimalEncodedHeaderValue
11
 * @package Genkgo\Mail\Header
12
 */
13
final class OptimalEncodedHeaderValue
14
{
15
    /**
16
     *
17
     */
18
    private const FOLDING = "\r\n ";
19
    /**
20
     * @var string
21
     */
22
    private $value;
23
    /**
24
     * @var array
25
     */
26
    private $phrase = false;
27
28
    /**
29
     * OptimalEncodedHeaderValue constructor.
30
     * @param string $value
31
     */
32 95
    public function __construct(string $value)
33
    {
34 95
        $this->value = $value;
35 95
    }
36
37
    /**
38
     * @return string
39
     */
40 95
    public function __toString(): string
41
    {
42 95
        if ($this->phrase === true) {
43 28
            $encoded = new OptimalTransferEncodedPhraseStream($this->value, 68, self::FOLDING);
44
45 28
            $encoding = $encoded->getMetadata(['transfer-encoding'])['transfer-encoding'];
46
        } else {
47 78
            $encoded = new OptimalTransferEncodedTextStream($this->value, 68, self::FOLDING);
48
49 78
            $encoding = $encoded->getMetadata(['transfer-encoding'])['transfer-encoding'];
50
        }
51
52 95
        if ($encoding === '7bit' || $encoding === '8bit') {
53 91
            return (string) $encoded;
54
        }
55
56 6
        if ($encoding === 'base64') {
57 5
            return sprintf('=?%s?B?%s?=', 'UTF-8', (string) $encoded);
58
        }
59
60 1
        return sprintf('=?%s?Q?%s?=', 'UTF-8', (string) $encoded);
61
    }
62
63
    /**
64
     * @param string $value
65
     * @return OptimalEncodedHeaderValue
66
     */
67 28
    public static function forPhrase(string $value): self
68
    {
69 28
        $encoded = new self($value);
70 28
        $encoded->value = $value;
71 28
        $encoded->phrase = true;
0 ignored issues
show
Documentation Bug introduced by
It seems like true of type boolean is incompatible with the declared type array of property $phrase.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72 28
        return $encoded;
73
    }
74
}