| Total Complexity | 40 |
| Total Lines | 222 |
| Duplicated Lines | 0 % |
| Changes | 15 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BlogManager 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 BlogManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class BlogManager extends CI_Model { |
||
| 5 | |||
| 6 | const TABLE_PREFIX = "blogger_posts"; |
||
| 7 | |||
| 8 | private $ci; |
||
| 9 | |||
| 10 | private $table_name; |
||
| 11 | |||
| 12 | function __construct() { |
||
| 13 | parent::__construct(); |
||
| 14 | $this->ci =& get_instance(); |
||
| 15 | $this->ci->load->database(); |
||
| 16 | } |
||
| 17 | /** |
||
| 18 | * [setBlogName description] |
||
| 19 | * @param [type] $name [description] |
||
|
|
|||
| 20 | */ |
||
| 21 | function setBlogName($name) { |
||
| 23 | } |
||
| 24 | /** |
||
| 25 | * [getBlogName description] |
||
| 26 | * @return [type] [description] |
||
| 27 | */ |
||
| 28 | function getBlogName() { |
||
| 30 | } |
||
| 31 | /** |
||
| 32 | * [createPost creates a post with the given $title and $content in the |
||
| 33 | * database.] |
||
| 34 | * |
||
| 35 | * @param string $title The title of the post. |
||
| 36 | * |
||
| 37 | * @param string $content The content of the post. |
||
| 38 | * |
||
| 39 | * @param int $adminId Optional, the id of the poster. this is needed if |
||
| 40 | * you provided an admins table name during blog |
||
| 41 | * installation. |
||
| 42 | * |
||
| 43 | * @return 0 Returns the id of the newly created post in the |
||
| 44 | * database. Returns 0 if the post couldn't be |
||
| 45 | * created. |
||
| 46 | */ |
||
| 47 | function createPost(string $title, string $content, int $adminId=null): int { |
||
| 48 | $this->ci->load->helper("url"); |
||
| 49 | $data = array( |
||
| 50 | "title" => $title, |
||
| 51 | "content" => $content, |
||
| 52 | "slug" => url_title($title) |
||
| 53 | ); |
||
| 54 | if (is_numeric($title)) $data["slug"] = "_" . $data["slug"]; |
||
| 55 | if ($adminId !== null) $data["poster_id"] = $adminId; |
||
| 56 | if ($this->db->insert($this->table_name, $data)) return $this->db->insert_id(); |
||
| 57 | return 0; |
||
| 58 | } |
||
| 59 | /** |
||
| 60 | * [createAndPublish description] |
||
| 61 | * @param [type] $title [description] |
||
| 62 | * @param [type] $content [description] |
||
| 63 | * @param [type] $adminId [description] |
||
| 64 | * @return [type] [description] |
||
| 65 | */ |
||
| 66 | function createAndPublishPost($title, $content, $adminId=null) { |
||
| 78 | } |
||
| 79 | /** |
||
| 80 | * [getPosts get posts from the database by the given $page starting from the |
||
| 81 | * value of 1 and returns $limit number of rows.] |
||
| 82 | * |
||
| 83 | * @param int $page Page number starting from 1. |
||
| 84 | * |
||
| 85 | * @param int $limit Number of posts to return. |
||
| 86 | * |
||
| 87 | * @param boolean $filter if true, returns only published posts, if false |
||
| 88 | * return all posts. false by default. |
||
| 89 | * |
||
| 90 | * @param boolean $hits order by hits. |
||
| 91 | * |
||
| 92 | * @return array Array of posts for a given page. |
||
| 93 | */ |
||
| 94 | function getPosts(int $page=1, int $limit=5, bool $filter=false, bool $hits=false): array { |
||
| 95 | if ($limit != 0) $this->db->limit($limit, ($page * $limit) - $limit); |
||
| 96 | if ($filter) $this->db->where("published", 1); |
||
| 97 | if ($hits) { |
||
| 98 | $this->db->order_by("hits", "DESC"); |
||
| 99 | } else { |
||
| 100 | $this->db->order_by("id", "DESC"); |
||
| 101 | } |
||
| 102 | return $this->db->get($this->table_name)->result_array(); |
||
| 103 | } |
||
| 104 | /** |
||
| 105 | * [getRecentPosts description] |
||
| 106 | * @param integer $limit [description] |
||
| 107 | * @param boolean $filter [description] |
||
| 108 | * @return [type] [description] |
||
| 109 | */ |
||
| 110 | function getRecentPosts($limit=5, $filter=false) { |
||
| 111 | if ($limit != null && $limit != null) $this->db->limit($limit); |
||
| 112 | if ($filter) $this->db->where("published", 1); |
||
| 113 | $this->db->order_by("id", "DESC"); |
||
| 114 | return $this->db->get($this->table_name)->result_array(); |
||
| 115 | } |
||
| 116 | /** |
||
| 117 | * [savePost saves or midfies the content of a post record given by $postId |
||
| 118 | * in the database.] |
||
| 119 | * @param int $postId ID of the post to modify. |
||
| 120 | * @param string $title New title of the post. |
||
| 121 | * @param string $content New content of the post. |
||
| 122 | * @return bool True on success, False if not. |
||
| 123 | */ |
||
| 124 | function savePost($postId, $title, $content): bool { |
||
| 125 | $data = array ( |
||
| 126 | "title" => $title, |
||
| 127 | "content" => $content, |
||
| 128 | "slug" => url_title($title) |
||
| 129 | ); |
||
| 130 | $this->db->where("id", $postId); |
||
| 131 | return $this->db->update($this->table_name, $data); |
||
| 132 | } |
||
| 133 | /** |
||
| 134 | * [getPost Gets a specific post by the given value of $postId. NB: This will |
||
| 135 | * increment the hit count of the particularpost in the database.] |
||
| 136 | * @param int $postId ID of the post to retrieve. |
||
| 137 | * @return array An associative array for the Posts's data. |
||
| 138 | */ |
||
| 139 | function getPost($postId, $hit=true) { |
||
| 140 | if (is_numeric($postId)) { |
||
| 141 | $this->db->where("id", $postId); |
||
| 142 | } else { |
||
| 143 | $this->db->where("slug", $postId); |
||
| 144 | } |
||
| 145 | $query = $this->db->get($this->table_name); |
||
| 146 | if ($query->num_rows() > 0) { |
||
| 147 | if ($hit) { |
||
| 148 | if ($this->ci->config->item("blogger_hits") === null || |
||
| 149 | $this->ci->config->item("blogger_hits") === true) { |
||
| 150 | if (!is_numeric($postId)) $postId = $query->result()[0]->id; |
||
| 151 | $this->db->where("id", $postId); |
||
| 152 | $this->db->set("hits", "hits+1", FALSE); |
||
| 153 | $this->db->update($this->table_name); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | $post = $query->result_array()[0]; |
||
| 157 | $images = array(); |
||
| 158 | // Fetch all images in post. |
||
| 159 | preg_match("/<img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>/", $post["content"], $images); |
||
| 160 | $share_image = count($images) == 0 ? null : $images[0]; |
||
| 161 | unset($images); |
||
| 162 | $src = array(); |
||
| 163 | // Get the contents of the src tag. |
||
| 164 | preg_match("/(http|https):\/\/[a-zA-Z0-9-._\/]+/", $share_image, $src); |
||
| 165 | if (count($src) == 0) return $post; |
||
| 166 | $post["share_image"] = $src[0]; |
||
| 167 | return $post; |
||
| 168 | } |
||
| 169 | return false; |
||
| 170 | } |
||
| 171 | /** |
||
| 172 | * [getHits description] |
||
| 173 | * @param [type] $postId [description] |
||
| 174 | * @return [type] [description] |
||
| 175 | */ |
||
| 176 | function getHits(int $postId): int { |
||
| 177 | $this->db->where("id", $postId); |
||
| 178 | $query = $this->db->get($this->table_name); |
||
| 179 | if ($query->num_rows() > 0) return $query->result()[0]->hits; |
||
| 180 | return 0; |
||
| 181 | } |
||
| 182 | /** |
||
| 183 | * [publishPost description] |
||
| 184 | * @param [type] $postId [description] |
||
| 185 | * @param [type] $publish [description] |
||
| 186 | * @return [type] [description] |
||
| 187 | */ |
||
| 188 | function publishPost($postId, $publish) { |
||
| 193 | } |
||
| 194 | /** |
||
| 195 | * [getPostsCount get the total number of posts in the database.] |
||
| 196 | * @return [type] [description] |
||
| 197 | */ |
||
| 198 | function getPostsCount(bool $filter=true): int { |
||
| 199 | if ($filter) $this->db->where("published", 1); |
||
| 200 | $this->db->select("COUNT(title) as posts"); |
||
| 201 | return (int) $this->db->get($this->table_name)->result()[0]->posts; |
||
| 202 | } |
||
| 203 | /** |
||
| 204 | * [deletePost description] |
||
| 205 | * @param [type] $postId [description] |
||
| 206 | * @return [type] [description] |
||
| 207 | */ |
||
| 208 | function deletePost($postId) { |
||
| 209 | $this->db->where("id", $postId); |
||
| 210 | return $this->db->delete($this->table_name); |
||
| 211 | } |
||
| 212 | /** |
||
| 213 | * [searchPosts description] |
||
| 214 | * @param [type] $words [description] |
||
| 215 | * @param [type] $page [description] |
||
| 216 | * @param integer $limit [description] |
||
| 217 | * @param boolean $filter [description] |
||
| 218 | * @return [type] [description] |
||
| 219 | */ |
||
| 220 | function searchPosts($words, $page=1, $limit=5, $filter=false) { |
||
| 226 | } |
||
| 227 | } |
||
| 228 |