| Total Complexity | 47 |
| Total Lines | 272 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like HeadlineRenderer 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 HeadlineRenderer, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 30 | class HeadlineRenderer |
||
| 31 | { |
||
| 32 | // holds reference to Headline class object |
||
| 33 | protected $headline; |
||
| 34 | protected $tpl; |
||
| 35 | protected $feed; |
||
| 36 | protected $block; |
||
| 37 | protected $errors = []; |
||
| 38 | // RSS2 SAX parser |
||
| 39 | protected $parser; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * HeadlineRenderer constructor. |
||
| 43 | */ |
||
| 44 | public function __construct(Headline $headline) |
||
| 45 | { |
||
| 46 | $this->headline = $headline; |
||
| 47 | $this->tpl = new \XoopsTpl(); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return int|bool |
||
| 52 | */ |
||
| 53 | public function updateCache() |
||
| 54 | { |
||
| 55 | $helper = Helper::getInstance(); |
||
| 56 | /** |
||
| 57 | * Update cache - first try using fopen and then cURL |
||
| 58 | */ |
||
| 59 | $retval = false; |
||
| 60 | if ($fp = @\fopen($this->headline->getVar('headline_rssurl'), 'rb')) { // successfully openned file using fopen |
||
| 61 | $data = ''; |
||
| 62 | while (!\feof($fp)) { |
||
| 63 | $data .= \fgets($fp, 4096); |
||
| 64 | } |
||
| 65 | \fclose($fp); |
||
| 66 | $this->headline->setVar('headline_xml', $this->convertToUtf8($data)); |
||
| 67 | $this->headline->setVar('headline_updated', \time()); |
||
| 68 | $headlineHandler = $helper->getHandler('Headline'); |
||
| 69 | if (null !== $headlineHandler) { |
||
| 70 | $retval = $headlineHandler->insert($this->headline); |
||
| 71 | } |
||
| 72 | } else { |
||
| 73 | // failed open using fopen, now try cURL |
||
| 74 | $ch = \curl_init($this->headline->getVar('headline_rssurl')); |
||
| 75 | if (\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true)) { |
||
| 76 | if ($data = \curl_exec($ch)) { |
||
| 77 | \curl_close($ch); |
||
| 78 | $this->headline->setVar('headline_xml', $this->convertToUtf8($data)); |
||
|
|
|||
| 79 | $this->headline->setVar('headline_updated', \time()); |
||
| 80 | $headlineHandler = $helper->getHandler('Headline'); |
||
| 81 | if (null !== $headlineHandler) { |
||
| 82 | $retval = $headlineHandler->insert($this->headline); |
||
| 83 | } |
||
| 84 | } else { |
||
| 85 | \curl_close($ch); |
||
| 86 | $errmsg = \sprintf(\_MD_XOOPSHEADLINE_NOTOPEN, $this->headline->getVar('headline_rssurl')); |
||
| 87 | $this->setErrors($errmsg); |
||
| 88 | } |
||
| 89 | } else { |
||
| 90 | $this->setErrors(\_MD_XOOPSHEADLINE_BADOPT); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | return $retval; |
||
| 95 | } |
||
| 96 | |||
| 97 | public function renderFeed(bool $force_update = false): bool |
||
| 98 | { |
||
| 99 | $retval = false; |
||
| 100 | if ($force_update || $this->headline->cacheExpired()) { |
||
| 101 | if (!$this->updateCache()) { |
||
| 102 | return $retval; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | if ($this->_parse()) { |
||
| 106 | $this->tpl->clear_all_assign(); |
||
| 107 | $this->tpl->assign('xoops_url', XOOPS_URL); |
||
| 108 | $channel_data = $this->parser->getChannelData(); |
||
| 109 | \array_walk($channel_data, [$this, 'convertFromUtf8']); |
||
| 110 | $this->tpl->assign_by_ref('channel', $channel_data); |
||
| 111 | if (1 == $this->headline->getVar('headline_mainimg')) { |
||
| 112 | $image_data = $this->parser->getImageData(); |
||
| 113 | \array_walk($image_data, [$this, 'convertFromUtf8']); |
||
| 114 | $max_width = 256; |
||
| 115 | $max_height = 92; |
||
| 116 | if (!isset($image_data['height']) || !isset($image_data['width'])) { |
||
| 117 | $image_size = @\getimagesize($image_data['url']); |
||
| 118 | if ($image_size) { |
||
| 119 | $image_data['width'] = $image_size[0]; |
||
| 120 | $image_data['height'] = $image_size[1]; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | if (\array_key_exists('height', $image_data) && \array_key_exists('width', $image_data) |
||
| 124 | && ($image_data['width'] > 0)) { |
||
| 125 | $width_ratio = $image_data['width'] / $max_width; |
||
| 126 | $height_ratio = $image_data['height'] / $max_height; |
||
| 127 | $scale = \max($width_ratio, $height_ratio); |
||
| 128 | if ($scale > 1) { |
||
| 129 | $image_data['width'] = (int)($image_data['width'] / $scale); |
||
| 130 | $image_data['height'] = (int)($image_data['height'] / $scale); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | $this->tpl->assign_by_ref('image', $image_data); |
||
| 134 | } |
||
| 135 | if (1 == $this->headline->getVar('headline_mainfull')) { |
||
| 136 | $this->tpl->assign('show_full', true); |
||
| 137 | } else { |
||
| 138 | $this->tpl->assign('show_full', false); |
||
| 139 | } |
||
| 140 | $items = $this->parser->getItems(); |
||
| 141 | $count = \count($items); |
||
| 142 | $max = ($count > $this->headline->getVar('headline_mainmax')) ? $this->headline->getVar('headline_mainmax') : $count; |
||
| 143 | for ($i = 0; $i < $max; ++$i) { |
||
| 144 | \array_walk($items[$i], [$this, 'convertFromUtf8']); |
||
| 145 | $this->tpl->append_by_ref('items', $items[$i]); |
||
| 146 | } |
||
| 147 | $this->tpl->assign( |
||
| 148 | [ |
||
| 149 | 'lang_lastbuild' => \_MD_XOOPSHEADLINE_LASTBUILD, |
||
| 150 | 'lang_language' => \_MD_XOOPSHEADLINE_LANGUAGE, |
||
| 151 | 'lang_description' => \_MD_XOOPSHEADLINE_DESCRIPTION, |
||
| 152 | 'lang_webmaster' => \_MD_XOOPSHEADLINE_WEBMASTER, |
||
| 153 | 'lang_category' => \_MD_XOOPSHEADLINE_CATEGORY, |
||
| 154 | 'lang_generator' => \_MD_XOOPSHEADLINE_GENERATOR, |
||
| 155 | 'lang_title' => \_MD_XOOPSHEADLINE_TITLE, |
||
| 156 | 'lang_pubdate' => \_MD_XOOPSHEADLINE_PUBDATE, |
||
| 157 | // 'lang_description2' => _MD_XOOPSHEADLINE_DESCRIPTION2, |
||
| 158 | 'lang_more' => _MORE, |
||
| 159 | ] |
||
| 160 | ); |
||
| 161 | $this->feed = $this->tpl->fetch('db:xoopsheadline_feed.tpl'); |
||
| 162 | $retval = true; |
||
| 163 | } |
||
| 164 | |||
| 165 | return $retval; |
||
| 166 | } |
||
| 167 | |||
| 168 | public function renderBlock(bool $force_update = false): bool |
||
| 207 | } |
||
| 208 | |||
| 209 | protected function &_parse(): bool |
||
| 210 | { |
||
| 211 | $retval = true; |
||
| 212 | if (!isset($this->parser)) { |
||
| 213 | require_once XOOPS_ROOT_PATH . '/class/xml/rss/xmlrss2parser.php'; |
||
| 214 | $temp = $this->headline->getVar('headline_xml'); |
||
| 215 | $this->parser = new \XoopsXmlRss2Parser($temp); |
||
| 216 | switch ($this->headline->getVar('headline_encoding')) { |
||
| 217 | case 'utf-8': |
||
| 218 | $this->parser->useUtfEncoding(); |
||
| 219 | break; |
||
| 220 | case 'us-ascii': |
||
| 221 | $this->parser->useAsciiEncoding(); |
||
| 222 | break; |
||
| 223 | default: |
||
| 224 | $this->parser->useIsoEncoding(); |
||
| 225 | break; |
||
| 226 | } |
||
| 227 | $result = $this->parser->parse(); |
||
| 228 | if (!$result) { |
||
| 229 | $this->setErrors($this->parser->getErrors(false)); |
||
| 230 | unset($this->parser); |
||
| 231 | $retval = false; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | return $retval; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return mixed |
||
| 240 | */ |
||
| 241 | public function &getFeed() |
||
| 242 | { |
||
| 243 | return $this->feed; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return mixed |
||
| 248 | */ |
||
| 249 | public function &getBlock() |
||
| 250 | { |
||
| 251 | return $this->block; |
||
| 252 | } |
||
| 253 | |||
| 254 | protected function setErrors(string $err): void |
||
| 255 | { |
||
| 256 | $this->errors[] = $err; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return array|string |
||
| 261 | */ |
||
| 262 | public function &getErrors(bool $ashtml = true) |
||
| 263 | { |
||
| 264 | if ($ashtml) { |
||
| 265 | $retval = ''; |
||
| 266 | if (\count($this->errors) > 0) { |
||
| 267 | foreach ($this->errors as $error) { |
||
| 268 | $retval .= $error . '<br>'; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } else { |
||
| 272 | $retval = $this->errors; |
||
| 273 | } |
||
| 274 | |||
| 275 | return $retval; |
||
| 276 | } |
||
| 277 | |||
| 278 | // abstract |
||
| 279 | // overide this method in /language/your_language/headlinerenderer.php |
||
| 280 | // this method is called by the array_walk function |
||
| 281 | // return void |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param $value |
||
| 285 | * @param $key |
||
| 286 | */ |
||
| 287 | public function convertFromUtf8(&$value, $key): void |
||
| 288 | { |
||
| 289 | } |
||
| 290 | |||
| 291 | // abstract |
||
| 292 | // overide this method in /language/your_language/headlinerenderer.php |
||
| 293 | // return string |
||
| 294 | |||
| 295 | public function &convertToUtf8(string &$xmlfile): string |
||
| 302 | } |
||
| 303 | } |
||
| 304 |