| Conditions | 12 |
| Paths | 145 |
| Total Lines | 83 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 119 | public function generatePDF() |
||
| 120 | { |
||
| 121 | if (!Config::inst()->get(BasePage::class, 'pdf_export')) { |
||
| 122 | return false; |
||
| 123 | } |
||
| 124 | |||
| 125 | $binaryPath = Config::inst()->get(BasePage::class, 'wkhtmltopdf_binary'); |
||
| 126 | if (!$binaryPath || !is_executable($binaryPath)) { |
||
| 127 | if (Environment::getEnv('WKHTMLTOPDF_BINARY') |
||
| 128 | && is_executable(Environment::getEnv('WKHTMLTOPDF_BINARY')) |
||
| 129 | ) { |
||
| 130 | $binaryPath = Environment::getEnv('WKHTMLTOPDF_BINARY'); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | if (!$binaryPath) { |
||
| 135 | user_error('Neither WKHTMLTOPDF_BINARY nor BasePage.wkhtmltopdf_binary are defined', E_USER_ERROR); |
||
| 136 | } |
||
| 137 | |||
| 138 | if (Versioned::get_reading_mode() == 'Stage.Stage') { |
||
| 139 | user_error('Generating PDFs on draft is not supported', E_USER_ERROR); |
||
| 140 | } |
||
| 141 | |||
| 142 | set_time_limit(60); |
||
| 143 | |||
| 144 | // prepare the paths |
||
| 145 | $pdfFile = $this->dataRecord->getPdfFilename(); |
||
| 146 | $bodyFile = str_replace('.pdf', '_pdf.html', $pdfFile); |
||
| 147 | $footerFile = str_replace('.pdf', '_pdffooter.html', $pdfFile); |
||
| 148 | |||
| 149 | // make sure the work directory exists |
||
| 150 | if (!file_exists(dirname($pdfFile))) { |
||
| 151 | Filesystem::makeFolder(dirname($pdfFile)); |
||
| 152 | } |
||
| 153 | |||
| 154 | //decide the domain to use in generation |
||
| 155 | $pdfBaseUrl = $this->getPDFBaseURL(); |
||
| 156 | |||
| 157 | // Force http protocol on CWP - fetching from localhost without using the proxy, SSL terminates on gateway. |
||
| 158 | if (Environment::getEnv('CWP_ENVIRONMENT')) { |
||
| 159 | Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
||
| 160 | //only set alternate protocol if CWP_SECURE_DOMAIN is defined OR pdf_base_url is |
||
| 161 | if ($pdfBaseUrl) { |
||
| 162 | Config::modify()->set(Director::class, 'alternate_base_url', 'http://' . $pdfBaseUrl); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | $bodyViewer = $this->getViewer('pdf'); |
||
| 167 | |||
| 168 | // write the output of this page to HTML, ready for conversion to PDF |
||
| 169 | file_put_contents($bodyFile, $bodyViewer->process($this)); |
||
| 170 | |||
| 171 | // get the viewer for the current template with _pdffooter |
||
| 172 | $footerViewer = $this->getViewer('pdffooter'); |
||
| 173 | |||
| 174 | // write the output of the footer template to HTML, ready for conversion to PDF |
||
| 175 | file_put_contents($footerFile, $footerViewer->process($this)); |
||
| 176 | |||
| 177 | //decide what the proxy should look like |
||
| 178 | $proxy = $this->getPDFProxy($pdfBaseUrl); |
||
| 179 | |||
| 180 | // finally, generate the PDF |
||
| 181 | $command = $binaryPath . $proxy . ' --outline -B 40pt -L 20pt -R 20pt -T 20pt --encoding utf-8 ' |
||
| 182 | . '--orientation Portrait --disable-javascript --quiet --print-media-type '; |
||
| 183 | $retVal = 0; |
||
| 184 | $output = array(); |
||
| 185 | exec( |
||
| 186 | $command . " --footer-html \"$footerFile\" \"$bodyFile\" \"$pdfFile\" &> /dev/stdout", |
||
| 187 | $output, |
||
| 188 | $retVal |
||
| 189 | ); |
||
| 190 | |||
| 191 | // remove temporary file |
||
| 192 | unlink($bodyFile); |
||
| 193 | unlink($footerFile); |
||
| 194 | |||
| 195 | // output any errors |
||
| 196 | if ($retVal != 0) { |
||
| 197 | user_error('wkhtmltopdf failed: ' . implode("\n", $output), E_USER_ERROR); |
||
| 198 | } |
||
| 199 | |||
| 200 | // serve the generated file |
||
| 201 | return HTTPRequest::send_file(file_get_contents($pdfFile), basename($pdfFile), 'application/pdf'); |
||
| 202 | } |
||
| 314 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths