| Total Complexity | 56 |
| Total Lines | 416 |
| Duplicated Lines | 0 % |
| Changes | 23 | ||
| 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 |
||
| 5 | class Blogger { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Code Igniter Instance |
||
| 9 | * @var object |
||
| 10 | */ |
||
| 11 | private $ci; |
||
| 12 | /** |
||
| 13 | * Code Igniter DB Forge instance reference for simplicity. |
||
| 14 | * @var object |
||
| 15 | */ |
||
| 16 | private $dbforge; |
||
| 17 | /** |
||
| 18 | * Current Blog Table Name. |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private $table_name; |
||
| 22 | /** |
||
| 23 | * String prefixed with ever blog name. |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | const TABLE_PREFIX = "blogger_posts"; |
||
| 27 | /** |
||
| 28 | * Name of this package for simplicity. |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | const PACKAGE = "francis94c/blog"; |
||
| 32 | /** |
||
| 33 | * Name of the dependent package for markdown. |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | const MARKDOWN_PACKAGE = "francis94c/ci-parsedown"; |
||
| 37 | /** |
||
| 38 | * Blog post create action. |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | const CREATE = "create"; |
||
| 42 | /** |
||
| 43 | * Blog post create and publish action. |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | const CREATE_AND_PUBLISH = "createAndPublish"; |
||
| 47 | /** |
||
| 48 | * Blog post edit action. |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | const EDIT = "edit"; |
||
| 52 | /** |
||
| 53 | * Blog post publish action. |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | const PUBLISH = "publish"; |
||
| 57 | /** |
||
| 58 | * Blog post delete action. |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | const DELETE = "delete"; |
||
| 62 | /** |
||
| 63 | * Blog post abort acction. This is an action taken internally when other |
||
| 64 | * actions fail. |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | const ABORT = "abortAction"; |
||
| 68 | /** |
||
| 69 | * Blog post no action. |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | const NO_ACTION = "no_action"; |
||
| 73 | /** |
||
| 74 | * Constructor |
||
| 75 | * @param mixed $params associative array of parameters. See README.md |
||
| 76 | */ |
||
| 77 | function __construct($params=null) { |
||
| 86 | } |
||
| 87 | /** |
||
| 88 | * Installs a blog with the given table name and paramters. |
||
| 89 | * |
||
| 90 | * @param string $blogName Name of blog tabke to install. |
||
| 91 | * |
||
| 92 | * @param string $adminTableName Name of admi table to restrict post to. |
||
| 93 | * |
||
| 94 | * @param string $adminIdColumnName Name of the column to add a foreign |
||
| 95 | * key constarint to the blog table with. |
||
| 96 | * |
||
| 97 | * @param int $adminIdColumnConstraint The column constarint or limit of |
||
| 98 | * $adminIdColumnName. |
||
| 99 | * |
||
| 100 | * @return bool True on Success, False if Not. |
||
| 101 | */ |
||
| 102 | public function install(string $blogName=null, string $adminTableName=null, string $adminIdColumnName=null, int $adminIdColumnConstraint=null): bool { |
||
| 103 | $blogName = $blogName === null ? $this->table_name : self::TABLE_PREFIX . "_" . $blogName; |
||
| 104 | $this->ci->load->dbforge(); |
||
| 105 | $this->ci->dbforge->add_field("id"); |
||
| 106 | $fields = array( |
||
| 107 | "title" => array( |
||
| 108 | "type" => "VARCHAR", |
||
| 109 | "constraint" => 70, |
||
| 110 | ), |
||
| 111 | "content" => array( |
||
| 112 | "type" => "TEXT" |
||
| 113 | ), |
||
| 114 | "date_published" => array( |
||
| 115 | "type" => "TIMESTAMP", |
||
| 116 | "null" => true |
||
| 117 | ), |
||
| 118 | "published" => array( |
||
| 119 | "type" => "TINYINT", |
||
| 120 | "default" => 0 |
||
| 121 | ), |
||
| 122 | "hits" => array( |
||
| 123 | "type" => "INT", |
||
| 124 | "constraint" => 7, |
||
| 125 | "default" => 0 |
||
| 126 | ), |
||
| 127 | "slug" => array( |
||
| 128 | "type" => "VARCHAR", |
||
| 129 | "constraint" => 80, |
||
| 130 | "unique" => true |
||
| 131 | ) |
||
| 132 | ); |
||
| 133 | $this->ci->dbforge->add_field($fields); |
||
| 134 | $constrain = $adminTableName !== null && $adminIdColumnName !== null && |
||
| 135 | $adminIdColumnConstraint !== null; |
||
| 136 | if ($constrain) { |
||
| 137 | $this->ci->dbforge->add_field( |
||
| 138 | "poster_id INT($adminIdColumnConstraint), FOREIGN KEY (poster_id) REFERENCES $adminTableName($adminIdColumnName)"); |
||
| 139 | } |
||
| 140 | $this->ci->dbforge->add_field("date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP"); |
||
| 141 | $attributes = array('ENGINE' => 'InnoDB'); |
||
| 142 | if (!$this->ci->dbforge->create_table($blogName, true, $attributes)) return false; |
||
| 143 | return true; |
||
| 144 | } |
||
| 145 | /** |
||
| 146 | * Sets the name of the current blog table. |
||
| 147 | * |
||
| 148 | * @param string $name name of a blog table. |
||
| 149 | * |
||
| 150 | * @deprecated |
||
| 151 | */ |
||
| 152 | public function setName(string $name): void { |
||
| 153 | $this->table_name = self::TABLE_PREFIX . "_" . $name; |
||
| 154 | $this->ci->bmanager->setBlogName($name != "" ? $name : null); |
||
| 155 | } |
||
| 156 | /** |
||
| 157 | * Same as the deprecated setName. Sets the name of the current blog table. |
||
| 158 | * |
||
| 159 | * @param string $blog [description] |
||
| 160 | */ |
||
| 161 | public function setBlog(string $blog): void { |
||
| 162 | $this->table_name = self::TABLE_PREFIX . "_" . $blog; |
||
| 163 | $this->ci->bmanager->setBlogName($blog != "" ? $blog : null); |
||
| 164 | } |
||
| 165 | /** |
||
| 166 | * Gets the name of the blog. |
||
| 167 | * @return string The name of the blog. |
||
| 168 | */ |
||
| 169 | public function getName(): string { |
||
| 170 | return $this->table_name; |
||
| 171 | } |
||
| 172 | /** |
||
| 173 | * Loads/echoes the client side scripts needed for the blog to render it's |
||
| 174 | * post editor and other views. |
||
| 175 | * |
||
| 176 | * @param bool $w3css If true, additionally loads the W3.CSS file for additional |
||
| 177 | * styling. Defaults internally on Blogmanager to true |
||
| 178 | */ |
||
| 179 | private function loadScripts(bool $w3css): void { |
||
| 180 | $this->ci->load->splint(self::PACKAGE, "-header_scripts", array( |
||
| 181 | "w3css" => $w3css |
||
| 182 | )); |
||
| 183 | } |
||
| 184 | /** |
||
| 185 | * Returns the W3.CSS client side script loading tag. |
||
| 186 | * @return string W3.CSS link tag. |
||
| 187 | */ |
||
| 188 | public function w3css(): string { |
||
| 189 | return "<link rel=\"stylesheet\" href=\"https://www.w3schools.com/w3css/4/w3.css\">"; |
||
| 190 | } |
||
| 191 | /** |
||
| 192 | * Returns the Fonts Awesome CSS link loading tag. |
||
| 193 | * @return string Fonts Awesome CSS link loading tag. |
||
| 194 | */ |
||
| 195 | public function fontsAwesome(): string { |
||
| 196 | return "<link rel=\"stylesheet\" href=\"https://use.fontawesome.com/releases/v5.3.1/css/all.css\""; |
||
| 197 | } |
||
| 198 | /** |
||
| 199 | * Echoes to the browser a 'SimpleMDE' markdown editor for editing post |
||
| 200 | * contents, as part of a form. |
||
| 201 | * |
||
| 202 | * @param string $callback The URI callback that will be passed to the Code |
||
| 203 | * Igniter form_open method when outputing the form. |
||
| 204 | * The call back is where you should read the contents |
||
| 205 | * of the submited form. |
||
| 206 | * The contents of the form should be read or handled |
||
| 207 | * by a call to the 'savePost($posterId)' function. |
||
| 208 | * You don't need to worry about reading it your self. |
||
| 209 | * |
||
| 210 | * @param int $postId (Optional) The ID of the post whose content should |
||
| 211 | * be present in the editor when loaded. provide this |
||
| 212 | * parameter when you want to edit a post. |
||
| 213 | * |
||
| 214 | * @param bool $w3css If true, echoes the W3.CSS link tag as well. |
||
| 215 | * |
||
| 216 | * @return bool True if sucessfull without errors, false if not. |
||
| 217 | */ |
||
| 218 | public function loadEditor(string $callback, int $postId=null, bool $w3css=true): bool { |
||
| 219 | $this->loadScripts($w3css); |
||
| 220 | $this->ci->load->helper("form"); |
||
| 221 | $data = array( |
||
| 222 | "callback" => "Admin/token", |
||
| 223 | "type" => $postId === null ? "create" : "edit", |
||
| 224 | "callback" => $callback |
||
| 225 | ); |
||
| 226 | if ($postId !== null) { |
||
| 227 | $data["id"] = $postId; |
||
| 228 | $post = $this->getPost($postId, false); |
||
| 229 | $data["title"] = $post["title"]; |
||
| 230 | $data["content"] = $post["content"]; |
||
| 231 | } |
||
| 232 | $this->ci->load->splint("francis94c/blog", "-post_edit", $data); |
||
| 233 | return true; |
||
| 234 | } |
||
| 235 | /** |
||
| 236 | * Handles form data from the Editor loaded by a call to 'loadEditor'. |
||
| 237 | * Traditionally, this function is to be called ath the controller function |
||
| 238 | * specified by the callback URI provided to the loadEditor method. |
||
| 239 | * |
||
| 240 | * @param int $posterId ID of the poster. A valid admin ID from the table |
||
| 241 | * specified as a foreign key constraint during the |
||
| 242 | * installation of the selected blog. |
||
| 243 | * |
||
| 244 | * @return string The final action reached in processing the form |
||
| 245 | * inputs. These are public string constants declared in |
||
| 246 | * this file. |
||
| 247 | */ |
||
| 248 | public function savePost(int $posterId=null): string { |
||
| 249 | $action = $this->ci->security->xss_clean($this->ci->input->post("action")); |
||
| 250 | if ($action == "save") { |
||
| 251 | return $this->handleSavePost(); |
||
| 252 | } elseif ($action == "publish" || $action == "createAndPublish") { |
||
| 253 | if ($action == "publish") { |
||
| 254 | $id = $this->ci->security->xss_clean($this->ci->input->post("id")); |
||
| 255 | if ($id == "") return self::ABORT; |
||
| 256 | $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); |
||
| 257 | $this->ci->bmanager->publishPost($id, true); |
||
| 258 | return self::PUBLISH; |
||
| 259 | } else { |
||
| 260 | if ($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) !== false) { |
||
| 261 | return self::CREATE_AND_PUBLISH; |
||
| 262 | } |
||
| 263 | return self::ABORT; |
||
| 264 | } |
||
| 265 | } elseif ($action == "delete") { |
||
| 266 | if ($this->ci->bmanager->deletePost($this->ci->security->xss_clean($this->ci->input->post("id")))) return self::DELETE; |
||
| 267 | } |
||
| 268 | return self::NO_ACTION; |
||
| 269 | } |
||
| 270 | /** |
||
| 271 | * [handleSavePost description] |
||
| 272 | * @return string [description] |
||
| 273 | */ |
||
| 274 | private function handleSavePost(): string { |
||
| 275 | $id = $this->ci->security->xss_clean($this->ci->input->post("id")); |
||
| 276 | if ($id != "") { |
||
| 277 | if (!$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)) return self::ABORT; |
||
|
|
|||
| 278 | return self::EDIT; |
||
| 279 | } else { |
||
| 280 | if ($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) == 0) return self::ABORT; |
||
| 281 | return self::CREATE; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | /** |
||
| 285 | * getPosts get posts from the database by the given $page starting from the |
||
| 286 | * value of 1 and returns $limit number of rows. |
||
| 287 | * |
||
| 288 | * @param int $page Page number starting from 1. |
||
| 289 | * |
||
| 290 | * @param int $limit Number of posts to return. |
||
| 291 | * |
||
| 292 | * @param bool $filter if true, returns only published posts, if false |
||
| 293 | * return all posts. false by default. |
||
| 294 | * |
||
| 295 | * @param bool $hits If truem orders the returned posts by number of hits. |
||
| 296 | * |
||
| 297 | * @return array Array of posts for a given page. |
||
| 298 | */ |
||
| 299 | public function getPosts(int $page, int $limit, bool $filter=false, bool $hits=false): array { |
||
| 300 | return $this->ci->bmanager->getPosts($page, $limit, $filter, $hits); |
||
| 301 | } |
||
| 302 | /** |
||
| 303 | * [renderPosts description] |
||
| 304 | * @param [type] $view [description] |
||
| 305 | * @param [type] $empty_view [description] |
||
| 306 | * @param [type] $page [description] |
||
| 307 | * @param [type] $limit [description] |
||
| 308 | * @param boolean $filter [description] |
||
| 309 | * @param boolean $hits [description] |
||
| 310 | * @return [type] [description] |
||
| 311 | */ |
||
| 312 | public function renderPostItems($view=null, $callback=null, $empty_view=null, $page=1, $limit=5, $filter=false, $hits=false, $slug=true) { |
||
| 313 | if ($view == null || $empty_view == null) $this->ci->load->bind("francis94c/blog", $blogger); |
||
| 314 | $posts = $this->getPosts($page, $limit, $filter, $hits); |
||
| 315 | if (count($posts) == 0) { |
||
| 316 | if ($empty_view == null) { $blogger->load->view("empty"); } else { |
||
| 317 | $this->ci->load->view($empty_view); |
||
| 318 | return true; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | $this->ci->load->helper("text"); |
||
| 322 | foreach ($posts as $post) { |
||
| 323 | $post["callback"] = $callback != null ? trim($callback, "/") . "/" . ($slug ? $post["slug"] : $post["id"]) : ""; |
||
| 324 | $post["filter"] = $filter; |
||
| 325 | $post["content"] = $this->ci->parsedown->text(ellipsize($post["content"], 300)); |
||
| 326 | if ($view == null) {$blogger->load->view("post_list_item", $post); } else { |
||
| 327 | $this->ci->load->view($view, $post); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | return true; |
||
| 331 | } |
||
| 332 | /** |
||
| 333 | * [getRecentPosts description] |
||
| 334 | * @param integer $limit [description] |
||
| 335 | * @param boolean $filter [description] |
||
| 336 | * @return [type] [description] |
||
| 337 | */ |
||
| 338 | public function getRecentPosts($limit=5, $filter=false) { |
||
| 339 | return $this->ci->bmanager->getRecentPosts($limit, $filter); |
||
| 340 | } |
||
| 341 | /** |
||
| 342 | * [renderPost description] |
||
| 343 | * @param [type] $post [description] |
||
| 344 | * @param [type] $view [description] |
||
| 345 | * @return [type] [description] |
||
| 346 | */ |
||
| 347 | public function renderPost($post, $view=null) { |
||
| 348 | if (!is_array($post)) $post = $this->ci->bmanager->getPost($post); |
||
| 349 | if (!$post) return false; |
||
| 350 | $post["content"] = $this->ci->parsedown->text($post["content"]); |
||
| 351 | if ($view == null) { |
||
| 352 | $this->ci->load->splint("francis94c/blog", "-post_item", $post); |
||
| 353 | } else { |
||
| 354 | $this->ci->load->view($view, $post); |
||
| 355 | } |
||
| 356 | return true; |
||
| 357 | } |
||
| 358 | /** |
||
| 359 | * [metaOg description] |
||
| 360 | * @param [type] $post [description] |
||
| 361 | * @return [type] [description] |
||
| 362 | */ |
||
| 363 | public function metaOg($post) { |
||
| 364 | $data = array(); |
||
| 365 | $data["title"] = $post["title"]; |
||
| 366 | $data["description"] = substr($post["content"], 0, 154); |
||
| 367 | if (isset($post["share_image"])) $data["image_link"] = $post["share_image"]; |
||
| 368 | $data["url"] = current_url(); |
||
|
1 ignored issue
–
show
|
|||
| 369 | return $this->ci->load->splint(self::PACKAGE, "-meta_og", $data, true); |
||
| 370 | } |
||
| 371 | /** |
||
| 372 | * [getPostsCount description] |
||
| 373 | * @return [type] [description] |
||
| 374 | */ |
||
| 375 | public function getPostsCount() { |
||
| 376 | return $this->ci->bmanager->getPostsCount(); |
||
| 377 | } |
||
| 378 | /** |
||
| 379 | * [getPost description] |
||
| 380 | * @param [type] $postId [description] |
||
| 381 | * @return [type] [description] |
||
| 382 | */ |
||
| 383 | public function getPost($postId, $hit=true) { |
||
| 384 | return $this->ci->bmanager->getPost($postId, $hit); |
||
| 385 | } |
||
| 386 | /** |
||
| 387 | * [getHits description] |
||
| 388 | * @param [type] $postId [description] |
||
| 389 | * @return [type] [description] |
||
| 390 | */ |
||
| 391 | public function getHits($postId) { |
||
| 393 | } |
||
| 394 | /** |
||
| 395 | * [publishPost description] |
||
| 396 | * @param [type] $postId [description] |
||
| 397 | * @param [type] $publish [description] |
||
| 398 | * @return [type] [description] |
||
| 399 | */ |
||
| 400 | public function publishPost($postId, $publish) { |
||
| 401 | return $this->ci->bmanager->publishPost($postId, $publish); |
||
| 402 | } |
||
| 403 | /** |
||
| 404 | * [deletePost description] |
||
| 405 | * @param [type] $postId [description] |
||
| 406 | * @return [type] [description] |
||
| 407 | */ |
||
| 408 | public function deletePost($postId) { |
||
| 410 | } |
||
| 411 | /** |
||
| 412 | * [searchPosts description] |
||
| 413 | * @param [type] $words [description] |
||
| 414 | * @param [type] $page [description] |
||
| 415 | * @param integer $limit [description] |
||
| 416 | * @param boolean $filter [description] |
||
| 417 | * @return [type] [description] |
||
| 418 | */ |
||
| 419 | public function searchPosts($words, $page, $limit=0, $filter=false) { |
||
| 420 | return $this->ci->bmanager->searchPosts($words, $page, $limit, $filter); |
||
| 421 | } |
||
| 422 | } |
||
| 423 |