| Conditions | 10 |
| Paths | 37 |
| Total Lines | 114 |
| Code Lines | 67 |
| 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 |
||
| 118 | public function __construct($article) |
||
| 119 | { |
||
| 120 | self::$logger = new Logger('TCPDFFacade'); |
||
| 121 | self::$logger->debug('>>__construct()'); |
||
| 122 | |||
| 123 | $config = ConfigProvider::getInstance(); |
||
| 124 | $cache = ServiceFactory::getInstance($config->get('cache.provider.name'), 'Alpha\Util\Cache\CacheProviderInterface'); |
||
| 125 | |||
| 126 | $this->article = $article; |
||
|
|
|||
| 127 | |||
| 128 | $reflect = new \ReflectionClass($this->article); |
||
| 129 | $classname = $reflect->getShortName(); |
||
| 130 | |||
| 131 | $this->PDFCacheKey = 'pdf_'.$classname.'_'.$this->article->getID().'_'.$this->article->getVersion(); |
||
| 132 | $this->HTMLCacheKey = 'html_'.$classname.'_'.$this->article->getID().'_'.$this->article->getVersion(); |
||
| 133 | |||
| 134 | // first check the PDF cache |
||
| 135 | if ($cache->get($this->PDFCacheKey) !== false) { |
||
| 136 | return; |
||
| 137 | } |
||
| 138 | |||
| 139 | if ($cache->get($this->HTMLCacheKey) !== false) { |
||
| 140 | $this->content = $cache->get($this->HTMLCacheKey); |
||
| 141 | } else { |
||
| 142 | $this->content = $this->markdown($this->article->get('content', true)); |
||
| 143 | $cache->set($this->HTMLCacheKey, $this->content); |
||
| 144 | } |
||
| 145 | |||
| 146 | // Replace all instances of $attachURL in link tags to links to the ViewAttachment controller |
||
| 147 | $attachments = array(); |
||
| 148 | preg_match_all('/href\=\"\$attachURL\/.*\"/', $this->content, $attachments); |
||
| 149 | |||
| 150 | foreach ($attachments[0] as $attachmentURL) { |
||
| 151 | $start = mb_strpos($attachmentURL, '/'); |
||
| 152 | $end = mb_strrpos($attachmentURL, '"'); |
||
| 153 | $fileName = mb_substr($attachmentURL, $start+1, $end-($start+1)); |
||
| 154 | |||
| 155 | if (method_exists($this->article, 'getAttachmentSecureURL')) { |
||
| 156 | $this->content = str_replace($attachmentURL, 'href='.$this->article->getAttachmentSecureURL($fileName), $this->content); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | // Handle image attachments |
||
| 161 | $attachments = array(); |
||
| 162 | preg_match_all('/\<img\ src\=\"\$attachURL\/.*\".*\>/', $this->content, $attachments); |
||
| 163 | |||
| 164 | foreach ($attachments[0] as $attachmentURL) { |
||
| 165 | $start = mb_strpos($attachmentURL, '/'); |
||
| 166 | $end = mb_strrpos($attachmentURL, '" alt'); |
||
| 167 | $fileName = mb_substr($attachmentURL, $start+1, $end-($start+1)); |
||
| 168 | |||
| 169 | if ($config->get('cms.images.widget')) { |
||
| 170 | // get the details of the source image |
||
| 171 | $path = $this->article->getAttachmentsLocation().'/'.$fileName; |
||
| 172 | $image_details = getimagesize($path); |
||
| 173 | $imgType = $image_details[2]; |
||
| 174 | if ($imgType == 1) { |
||
| 175 | $type = 'gif'; |
||
| 176 | } elseif ($imgType == 2) { |
||
| 177 | $type = 'jpg'; |
||
| 178 | } else { |
||
| 179 | $type = 'png'; |
||
| 180 | } |
||
| 181 | |||
| 182 | $img = new Image($path, $image_details[0], $image_details[1], $type, 0.95, false, (boolean)$config->get('cms.images.widget.secure')); |
||
| 183 | $this->content = str_replace($attachmentURL, $img->renderHTMLLink(), $this->content); |
||
| 184 | } else { |
||
| 185 | // render a normal image link to the ViewAttachment controller |
||
| 186 | if (method_exists($this->article, 'getAttachmentSecureURL')) { |
||
| 187 | $this->content = str_replace($attachmentURL, '<img src="'.$this->article->getAttachmentSecureURL($fileName).'">', $this->content); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | $this->pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); |
||
| 193 | $this->pdf->SetCreator(PDF_CREATOR); |
||
| 194 | $this->pdf->SetAuthor($this->article->get('author')); |
||
| 195 | $this->pdf->SetTitle($this->article->get('title')); |
||
| 196 | $this->pdf->SetSubject($this->article->get('description')); |
||
| 197 | |||
| 198 | //set margins |
||
| 199 | $this->pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); |
||
| 200 | $this->pdf->SetHeaderMargin(PDF_MARGIN_HEADER); |
||
| 201 | $this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER); |
||
| 202 | |||
| 203 | //set auto page breaks |
||
| 204 | $this->pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM); |
||
| 205 | |||
| 206 | //set image scale factor |
||
| 207 | $this->pdf->setImageScale(2.5); |
||
| 208 | |||
| 209 | // add a page |
||
| 210 | $this->pdf->AddPage(); |
||
| 211 | |||
| 212 | // add the title |
||
| 213 | $title = '<h1>'.$this->article->get('title').'</h1>'; |
||
| 214 | // add some custom footer info about the article |
||
| 215 | $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>'; |
||
| 216 | |||
| 217 | // write the title |
||
| 218 | self::$logger->debug('Writing the title ['.$title.'] to the PDF'); |
||
| 219 | $this->pdf->writeHTML(utf8_encode($title), true, false, true, false, ''); |
||
| 220 | // output the HTML content |
||
| 221 | self::$logger->debug('Writing the content ['.$this->content.'] to the PDF'); |
||
| 222 | $this->pdf->writeHTML(utf8_encode($this->content), true, false, true, false, ''); |
||
| 223 | // write the article footer |
||
| 224 | $this->pdf->writeHTML(utf8_encode($footer), true, false, true, false, ''); |
||
| 225 | self::$logger->debug('Writing the footer ['.$footer.'] to the PDF'); |
||
| 226 | |||
| 227 | // save this PDF to the cache |
||
| 228 | $config->set($this->PDFCacheKey, $this->pdf->Output($this->PDFCacheKey, 'S')); |
||
| 229 | |||
| 230 | self::$logger->debug('<<__construct()'); |
||
| 231 | } |
||
| 232 | |||
| 291 |
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.