blib/lib/Mail/AuthenticationResults/FoldableHeader.pm untested

filter: Statementstmt 0.0% 0 / 145 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 233 CC 16 SCAR 36.3
CC
16
Cov
0%
as_string
132.0
eol
6.0
indent
6.0
CRAP 37.9
1 package Mail::AuthenticationResults::FoldableHeader;
2 # ABSTRACT: Class for modelling a foldable header string
3  
4 require 5.008;
5 use strict;
6 use warnings;
7 our $VERSION = '2.20260216'; # VERSION
8 use Carp;
9  
10 use Mail::AuthenticationResults::Token::String;
11 use Mail::AuthenticationResults::Token::Space;
12 use Mail::AuthenticationResults::Token::Separator;
13 use Mail::AuthenticationResults::Token::Comment;
14 use Mail::AuthenticationResults::Token::Assignment;
15  
16  
17 sub new {
18 my ( $class, $args ) = @_;
19  
20 my $self = {};
21 bless $self, $class;
22  
23 $self->{ 'string' } = [];
24  
25 return $self;
26 }
27  
28  
29 sub eol {
30 my ( $self ) = @_;
31 return $self->{ 'eol' } if exists ( $self->{ 'eol' } );
32 return "\n";
33 }
34  
35  
36 sub set_eol {
37 my ( $self, $eol ) = @_;
38 $self->{ 'eol' } = $eol;
39 return $self;
40 }
41  
42  
43 sub indent {
44 my ( $self ) = @_;
45 return $self->{ 'indent' } if exists ( $self->{ 'indent' } );
46 return ' ';
47 }
48  
49  
50 sub set_indent {
51 my ( $self, $indent ) = @_;
52 $self->{ 'indent' } = $indent;
53 return $self;
54 }
55  
56  
57 sub sub_indent {
58 my ( $self ) = @_;
59 return $self->{ 'sub_indent' } if exists ( $self->{ 'sub_indent' } );
60 return ' ';
61 }
62  
63  
64 sub set_sub_indent {
65 my ( $self, $indent ) = @_;
66 $self->{ 'sub_indent' } = $indent;
67 return $self;
68 }
69  
70  
71 sub try_fold_at {
72 my ( $self ) = @_;
73 return $self->{ 'try_fold_at' } if exists ( $self->{ 'try_fold_at' } );
74 return 800;
75 }
76  
77  
78 sub set_try_fold_at {
79 my ( $self, $length ) = @_;
80 $self->{ 'try_fold_at' } = $length;
81 return $self;
82 }
83  
84  
85 sub force_fold_at {
86 my ( $self ) = @_;
87 return $self->{ 'force_fold_at' } if exists ( $self->{ 'force_fold_at' } );
88 return 900;
89 }
90  
91  
92 sub set_force_fold_at {
93 my ( $self, $length ) = @_;
94 $self->{ 'force_fold_at' } = $length;
95 return $self;
96 }
97  
98  
99 sub string {
100 my( $self, $string ) = @_;
101 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::String->new_from_value( $string );
102 return $self;
103 }
104  
105  
106 sub space {
107 my ( $self, $string ) = @_;
108 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Space->new_from_value( $string );
109 return $self;
110 }
111  
112  
113 sub separator {
114 my ( $self, $string ) = @_;
115 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Separator->new_from_value( $string );
116 return $self;
117 }
118  
119  
120 sub comment {
121 my ( $self, $string ) = @_;
122 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Comment->new_from_value( $string );
123 return $self;
124 }
125  
126  
127 sub assignment {
128 my ( $self, $string ) = @_;
129 push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Assignment->new_from_value( $string );
130 return $self;
131 }
132  
133  
134 sub as_string {
135 my ( $self ) = @_;
136  
137 my $string = q{};
138 my $string_length = 0;
139 my $content_added = 0;
140  
141 my $sections = [];
142 my $stack = [];
143 my $last_type;
144  
145 foreach my $part ( @{ $self->{ 'string' } } ) {
146 if ( $part->is() eq 'space' && $last_type ne 'space' ) {
147 # We have a folding space
148 push @$sections, $stack if @$stack;
149 $stack = [];
150 }
151 push @$stack, $part;
152 $last_type = $part->is();
153 }
154 push @$sections, $stack if @$stack;
155  
156 my $eol = $self->eol();;
157 my $indent = $self->indent();
158 my $sub_indent = $self->sub_indent();
159  
160 my $fold_length = 0;
161 SECTION:
162 while ( my $section = shift @$sections ) {
163 if ( $section->[0]->is() eq 'space' && $section->[0]->value() eq $eol ) {
164 # This section starts a new line
165 $fold_length = 0;
166 if ( ! exists( $section->[0]->{ '_folded' } ) ) {
167 if ( $section->[1]->is() eq 'space' ) {
168 # Take the last indent value for the fold indent
169 $indent = $section->[1]->value();
170 }
171 }
172 }
173  
174 my $section_string = join( q{}, map { $_->value() } @$section );
175 my $section_length = length( $section_string );
176  
177 if ( $fold_length + $section_length > $self->try_fold_at() ) {
178 if ( $fold_length > 0 ) {
179 # Remove whitespace tokens at beginning of section
180 while ( $section->[0]->is() eq 'space' ) {
181 shift @$section;
182 }
183 # Insert new folding whitespace at beginning of section
184 unshift @$section, Mail::AuthenticationResults::Token::Space->new_from_value( $indent . $sub_indent );
185 unshift @$section, Mail::AuthenticationResults::Token::Space->new_from_value( $eol );
186 $section->[0]->{ '_folded' } = 1;
187 unshift @$sections, $section;
188 next SECTION;
189 }
190 else {
191 # ToDo:
192 # This section alone is over the line limit
193 # It already starts with a fold, so we need to remove
194 # some of it to a new line if we can.
195  
196 # Strategy 1: Fold at a relevant token boundary
197 my $first_section = [];
198 my $second_section = [];
199 push @$second_section, Mail::AuthenticationResults::Token::Space->new_from_value( $eol );
200 push @$second_section, Mail::AuthenticationResults::Token::Space->new_from_value( $indent . $sub_indent );
201 $second_section->[0]->{ '_folded' } = 1;
202 my $first_section_length = 0;
203 foreach my $part ( @$section ) {
204 my $part_length = length $part->value();
205 if ( $part_length + $first_section_length < $self->try_fold_at() ) {
206 push @$first_section, $part;
207 $first_section_length += $part_length;
208 }
209 else {
210 push @$second_section, $part;
211 $first_section_length = $self->try_fold_at() + 1; # everything from this point goes onto second
212 }
213 }
214 # Do we have a first and second section with actual content?
215 if ( ( grep { $_->is() ne 'space' } @$first_section ) &&
216 ( grep { $_->is() ne 'space' } @$second_section ) ) {
217 unshift @$sections, $second_section;
218 unshift @$sections, $first_section;
219 next SECTION;
220 }
221  
222 # We MUST fold at $self->force_fold_at();
223 # Strategy 2: Force fold at a space within a string
224 # Strategy 3: Force fold anywhere
225  
226 # We assume that force fold is greater than try fold
227 }
228 }
229  
230 $string .= $section_string;
231 $fold_length += $section_length;
232 }
233  
234 return $string;
235 }
236  
237 1;
238  
239 __END__