Writer::writeLine()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kodus\Mail;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Internal helper component - writes data to a stream using various filters.
9
 *
10
 * @internal
11
 */
12
class Writer
13
{
14
    /**
15
     * @var int
16
     */
17
    public $line_length = 76;
18
19
    /**
20
     * @var string end-of-line characters
21
     */
22
    public $eol = "\r\n";
23
24
    /**
25
     * @var resource
26
     */
27
    protected $output;
28
29
    /**
30
     * @param resource $output output stream handle
31
     */
32 15
    public function __construct($output)
33
    {
34 15
        $this->output = $output;
35 15
    }
36
37
    /**
38
     * Writes raw data from a string, or the raw contents of a given resource, to the output stream
39
     *
40
     * @param string|resource $input
41
     */
42 15
    public function write($input): void
43
    {
44 15
        if (is_string($input)) {
45 15
            fwrite($this->output, $input);
46 15
        } elseif (is_resource($input)) {
47 7
            rewind($input);
48 7
            stream_copy_to_stream($input, $this->output);
49 7
        } else {
50
            throw new InvalidArgumentException("unsupported data-type; expected string or resource handle");
51
        }
52 15
    }
53
54
    /**
55
     * Writes the given raw string data, plus an EOL character, to the output stream
56
     *
57
     * @param string $string
58
     */
59 15
    public function writeLine($string = ""): void
60
    {
61 15
        fwrite($this->output, $string);
62 15
        fwrite($this->output, $this->eol);
63 15
    }
64
65
    /**
66
     * Writes data from a string, or the raw contents of a given resource, as quoted-printable, to the output stream
67
     *
68
     * @param string|resource $input
69
     */
70 15
    public function writeQuotedPrintable($input): void
71
    {
72 15
        $this->writeFiltered(
73 15
            $input,
74 15
            'convert.quoted-printable-encode',
75
            [
76 15
                "line-length"      => $this->line_length,
77 15
                "line-break-chars" => $this->eol,
78
            ]
79 15
        );
80 15
    }
81
82
    /**
83
     * Writes data from a string, or the raw contents of a given resource, base-64 encoded, to the output stream
84
     *
85
     * @param string|resource $input
86
     */
87 9
    public function writeBase64($input): void
88
    {
89 9
        $this->writeFiltered(
90 9
            $input,
91 9
            'convert.base64-encode',
92
            [
93 9
                "line-length"        => $this->line_length,
94 9
                "line-break-chars"   => $this->eol,
95 9
                "force-encode-first" => true,
96
            ]
97 9
        );
98 9
    }
99
100
    /**
101
     * Write data from a string, or the contents of a given resource, to the output string, while applying a filter
102
     *
103
     * @param string|resource $input
104
     * @param string          $filter
105
     * @param array           $options
106
     */
107 15
    protected function writeFiltered($input, string $filter, array $options = []): void
108
    {
109 15
        $filter = stream_filter_prepend($this->output, $filter, STREAM_FILTER_WRITE, $options);
110
111 15
        $this->write($input);
112
113 15
        stream_filter_remove($filter);
114 15
    }
115
}
116