| Total Complexity | 51 |
| Total Lines | 323 |
| Duplicated Lines | 0 % |
| Changes | 17 | ||
| Bugs | 0 | Features | 2 |
Complex classes like Blogger 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 Blogger, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class Blogger { |
||
| 5 | |||
| 6 | private $ci; |
||
| 7 | |||
| 8 | private $dbforge; |
||
| 9 | |||
| 10 | private $table_name; |
||
| 11 | |||
| 12 | const TABLE_PREFIX = "blogger_posts"; |
||
| 13 | |||
| 14 | const PACKAGE = "francis94c/blog"; |
||
| 15 | |||
| 16 | const MARKDOWN_PACKAGE = "francis94c/ci-parsedown"; |
||
| 17 | |||
| 18 | const CREATE = "create"; |
||
| 19 | |||
| 20 | const CREATE_AND_PUBLISH = "createAndPublish"; |
||
| 21 | |||
| 22 | const EDIT = "edit"; |
||
| 23 | |||
| 24 | const PUBLISH = "publish"; |
||
| 25 | |||
| 26 | const DELETE = "delete"; |
||
| 27 | |||
| 28 | const ABORT = "abortAction"; |
||
| 29 | |||
| 30 | function __construct($params=null) { |
||
| 31 | $this->ci =& get_instance(); |
||
|
1 ignored issue
–
show
|
|||
| 32 | $this->ci->load->database(); |
||
| 33 | $this->table_name = self::TABLE_PREFIX . (isset($params["name"]) ? "_" . $params["name"] : ""); |
||
| 34 | $this->ci->load->database(); |
||
| 35 | $this->ci->load->splint(self::PACKAGE, "*BlogManager", "bmanager"); |
||
| 36 | $this->ci->load->splint(self::MARKDOWN_PACKAGE, "+Parsedown", null, "parsedown"); |
||
| 37 | $this->ci->bmanager->setBlogName(isset($params["name"]) ? $params["name"] : null); |
||
| 38 | $this->ci->load->helper("url"); |
||
| 39 | } |
||
| 40 | /** |
||
| 41 | * [install description] |
||
| 42 | * @param [type] $adminTableName [description] |
||
| 43 | * @param [type] $adminTableName [description] |
||
| 44 | * @param [type] $adminIdColumnName [description] |
||
| 45 | * @param [type] $adminIdColumnConstraint [description] |
||
| 46 | * @return [type] [description] |
||
| 47 | */ |
||
| 48 | public function install($blogName=null, $adminTableName = null, $adminIdColumnName = null, $adminIdColumnConstraint = null) { |
||
| 93 | } |
||
| 94 | /** |
||
| 95 | * [setBlog description] |
||
| 96 | * @param [type] $name [description] |
||
| 97 | * @deprecated |
||
| 98 | */ |
||
| 99 | public function setName($name) { |
||
| 100 | $this->table_name = self::TABLE_PREFIX . "_" . $name; |
||
| 101 | $this->ci->bmanager->setBlogName($name != "" ? $name : null); |
||
| 102 | } |
||
| 103 | /** |
||
| 104 | * [setBlog description] |
||
| 105 | * @param [type] $blog [description] |
||
| 106 | */ |
||
| 107 | public function setBlog($blog) { |
||
| 108 | $this->table_name = self::TABLE_PREFIX . "_" . $blog; |
||
| 109 | $this->ci->bmanager->setBlogName($blog != "" ? $blog : null); |
||
| 110 | } |
||
| 111 | /** |
||
| 112 | * [getBlog description] |
||
| 113 | * @return [type] [description] |
||
| 114 | */ |
||
| 115 | public function getName() { |
||
| 116 | return $this->table_name; |
||
| 117 | } |
||
| 118 | /** |
||
| 119 | * [loadHeaderScripts description] |
||
| 120 | * @param boolean $w3css [description] |
||
| 121 | * @return [type] [description] |
||
| 122 | */ |
||
| 123 | private function loadScripts($w3css) { |
||
| 124 | $this->ci->load->splint(self::PACKAGE, "-header_scripts", array( |
||
| 125 | "w3css" => $w3css |
||
| 126 | )); |
||
| 127 | } |
||
| 128 | /** |
||
| 129 | * [w3css description] |
||
| 130 | * @return [type] [description] |
||
| 131 | */ |
||
| 132 | public function w3css() { |
||
| 133 | return "<link rel=\"stylesheet\" href=\"https://www.w3schools.com/w3css/4/w3.css\">"; |
||
| 134 | } |
||
| 135 | /** |
||
| 136 | * [fontsAwesome description] |
||
| 137 | * @return [type] [description] |
||
| 138 | */ |
||
| 139 | public function fontsAwesome() { |
||
| 140 | return "<link rel=\"stylesheet\" href=\"https://use.fontawesome.com/releases/v5.3.1/css/all.css\""; |
||
| 141 | } |
||
| 142 | /** |
||
| 143 | * [loadEditor description] |
||
| 144 | * @param [type] $callback [description] |
||
| 145 | * @param [type] $postId [description] |
||
| 146 | * @param boolean $w3css [description] |
||
| 147 | * @return [type] [description] |
||
| 148 | */ |
||
| 149 | public function loadEditor($callback, $postId=null, $w3css=true) { |
||
| 150 | $this->loadScripts($w3css); |
||
| 151 | $this->ci->load->helper("form"); |
||
| 152 | $data = array( |
||
| 153 | "callback" => "Admin/token", |
||
| 154 | "type" => $postId == null ? "create" : "edit", |
||
| 155 | "callback" => $callback |
||
| 156 | ); |
||
| 157 | if ($postId != null) { |
||
| 158 | $data["id"] = $postId; |
||
| 159 | $post = $this->getPost($postId, false); |
||
| 160 | $data["title"] = $post["title"]; |
||
| 161 | $data["content"] = $post["content"]; |
||
| 162 | } |
||
| 163 | $this->ci->load->splint("francis94c/blog", "-post_edit", $data); |
||
| 164 | return true; |
||
| 165 | } |
||
| 166 | /** |
||
| 167 | * [savePost description] |
||
| 168 | * @param [type] $posterId [description] |
||
| 169 | * @return [type] [description] |
||
| 170 | */ |
||
| 171 | public function savePost($posterId=null) { |
||
| 172 | $action = $this->ci->security->xss_clean($this->ci->input->post("action")); |
||
| 173 | if ($action == "save") { |
||
| 174 | $id = $this->ci->security->xss_clean($this->ci->input->post("id")); |
||
| 175 | if ($id != "") { |
||
| 176 | $this->ci->bmanager->savePost($this->ci->input->post("id"), $this->ci->security->xss_clean($this->ci->input->post("title")), $this->ci->security->xss_clean($this->ci->input->post("editor")), $posterId); |
||
| 177 | return self::EDIT; |
||
| 178 | } else { |
||
| 179 | $this->ci->bmanager->createPost($this->ci->security->xss_clean($this->ci->input->post("title")), $this->ci->security->xss_clean($this->ci->input->post("editor")), $posterId); |
||
| 180 | return self::CREATE; |
||
| 181 | } |
||
| 182 | } elseif ($action == "publish" || $action == "createAndPublish") { |
||
| 183 | if ($action == "publish") { |
||
| 184 | $id = $this->ci->security->xss_clean($this->ci->input->post("id")); |
||
| 185 | if ($id == "") return self::ABORT; |
||
| 186 | $this->ci->bmanager->savePost($id, $this->ci->security->xss_clean($this->ci->input->post("title")), $this->ci->security->xss_clean($this->ci->input->post("editor")), $posterId); |
||
| 187 | $this->ci->bmanager->publishPost($id, true); |
||
| 188 | return self::PUBLISH; |
||
| 189 | } else { |
||
| 190 | $this->ci->bmanager->createAndPublishPost($this->ci->security->xss_clean($this->ci->input->post("title")), $this->ci->security->xss_clean($this->ci->input->post("editor")), $posterId); |
||
| 191 | return self::CREATE_AND_PUBLISH; |
||
| 192 | } |
||
| 193 | } elseif ($action == "delete") { |
||
| 194 | if ($this->ci->bmanager->deletePost($this->ci->security->xss_clean($this->ci->input->post("id")))) return self::DELETE; |
||
| 195 | } |
||
| 196 | return false; |
||
| 197 | } |
||
| 198 | /** |
||
| 199 | * [getPosts get posts from the database by the given $page starting from the |
||
| 200 | * value of 1 and returns $limit number of rows.] |
||
| 201 | * @param int $page Page number starting from 1. |
||
| 202 | * @param int $limit Number of posts to return. |
||
| 203 | * @param boolean $filter if true, returns only published posts, if false |
||
| 204 | * return all posts. false by default. |
||
| 205 | * @return array Array of posts for a given page. |
||
| 206 | */ |
||
| 207 | public function getPosts($page, $limit, $filter=false, $hits=false) { |
||
| 208 | return $this->ci->bmanager->getPosts($page, $limit, $filter, $hits); |
||
| 209 | } |
||
| 210 | /** |
||
| 211 | * [renderPosts description] |
||
| 212 | * @param [type] $view [description] |
||
| 213 | * @param [type] $empty_view [description] |
||
| 214 | * @param [type] $page [description] |
||
| 215 | * @param [type] $limit [description] |
||
| 216 | * @param boolean $filter [description] |
||
| 217 | * @param boolean $hits [description] |
||
| 218 | * @return [type] [description] |
||
| 219 | */ |
||
| 220 | public function renderPostItems($view=null, $callback=null, $empty_view=null, $page=1, $limit=5, $filter=false, $hits=false, $slug=true) { |
||
| 221 | if ($view == null || $empty_view == null) $this->ci->load->bind("francis94c/blog", $blogger); |
||
|
1 ignored issue
–
show
|
|||
| 222 | $posts = $this->getPosts($page, $limit, $filter, $hits); |
||
| 223 | if (count($posts) == 0) { |
||
| 224 | if ($empty_view == null) { $blogger->load->view("empty"); } else { |
||
| 225 | $this->ci->load->view($empty_view); |
||
| 226 | return true; |
||
| 227 | } |
||
| 228 | } |
||
| 229 | $this->ci->load->helper("text"); |
||
| 230 | foreach ($posts as $post) { |
||
| 231 | $post["callback"] = $callback != null ? trim($callback, "/") . "/" . ($slug ? $post["slug"] : $post["id"]) : ""; |
||
| 232 | $post["filter"] = $filter; |
||
| 233 | $post["content"] = $this->ci->parsedown->text(ellipsize($post["content"], 300)); |
||
|
1 ignored issue
–
show
|
|||
| 234 | if ($view == null) {$blogger->load->view("post_list_item", $post); } else { |
||
| 235 | $this->ci->load->view($view, $post); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | return true; |
||
| 239 | } |
||
| 240 | /** |
||
| 241 | * [getRecentPosts description] |
||
| 242 | * @param integer $limit [description] |
||
| 243 | * @param boolean $filter [description] |
||
| 244 | * @return [type] [description] |
||
| 245 | */ |
||
| 246 | public function getRecentPosts($limit=5, $filter=false) { |
||
| 247 | return $this->ci->bmanager->getRecentPosts($limit, $filter); |
||
| 248 | } |
||
| 249 | /** |
||
| 250 | * [renderPost description] |
||
| 251 | * @param [type] $post [description] |
||
| 252 | * @param [type] $view [description] |
||
| 253 | * @return [type] [description] |
||
| 254 | */ |
||
| 255 | public function renderPost($post, $view=null) { |
||
| 256 | if (!is_array($post)) $post = $this->ci->bmanager->getPost($post); |
||
| 257 | $post["content"] = $this->ci->parsedown->text($post["content"]); |
||
| 258 | if ($view == null) { |
||
| 259 | $this->ci->load->splint("francis94c/blog", "-post_item", $post); |
||
| 260 | } else { |
||
| 261 | $this->ci->load->view($view, $post); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | /** |
||
| 265 | * [metaOg description] |
||
| 266 | * @param [type] $post [description] |
||
| 267 | * @return [type] [description] |
||
| 268 | */ |
||
| 269 | public function metaOg($post) { |
||
| 270 | $data = array(); |
||
| 271 | $data["title"] = $post["title"]; |
||
| 272 | $data["description"] = substr($post["content"], 0, 154); |
||
| 273 | if (isset($post["share_image"])) $data["image_link"] = $post["share_image"]; |
||
| 274 | $data["url"] = current_url(); |
||
|
1 ignored issue
–
show
|
|||
| 275 | return $this->ci->load->splint(self::PACKAGE, "-meta_og", $data, true); |
||
| 276 | } |
||
| 277 | /** |
||
| 278 | * [getPostsCount description] |
||
| 279 | * @return [type] [description] |
||
| 280 | */ |
||
| 281 | public function getPostsCount() { |
||
| 282 | return $this->ci->bmanager->getPostsCount(); |
||
| 283 | } |
||
| 284 | /** |
||
| 285 | * [getPost description] |
||
| 286 | * @param [type] $postId [description] |
||
| 287 | * @return [type] [description] |
||
| 288 | */ |
||
| 289 | public function getPost($postId, $hit=true) { |
||
| 290 | return $this->ci->bmanager->getPost($postId, $hit); |
||
| 291 | } |
||
| 292 | /** |
||
| 293 | * [getHits description] |
||
| 294 | * @param [type] $postId [description] |
||
| 295 | * @return [type] [description] |
||
| 296 | */ |
||
| 297 | public function getHits($postId) { |
||
| 299 | } |
||
| 300 | /** |
||
| 301 | * [publishPost description] |
||
| 302 | * @param [type] $postId [description] |
||
| 303 | * @param [type] $publish [description] |
||
| 304 | * @return [type] [description] |
||
| 305 | */ |
||
| 306 | public function publishPost($postId, $publish) { |
||
| 307 | return $this->ci->bmanager->publishPost($postId, $publish); |
||
| 308 | } |
||
| 309 | /** |
||
| 310 | * [deletePost description] |
||
| 311 | * @param [type] $postId [description] |
||
| 312 | * @return [type] [description] |
||
| 313 | */ |
||
| 314 | public function deletePost($postId) { |
||
| 316 | } |
||
| 317 | /** |
||
| 318 | * [searchPosts description] |
||
| 319 | * @param [type] $words [description] |
||
| 320 | * @param [type] $page [description] |
||
| 321 | * @param integer $limit [description] |
||
| 322 | * @param boolean $filter [description] |
||
| 323 | * @return [type] [description] |
||
| 324 | */ |
||
| 325 | public function searchPosts($words, $page, $limit=0, $filter=false) { |
||
| 326 | return $this->ci->bmanager->searchPosts($words, $page, $limit, $filter); |
||
| 327 | } |
||
| 328 | } |
||
| 329 |