Total Complexity | 120 |
Total Lines | 242 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like HtmlPdfApiValidator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HtmlPdfApiValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class HtmlPdfApiValidator implements ValidatorInterface { |
||
11 | |||
12 | /** |
||
13 | * Validates request parameters for HtmlPdfApi |
||
14 | * |
||
15 | * @param array $params Parameters to validate |
||
16 | * @return array $params Validated parameters |
||
17 | * @throws \Netgen\HtmlPdfApi\Exception\InvalidParameterException |
||
18 | */ |
||
19 | public function validate($params) |
||
20 | { |
||
21 | $params = $this->boolParams($params); |
||
22 | |||
23 | if (!empty($params['html']) && (!empty($params['url']) || !empty($params['file']))) |
||
24 | throw new InvalidParameterException("Only one of [html, url, file] parameters can be set!"); |
||
25 | |||
26 | if (!empty($params['url']) && !empty($params['file'])) |
||
27 | throw new InvalidParameterException("Only one of [html, url, file] parameters can be set!"); |
||
28 | |||
29 | if ((!empty($params['page_width']) && empty($params['page_height'])) |
||
30 | ||(empty($params['page_width']) && !empty($params['page_height']))) |
||
31 | { |
||
32 | throw new InvalidParameterException('Page width and page height must both be set or unset!'); |
||
33 | } |
||
34 | if (!empty($params['filename']) && !is_string($params['filename'])) |
||
35 | throw new InvalidParameterException('Filename must be a string.'); |
||
36 | |||
37 | if (!empty($params['footer']) && !is_string($params['footer'])) |
||
38 | throw new InvalidParameterException('Footer must be a string.'); |
||
39 | |||
40 | if (!empty($params['header']) && !is_string($params['header'])) |
||
41 | throw new InvalidParameterException('Header must be a string.'); |
||
42 | |||
43 | if (!empty($params['header_spacing']) && !is_int($params['header_spacing'])) |
||
44 | throw new InvalidParameterException('Header spacing parameter must be an integer.'); |
||
45 | |||
46 | if (!empty($params['footer_spacing']) && !is_int($params['footer_spacing'])) |
||
47 | throw new InvalidParameterException('Footer spacing parameter must be an integer.'); |
||
48 | |||
49 | if (!empty($params['page_size']) && !is_string($params['page_size'])) |
||
50 | throw new InvalidParameterException('Page size must be a string of some standard page size.'); |
||
51 | |||
52 | if (!empty($params['dpi']) && (!is_int($params['dpi']) || $params['dpi']<75)) |
||
53 | throw new InvalidParameterException('DPI must be an integer larger than 75.'); |
||
54 | |||
55 | if (!empty($params['image_dpi']) && !is_int($params['image_dpi'])) |
||
56 | throw new InvalidParameterException('Image DPI must be an integer.'); |
||
57 | |||
58 | if (!empty($params['lowquality']) && (!is_int($params['lowquality']) || !in_array($params['lowquality'], array( |
||
59 | 0, 1 ) ))) |
||
60 | throw new InvalidParameterException( |
||
61 | 'Lowquality must be either 0 or 1 (alternative: use bool values'); |
||
62 | |||
63 | if (!empty($params['orientation']) && (!is_string($params['orientation']) || !in_array($params['orientation'], |
||
64 | array("landscape", "portrait")))) |
||
65 | throw new InvalidParameterException('Orientation must be set to either "landscape" or "portrait"'); |
||
66 | |||
67 | if (!empty($params['margin_top']) && !is_int($params['margin_top'])) |
||
68 | throw new InvalidParameterException('Margin top must be an integer.'); |
||
69 | |||
70 | if (!empty($params['margin_bottom']) && !is_int($params['margin_bottom'])) |
||
71 | throw new InvalidParameterException('Margin top must be an integer.'); |
||
72 | |||
73 | if (!empty($params['margin_left']) && !is_int($params['margin_left'])) |
||
74 | throw new InvalidParameterException('Margin top must be an integer.'); |
||
75 | |||
76 | if (!empty($params['margin_right']) && !is_int($params['margin_right'])) |
||
77 | throw new InvalidParameterException('Margin top must be an integer.'); |
||
78 | |||
79 | if (!empty($params['background']) && (!is_int($params['background']) || !in_array($params['background'], array( |
||
80 | 0, 1 ) ))) |
||
81 | throw new InvalidParameterException( |
||
82 | 'Background must be either 0 or 1 (alternative: use bool values'); |
||
83 | |||
84 | if (!empty($params['images']) && (!is_int($params['images']) || !in_array($params['images'], array( |
||
85 | 0, 1 ) ))) |
||
86 | throw new InvalidParameterException( |
||
87 | 'Images must be either 0 or 1 (alternative: use bool values'); |
||
88 | |||
89 | if (!empty($params['page_width']) && !is_int($params['page_width'])) |
||
90 | throw new InvalidParameterException('Page width must be an integer.'); |
||
91 | |||
92 | if (!empty($params['page_height']) && !is_int($params['page_height'])) |
||
93 | throw new InvalidParameterException('Page height must be an integer.'); |
||
94 | |||
95 | if (!empty($params['disposition']) && (!is_string($params['disposition']) || !in_array($params['disposition'], |
||
96 | array("attachment", "inline")))) |
||
97 | throw new InvalidParameterException('Disposition can only be "attachment or "inline"'); |
||
98 | |||
99 | if (!empty($params['group']) && !is_string($params['group'])) |
||
100 | throw new InvalidParameterException('Group must be a string.'); |
||
101 | |||
102 | if (!empty($params['engine_version']) && (!is_int($params['engine_version']) || !in_array($params['engine_version'], |
||
103 | array( 11, 12 ) ))) |
||
104 | throw new InvalidParameterException( |
||
105 | 'Engine version can be integer 11 or 12'); |
||
106 | |||
107 | if (!empty($params['title']) && !is_string($params['title'])) |
||
108 | throw new InvalidParameterException('Title must be a string.'); |
||
109 | |||
110 | if (!empty($params['outline']) && (!is_int($params['outline']) || !in_array($params['outline'], array( |
||
111 | 0, 1 ) ))) |
||
112 | throw new InvalidParameterException( |
||
113 | 'Outline must be either 0 or 1 (alternative: use bool values'); |
||
114 | |||
115 | if (!empty($params['outline_depth']) && !is_int($params['outline_depth'])) |
||
116 | throw new InvalidParameterException('Outline depth must be an integer.'); |
||
117 | |||
118 | if (!empty($params['encoding']) && !is_string($params['encoding'])) |
||
119 | throw new InvalidParameterException('Encoding must be a string.'); |
||
120 | |||
121 | if (!empty($params['javascript']) && (!is_int($params['javascript']) || !in_array($params['javascript'], array( |
||
122 | 0, 1 ) ))) |
||
123 | throw new InvalidParameterException( |
||
124 | 'Javascript must be either 0 or 1 (alternative: use bool values'); |
||
125 | |||
126 | if (!empty($params['javascript_delay']) && |
||
127 | (!is_int($params['javascript_delay']) || $params['javascript_delay']<1 || $params['javascript_delay']>800)) |
||
128 | throw new InvalidParameterException('Javascript delay must be an integer larger than 1, but smaller than 800.'); |
||
129 | |||
130 | if (!empty($params['internal_links']) && |
||
131 | (!is_int($params['internal_links']) || !in_array($params['internal_links'], array( 0, 1 ) ))) |
||
132 | throw new InvalidParameterException( |
||
133 | 'Internal links must be either 0 or 1 (alternative: use bool values'); |
||
134 | |||
135 | if (!empty($params['external_links']) && |
||
136 | (!is_int($params['external_links']) || !in_array($params['external_links'], array( 0, 1 ) ))) |
||
137 | throw new InvalidParameterException( |
||
138 | 'External links must be either 0 or 1 (alternative: use bool values'); |
||
139 | |||
140 | if (!empty($params['page_offset']) && !is_int($params['page_offset'])) |
||
141 | throw new InvalidParameterException('Page offset must be an integer.'); |
||
142 | |||
143 | if (!empty($params['username']) && !is_string($params['username'])) |
||
144 | throw new InvalidParameterException('Username must be a string.'); |
||
145 | |||
146 | if (!empty($params['password']) && !is_string($params['password'])) |
||
147 | throw new InvalidParameterException('Password must be a string.'); |
||
148 | |||
149 | if (!empty($params['use_print_media']) && |
||
150 | (!is_int($params['use_print_media']) || !in_array($params['use_print_media'], array( |
||
151 | 0, 1 ) ))) |
||
152 | throw new InvalidParameterException( |
||
153 | 'Use print media must be either 0 or 1 (alternative: use bool values'); |
||
154 | |||
155 | if (!empty($params['zoom']) && !is_numeric($params['zoom'])) |
||
156 | throw new InvalidParameterException('Zoom must be an integer.'); |
||
157 | |||
158 | if (!empty($params['viewport_size']) && |
||
159 | (!is_string($params['viewport_size'] || preg_match("^\\d+x\\d+$", $params['viewport_size'])))) |
||
160 | throw new InvalidParameterException('Viewport must be a string of a following format: [width]x[height].'); |
||
161 | |||
162 | if (!empty($params['id']) && !is_string($params['id'])) |
||
163 | throw new InvalidParameterException('ID must be a string.'); |
||
164 | |||
165 | /*if (!empty($params['file'])) |
||
|
|||
166 | $this->validateFile($params['file']);*/ |
||
167 | |||
168 | return $params; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Checks if asset exists at provided path, and is of supported format |
||
173 | * |
||
174 | * @param string $file Path to file (prefixed with @) |
||
175 | * |
||
176 | * @throws \Netgen\HtmlPdfApi\Exception\FileNotFoundException |
||
177 | * @throws \Netgen\HtmlPdfApi\Exception\WrongFileExtensionException |
||
178 | */ |
||
179 | public function validateAssetFile($file) |
||
180 | { |
||
181 | $filePath = substr($file, 1); |
||
182 | |||
183 | if (!is_file($filePath)) |
||
184 | throw new FileNotFoundException("File ".$filePath." was not found"); |
||
185 | |||
186 | $path_parts = pathinfo($filePath); |
||
187 | |||
188 | if( !in_array($path_parts['extension'], array( |
||
189 | "js", "css", "png", "jpg", "jpeg", "gif", "ttf", "otf", "woff" |
||
190 | ))) |
||
191 | throw new WrongFileExtensionException("Wrong file format\n |
||
192 | Supported formats: js, css, png, jpg, jpeg, gif, ttf, otf, woff"); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Checks if file exists at provided path, and is of supported format |
||
197 | * |
||
198 | * @param string $file Path to file (prefixed with @) |
||
199 | * |
||
200 | * @throws \Netgen\HtmlPdfApi\Exception\FileNotFoundException |
||
201 | * @throws \Netgen\HtmlPdfApi\Exception\WrongFileExtensionException |
||
202 | */ |
||
203 | public function validateHtmlFile($file) |
||
204 | { |
||
205 | $filePath = substr($file, 1); |
||
206 | |||
207 | if (!is_file($filePath)) |
||
208 | throw new FileNotFoundException("File ".$filePath." was not found"); |
||
209 | |||
210 | $path_parts = pathinfo($filePath); |
||
211 | |||
212 | if( !in_array($path_parts['extension'], array( |
||
213 | "html", "htm", "zip", "tar.gz", "tgz", "tar.bz2" |
||
214 | ))) |
||
215 | throw new WrongFileExtensionException("Wrong file format\n |
||
216 | Supported formats: html, htm, zip, tar.gz, tgz, tar.bz2"); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Casts bool parameters to integer if set to false |
||
221 | * |
||
222 | * @param array $params Parameters |
||
223 | * @return array $params |
||
224 | */ |
||
225 | private function boolParams($params) |
||
252 | } |
||
253 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.