| Total Complexity | 50 |
| Total Lines | 325 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Topic 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 Topic, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Topic { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * DB handle |
||
| 14 | */ |
||
| 15 | private $db; |
||
| 16 | |||
| 17 | private $raw; |
||
|
|
|||
| 18 | private $id; |
||
| 19 | private $title; |
||
| 20 | private $slug; |
||
| 21 | private $description; |
||
| 22 | private $image; |
||
| 23 | private $search_string; |
||
| 24 | private $front_page; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Constructor |
||
| 28 | * |
||
| 29 | */ |
||
| 30 | |||
| 31 | public function __construct($data = NULL) |
||
| 32 | { |
||
| 33 | $this->db = new \ParlDB; |
||
| 34 | |||
| 35 | if (is_null($data)) { |
||
| 36 | return; |
||
| 37 | } |
||
| 38 | |||
| 39 | $this->id = $data['id']; |
||
| 40 | $this->title = $data['title']; |
||
| 41 | $this->slug = $data['slug']; |
||
| 42 | $this->description = $data['description']; |
||
| 43 | $this->search_string = $data['search_string']; |
||
| 44 | $this->front_page = $data['front_page']; |
||
| 45 | $this->image = $data['image']; |
||
| 46 | |||
| 47 | } |
||
| 48 | |||
| 49 | |||
| 50 | function title() { |
||
| 51 | return $this->title; |
||
| 52 | } |
||
| 53 | |||
| 54 | function sctitle() { |
||
| 55 | $title = $this->title; |
||
| 56 | if (strpos($title, 'The ') === 0 ) { |
||
| 57 | $title = lcfirst($title); |
||
| 58 | } |
||
| 59 | |||
| 60 | return $title; |
||
| 61 | } |
||
| 62 | |||
| 63 | function set_title($title) { |
||
| 64 | $this->title = $title; |
||
| 65 | } |
||
| 66 | |||
| 67 | function slug() { |
||
| 68 | return $this->slug; |
||
| 69 | } |
||
| 70 | |||
| 71 | function set_slug($slug) { |
||
| 72 | $this->slug = $slug; |
||
| 73 | } |
||
| 74 | |||
| 75 | function url() { |
||
| 76 | $url = new Url('topic'); |
||
| 77 | return $url->generate() . $this->slug; |
||
| 78 | } |
||
| 79 | |||
| 80 | function image() { |
||
| 81 | return $this->image; |
||
| 82 | } |
||
| 83 | |||
| 84 | function image_url() { |
||
| 85 | return "/topic/image.php?id=" . $this->slug(); |
||
| 86 | } |
||
| 87 | |||
| 88 | function image_path() { |
||
| 94 | } |
||
| 95 | |||
| 96 | function set_image($image) { |
||
| 97 | $this->image = $image; |
||
| 98 | } |
||
| 99 | |||
| 100 | |||
| 101 | function description() { |
||
| 103 | } |
||
| 104 | |||
| 105 | function set_description($description) { |
||
| 107 | } |
||
| 108 | |||
| 109 | function search_string() { |
||
| 110 | return $this->search_string; |
||
| 111 | } |
||
| 112 | |||
| 113 | function set_search_string($search_string) { |
||
| 114 | $this->search_string = $search_string; |
||
| 115 | } |
||
| 116 | |||
| 117 | function set_front_page($on) { |
||
| 118 | $this->front_page = $on; |
||
| 119 | } |
||
| 120 | |||
| 121 | function onFrontPage() { |
||
| 122 | return $this->front_page == 1; |
||
| 123 | } |
||
| 124 | |||
| 125 | private function _getContentIDs() { |
||
| 126 | $q = $this->db->query( |
||
| 127 | "SELECT body, gid, ep.epobject_id FROM epobject ep |
||
| 128 | JOIN hansard h on ep.epobject_id = h.epobject_id |
||
| 129 | JOIN topic_epobjects te on te.epobject_id = ep.epobject_id |
||
| 130 | WHERE topic_key = :topic_key", |
||
| 131 | array( |
||
| 132 | ':topic_key' => $this->id |
||
| 133 | ) |
||
| 134 | ); |
||
| 135 | |||
| 136 | return $q; |
||
| 137 | } |
||
| 138 | |||
| 139 | function getContent() { |
||
| 140 | $q = $this->_getContentIDs(); |
||
| 141 | |||
| 142 | $content = array(); |
||
| 143 | $rows = $q->rows; |
||
| 144 | for ($i = 0; $i < $rows; $i++) { |
||
| 145 | $content[] = array( |
||
| 146 | 'title' => $q->field($i, 'body'), |
||
| 147 | 'href' => Utility\Hansard::gid_to_url($q->field($i, 'gid')), |
||
| 148 | 'id' => $q->field($i, 'epobject_id'), |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | |||
| 152 | return $content; |
||
| 153 | } |
||
| 154 | |||
| 155 | function getFullContent() { |
||
| 179 | } |
||
| 180 | |||
| 181 | function addContent($gid) { |
||
| 182 | $q = $this->db->query( |
||
| 183 | "SELECT epobject_id FROM hansard WHERE gid = :gid", |
||
| 184 | array( |
||
| 185 | ":gid" => $gid |
||
| 186 | ) |
||
| 187 | ); |
||
| 188 | |||
| 189 | if (!$q->success() || $q->rows == 0) { |
||
| 190 | return false; |
||
| 191 | } |
||
| 192 | |||
| 193 | $epobject_id = $q->field(0, 'epobject_id'); |
||
| 194 | |||
| 195 | $q = $this->db->query( |
||
| 196 | "INSERT INTO topic_epobjects (topic_key, epobject_id) VALUES (:topic, :ep_id)", |
||
| 197 | array( |
||
| 198 | ":topic" => $this->id, |
||
| 199 | ":ep_id" => $epobject_id |
||
| 200 | ) |
||
| 201 | ); |
||
| 202 | |||
| 203 | return $q->success(); |
||
| 204 | } |
||
| 205 | |||
| 206 | function deleteContent($id) { |
||
| 207 | $q = $this->db->query( |
||
| 208 | "DELETE FROM topic_epobjects WHERE topic_key = :topic AND epobject_id = :ep_id", |
||
| 209 | array( |
||
| 210 | ":topic" => $this->id, |
||
| 211 | ":ep_id" => $id |
||
| 212 | ) |
||
| 213 | ); |
||
| 214 | |||
| 215 | return $q->success(); |
||
| 216 | } |
||
| 217 | |||
| 218 | function getPolicySets() { |
||
| 219 | $q = $this->db->query( |
||
| 220 | "SELECT policyset FROM topic_policysets WHERE topic_key = :key", |
||
| 221 | array( |
||
| 222 | ':key' => $this->id |
||
| 223 | ) |
||
| 224 | ); |
||
| 225 | |||
| 226 | $sets = array(); |
||
| 227 | $count = $q->rows; |
||
| 228 | for ($i = 0; $i < $count; $i++) { |
||
| 229 | $sets[] = $q->field($i, 'policyset'); |
||
| 230 | } |
||
| 231 | |||
| 232 | return $sets; |
||
| 233 | } |
||
| 234 | |||
| 235 | function addPolicySets($sets) { |
||
| 236 | if ($sets === '' or count($sets) == 0) { |
||
| 237 | $q = $this->db->query( |
||
| 238 | "DELETE FROM topic_policysets WHERE topic_key = :topic_key", |
||
| 239 | array( |
||
| 240 | ":topic_key" => $this->id |
||
| 241 | ) |
||
| 242 | ); |
||
| 243 | } else { |
||
| 244 | foreach ($sets as $set) { |
||
| 245 | if ($set == '' ) { |
||
| 246 | continue; |
||
| 247 | } |
||
| 248 | $q = $this->db->query( |
||
| 249 | "REPLACE INTO topic_policysets (policyset, topic_key) VALUES (:policyset, :topic_key)", |
||
| 250 | array( |
||
| 251 | ':topic_key' => $this->id, |
||
| 252 | ':policyset' => $set |
||
| 253 | ) |
||
| 254 | ); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | return $q->success(); |
||
| 259 | } |
||
| 260 | |||
| 261 | function getPolicies() { |
||
| 262 | $q = $this->db->query( |
||
| 263 | 'SELECT policy_id FROM topic_policies WHERE topic_key = :key', |
||
| 264 | array( |
||
| 265 | ':key' => $this->id |
||
| 266 | ) |
||
| 267 | ); |
||
| 268 | |||
| 269 | $policies = array(); |
||
| 270 | $count = $q->rows; |
||
| 271 | for ($i = 0; $i < $count; $i++) { |
||
| 272 | $policies[] = $q->field($i, 'policy_id'); |
||
| 273 | } |
||
| 274 | |||
| 275 | return $policies; |
||
| 276 | } |
||
| 277 | |||
| 278 | function addPolicies($policies) { |
||
| 279 | if ($policies === '' or count($policies) == 0) { |
||
| 280 | $q = $this->db->query( |
||
| 281 | "DELETE FROM topic_policies WHERE topic_key = :topic_key", |
||
| 282 | array( |
||
| 283 | ":topic_key" => $this->id |
||
| 284 | ) |
||
| 285 | ); |
||
| 286 | } else { |
||
| 287 | foreach ($policies as $policy) { |
||
| 288 | if ($policy == '' ) { |
||
| 289 | continue; |
||
| 290 | } |
||
| 291 | $q = $this->db->query( |
||
| 292 | "REPLACE INTO topic_policies (policy_id, topic_key) VALUES (:policy, :topic_key)", |
||
| 293 | array( |
||
| 294 | ':topic_key' => $this->id, |
||
| 295 | ':policy' => $policy |
||
| 296 | ) |
||
| 297 | ); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | return $q->success(); |
||
| 302 | } |
||
| 303 | |||
| 304 | function getAllPolicies() { |
||
| 305 | $policy_sets = $this->getPolicySets(); |
||
| 306 | $all_policies = array(); |
||
| 307 | $policies = new Policies(); |
||
| 308 | foreach ($policy_sets as $set) { |
||
| 309 | $all_policies = array_merge($all_policies, array_keys($policies->limitToSet($set)->getPolicies())); |
||
| 310 | } |
||
| 311 | $topic_policies = $this->getPolicies(); |
||
| 312 | $all_policies = array_merge($all_policies, $topic_policies); |
||
| 313 | |||
| 314 | return array_unique($all_policies); |
||
| 315 | } |
||
| 316 | |||
| 317 | function save() { |
||
| 335 | } |
||
| 336 | } |
||
| 337 |