| Conditions | 10 |
| Paths | 37 |
| Total Lines | 113 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 117 | public function __construct($article) |
||
| 118 | { |
||
| 119 | self::$logger = new Logger('TCPDFFacade'); |
||
| 120 | self::$logger->debug('>>__construct()'); |
||
| 121 | |||
| 122 | $config = ConfigProvider::getInstance(); |
||
| 123 | |||
| 124 | $this->article = $article; |
||
|
|
|||
| 125 | |||
| 126 | $reflect = new \ReflectionClass($this->article); |
||
| 127 | $classname = $reflect->getShortName(); |
||
| 128 | |||
| 129 | $this->PDFFilename = $config->get('app.file.store.dir').'cache/pdf/'.$classname.'_'.$this->article->getID().'_'.$this->article->getVersion().'.pdf'; |
||
| 130 | $this->HTMLFilename = $config->get('app.file.store.dir').'cache/html/'.$classname.'_'.$this->article->getID().'_'.$this->article->getVersion().'.html'; |
||
| 131 | |||
| 132 | // first check the PDF cache |
||
| 133 | if ($this->checkPDFCache()) { |
||
| 134 | return; |
||
| 135 | } |
||
| 136 | |||
| 137 | if ($this->checkHTMLCache()) { |
||
| 138 | $this->loadHTMLCache(); |
||
| 139 | } else { |
||
| 140 | $this->content = $this->markdown($this->article->get('content', true)); |
||
| 141 | $this->HTMLCache(); |
||
| 142 | } |
||
| 143 | |||
| 144 | // Replace all instances of $attachURL in link tags to links to the ViewAttachment controller |
||
| 145 | $attachments = array(); |
||
| 146 | preg_match_all('/href\=\"\$attachURL\/.*\"/', $this->content, $attachments); |
||
| 147 | |||
| 148 | foreach ($attachments[0] as $attachmentURL) { |
||
| 149 | $start = mb_strpos($attachmentURL, '/'); |
||
| 150 | $end = mb_strrpos($attachmentURL, '"'); |
||
| 151 | $fileName = mb_substr($attachmentURL, $start + 1, $end - ($start + 1)); |
||
| 152 | |||
| 153 | if (method_exists($this->article, 'getAttachmentSecureURL')) { |
||
| 154 | $this->content = str_replace($attachmentURL, 'href='.$this->article->getAttachmentSecureURL($fileName), $this->content); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | // Handle image attachments |
||
| 159 | $attachments = array(); |
||
| 160 | preg_match_all('/\<img\ src\=\"\$attachURL\/.*\".*\>/', $this->content, $attachments); |
||
| 161 | |||
| 162 | foreach ($attachments[0] as $attachmentURL) { |
||
| 163 | $start = mb_strpos($attachmentURL, '/'); |
||
| 164 | $end = mb_strrpos($attachmentURL, '" alt'); |
||
| 165 | $fileName = mb_substr($attachmentURL, $start + 1, $end - ($start + 1)); |
||
| 166 | |||
| 167 | if ($config->get('cms.images.widget')) { |
||
| 168 | // get the details of the source image |
||
| 169 | $path = $this->article->getAttachmentsLocation().'/'.$fileName; |
||
| 170 | $image_details = getimagesize($path); |
||
| 171 | $imgType = $image_details[2]; |
||
| 172 | if ($imgType == 1) { |
||
| 173 | $type = 'gif'; |
||
| 174 | } elseif ($imgType == 2) { |
||
| 175 | $type = 'jpg'; |
||
| 176 | } else { |
||
| 177 | $type = 'png'; |
||
| 178 | } |
||
| 179 | |||
| 180 | $img = new Image($path, $image_details[0], $image_details[1], $type, 0.95, false, (boolean) $config->get('cms.images.widget.secure')); |
||
| 181 | $this->content = str_replace($attachmentURL, $img->renderHTMLLink(), $this->content); |
||
| 182 | } else { |
||
| 183 | // render a normal image link to the ViewAttachment controller |
||
| 184 | if (method_exists($this->article, 'getAttachmentSecureURL')) { |
||
| 185 | $this->content = str_replace($attachmentURL, '<img src="'.$this->article->getAttachmentSecureURL($fileName).'">', $this->content); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $this->pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); |
||
| 191 | $this->pdf->SetCreator(PDF_CREATOR); |
||
| 192 | $this->pdf->SetAuthor($this->article->get('author')); |
||
| 193 | $this->pdf->SetTitle($this->article->get('title')); |
||
| 194 | $this->pdf->SetSubject($this->article->get('description')); |
||
| 195 | |||
| 196 | //set margins |
||
| 197 | $this->pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); |
||
| 198 | $this->pdf->SetHeaderMargin(PDF_MARGIN_HEADER); |
||
| 199 | $this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER); |
||
| 200 | |||
| 201 | //set auto page breaks |
||
| 202 | $this->pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM); |
||
| 203 | |||
| 204 | //set image scale factor |
||
| 205 | $this->pdf->setImageScale(2.5); |
||
| 206 | |||
| 207 | // add a page |
||
| 208 | $this->pdf->AddPage(); |
||
| 209 | |||
| 210 | // add the title |
||
| 211 | $title = '<h1>'.$this->article->get('title').'</h1>'; |
||
| 212 | // add some custom footer info about the article |
||
| 213 | $footer = '<br><p>Article URL: <a href="'.$this->article->get('URL').'">'.$this->article->get('URL').'</a><br>Title: '.$this->article->get('title').'<br>Author: '.$this->article->get('author').'</p>'; |
||
| 214 | |||
| 215 | // write the title |
||
| 216 | self::$logger->debug('Writing the title ['.$title.'] to the PDF'); |
||
| 217 | $this->pdf->writeHTML(utf8_encode($title), true, false, true, false, ''); |
||
| 218 | // output the HTML content |
||
| 219 | self::$logger->debug('Writing the content ['.$this->content.'] to the PDF'); |
||
| 220 | $this->pdf->writeHTML(utf8_encode($this->content), true, false, true, false, ''); |
||
| 221 | // write the article footer |
||
| 222 | $this->pdf->writeHTML(utf8_encode($footer), true, false, true, false, ''); |
||
| 223 | self::$logger->debug('Writing the footer ['.$footer.'] to the PDF'); |
||
| 224 | |||
| 225 | // save this PDF to the cache |
||
| 226 | $this->pdf->Output($this->PDFFilename, 'F'); |
||
| 227 | |||
| 228 | self::$logger->debug('<<__construct()'); |
||
| 229 | } |
||
| 230 | |||
| 357 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.