| Total Complexity | 58 |
| Total Lines | 456 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DraftRepository 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 DraftRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class DraftRepository { |
||
| 10 | private $app; |
||
| 11 | |||
| 12 | public function __construct(Application $app) { |
||
| 14 | } |
||
| 15 | |||
| 16 | public function GetPublicDrafts(Request $request, $password = '') { |
||
| 17 | $draft_stmt = $this->app['db']->prepare("SELECT d.*, u.Name AS commish_name FROM draft d |
||
| 18 | LEFT OUTER JOIN users u |
||
| 19 | ON d.commish_id = u.id |
||
| 20 | ORDER BY draft_create_time DESC"); |
||
| 21 | |||
| 22 | $draft_stmt->setFetchMode(\PDO::FETCH_CLASS, '\PhpDraft\Domain\Entities\Draft'); |
||
| 23 | |||
| 24 | $currentUser = $this->app['phpdraft.LoginUserService']->GetUserFromHeaderToken($request); |
||
| 25 | |||
| 26 | if (!$draft_stmt->execute()) { |
||
| 27 | throw new \Exception("Unable to load drafts."); |
||
| 28 | } |
||
| 29 | |||
| 30 | $drafts = array(); |
||
| 31 | |||
| 32 | while ($draft = $draft_stmt->fetch()) { |
||
| 33 | $draft = $this->NormalizeDraftTimesAndStatuses($draft); |
||
| 34 | $draft = $this->SetSecurityProperties($currentUser, $draft, $password); |
||
| 35 | |||
| 36 | $drafts[] = $draft; |
||
| 37 | } |
||
| 38 | |||
| 39 | return $drafts; |
||
| 40 | } |
||
| 41 | |||
| 42 | public function GetPublicDraftsByCommish(Request $request, $commish_id, $password = '') { |
||
| 43 | $commish_id = (int)$commish_id; |
||
| 44 | |||
| 45 | $draftStatement = $this->app['db']->prepare("SELECT d.*, u.Name AS commish_name FROM draft d |
||
| 46 | LEFT OUTER JOIN users u |
||
| 47 | ON d.commish_id = u.id |
||
| 48 | WHERE commish_id = ? |
||
| 49 | ORDER BY draft_create_time DESC"); |
||
| 50 | |||
| 51 | $draftStatement->setFetchMode(\PDO::FETCH_CLASS, '\PhpDraft\Domain\Entities\Draft'); |
||
| 52 | $draftStatement->bindParam(1, $commish_id); |
||
| 53 | |||
| 54 | if (!$draftStatement->execute()) { |
||
| 55 | throw new \Exception("Unable to load drafts."); |
||
| 56 | } |
||
| 57 | |||
| 58 | $currentUser = $this->app['phpdraft.LoginUserService']->GetUserFromHeaderToken($request); |
||
| 59 | |||
| 60 | $drafts = array(); |
||
| 61 | |||
| 62 | while ($draft = $draftStatement->fetch()) { |
||
| 63 | $draft = $this->NormalizeDraftTimesAndStatuses($draft); |
||
| 64 | $draft = $this->SetSecurityProperties($currentUser, $draft, $password); |
||
| 65 | |||
| 66 | $drafts[] = $draft; |
||
| 67 | } |
||
| 68 | |||
| 69 | return $drafts; |
||
| 70 | } |
||
| 71 | |||
| 72 | //Note: this method is to be used by admin section only |
||
| 73 | public function GetAllDraftsByCommish($commish_id) { |
||
| 100 | } |
||
| 101 | |||
| 102 | //Note: this method is to be used by admin section only |
||
| 103 | public function GetAllCompletedDrafts() { |
||
| 126 | } |
||
| 127 | |||
| 128 | public function GetPublicDraft(Request $request, $id, $getDraftData = false, $password = '') { |
||
| 129 | $cachedDraft = $this->GetCachedDraft($id); |
||
| 130 | |||
| 131 | if ($cachedDraft != null) { |
||
| 132 | $draft = $cachedDraft; |
||
| 133 | } else { |
||
| 134 | $draft = $this->FetchPublicDraftById($id); |
||
| 135 | |||
| 136 | $this->SetCachedDraft($draft); |
||
| 137 | } |
||
| 138 | |||
| 139 | $currentUser = $this->app['phpdraft.LoginUserService']->GetUserFromHeaderToken($request); |
||
| 140 | |||
| 141 | $draft = $this->NormalizeDraftTimesAndStatuses($draft); |
||
| 142 | $draft = $this->SetSecurityProperties($currentUser, $draft, $password); |
||
| 143 | |||
| 144 | if ($getDraftData) { |
||
| 145 | $draft->sports = $this->app['phpdraft.DraftDataRepository']->GetSports(); |
||
| 146 | $draft->styles = $this->app['phpdraft.DraftDataRepository']->GetStyles(); |
||
| 147 | $draft->statuses = $this->app['phpdraft.DraftDataRepository']->GetStatuses(); |
||
| 148 | $draft->teams = $this->app['phpdraft.DraftDataRepository']->GetTeams($draft->draft_sport); |
||
| 149 | $draft->historical_teams = $this->app['phpdraft.DraftDataRepository']->GetHistoricalTeams($draft->draft_sport); |
||
| 150 | $draft->positions = $this->app['phpdraft.DraftDataRepository']->GetPositions($draft->draft_sport); |
||
| 151 | |||
| 152 | if ($draft->using_depth_charts) { |
||
| 153 | $draft->depthChartPositions = $this->app['phpdraft.DepthChartPositionRepository']->LoadAll($draft->draft_id); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | return $draft; |
||
| 158 | } |
||
| 159 | |||
| 160 | /* |
||
| 161 | * This method is only to be used internally or when the user has been verified as owner of the draft (or is admin) |
||
| 162 | * (in other words, don't call this then return the result as JSON!) |
||
| 163 | */ |
||
| 164 | public function Load($id, $bustCache = false) { |
||
| 165 | $cachedDraft = $this->GetCachedDraft($id); |
||
| 166 | |||
| 167 | if ($bustCache || $cachedDraft == null) { |
||
| 168 | $draft = $this->FetchPublicDraftById($id); |
||
| 169 | |||
| 170 | if ($bustCache) { |
||
| 171 | $this->UnsetCachedDraft($draft->draft_id); |
||
| 172 | } |
||
| 173 | |||
| 174 | $this->SetCachedDraft($draft); |
||
| 175 | } else { |
||
| 176 | $draft = $cachedDraft; |
||
| 177 | } |
||
| 178 | |||
| 179 | $draft->draft_rounds = (int)$draft->draft_rounds; |
||
| 180 | |||
| 181 | return $draft; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function Create(Draft $draft) { |
||
| 185 | $insert_stmt = $this->app['db']->prepare("INSERT INTO draft |
||
| 186 | (draft_id, commish_id, draft_create_time, draft_name, draft_sport, draft_status, draft_style, draft_rounds, draft_password, using_depth_charts) |
||
| 187 | VALUES |
||
| 188 | (NULL, ?, UTC_TIMESTAMP(), ?, ?, ?, ?, ?, ?, ?)"); |
||
| 189 | |||
| 190 | $insert_stmt->bindParam(1, $draft->commish_id); |
||
| 191 | $insert_stmt->bindParam(2, $draft->draft_name); |
||
| 192 | $insert_stmt->bindParam(3, $draft->draft_sport); |
||
| 193 | $insert_stmt->bindParam(4, $draft->draft_status); |
||
| 194 | $insert_stmt->bindParam(5, $draft->draft_style); |
||
| 195 | $insert_stmt->bindParam(6, $draft->draft_rounds); |
||
| 196 | $insert_stmt->bindParam(7, $draft->draft_password); |
||
| 197 | $insert_stmt->bindParam(8, $draft->using_depth_charts); |
||
| 198 | |||
| 199 | if (!$insert_stmt->execute()) { |
||
| 200 | throw new \Exception("Unable to create draft."); |
||
| 201 | } |
||
| 202 | |||
| 203 | $draft = $this->Load((int)$this->app['db']->lastInsertId(), true); |
||
| 204 | |||
| 205 | return $draft; |
||
| 206 | } |
||
| 207 | |||
| 208 | //Excluded properties in update: |
||
| 209 | //draft_start_time/draft_end_time - updated in separate operations at start/end of draft |
||
| 210 | //draft_current_round/draft_current_pick - updated when new picks are made |
||
| 211 | //draft_counter - call IncrementDraftCounter instead - this call's made a lot independently of other properties. |
||
| 212 | //draft_status - separate API call to update the draft status |
||
| 213 | public function Update(Draft $draft) { |
||
| 214 | $update_stmt = $this->app['db']->prepare("UPDATE draft |
||
| 215 | SET commish_id = ?, draft_name = ?, draft_sport = ?, |
||
| 216 | draft_style = ?, draft_password = ?, draft_rounds = ?, |
||
| 217 | using_depth_charts = ? |
||
| 218 | WHERE draft_id = ?"); |
||
| 219 | |||
| 220 | $draft->using_depth_charts = $draft->using_depth_charts; |
||
| 221 | |||
| 222 | $update_stmt->bindParam(1, $draft->commish_id); |
||
| 223 | $update_stmt->bindParam(2, $draft->draft_name); |
||
| 224 | $update_stmt->bindParam(3, $draft->draft_sport); |
||
| 225 | $update_stmt->bindParam(4, $draft->draft_style); |
||
| 226 | $update_stmt->bindParam(5, $draft->draft_password); |
||
| 227 | $update_stmt->bindParam(6, $draft->draft_rounds); |
||
| 228 | $update_stmt->bindParam(7, $draft->using_depth_charts); |
||
| 229 | $update_stmt->bindParam(8, $draft->draft_id); |
||
| 230 | |||
| 231 | if (!$update_stmt->execute()) { |
||
| 232 | throw new \Exception("Unable to update draft."); |
||
| 233 | } |
||
| 234 | |||
| 235 | $this->ResetDraftCache($draft->draft_id); |
||
| 236 | |||
| 237 | return $draft; |
||
| 238 | } |
||
| 239 | |||
| 240 | public function UpdateStatus(Draft $draft) { |
||
| 254 | } |
||
| 255 | |||
| 256 | public function UpdateStatsTimestamp(Draft $draft) { |
||
| 257 | $status_stmt = $this->app['db']->prepare("UPDATE draft |
||
| 258 | SET draft_stats_generated = UTC_TIMESTAMP() WHERE draft_id = ?"); |
||
| 259 | |||
| 260 | $status_stmt->bindParam(1, $draft->draft_id); |
||
| 261 | |||
| 262 | if (!$status_stmt->execute()) { |
||
| 263 | throw new \Exception("Unable to update draft's stats timestamp."); |
||
| 264 | } |
||
| 265 | |||
| 266 | $this->ResetDraftCache($draft->draft_id); |
||
| 267 | |||
| 268 | return $draft; |
||
| 269 | } |
||
| 270 | |||
| 271 | public function IncrementDraftCounter(Draft $draft) { |
||
| 272 | $incrementedCounter = (int)$draft->draft_counter + 1; |
||
| 273 | |||
| 274 | $increment_stmt = $this->app['db']->prepare("UPDATE draft |
||
| 275 | SET draft_counter = ? WHERE draft_id = ?"); |
||
| 276 | |||
| 277 | $increment_stmt->bindParam(1, $incrementedCounter); |
||
| 278 | $increment_stmt->bindParam(2, $draft->draft_id); |
||
| 279 | |||
| 280 | if (!$increment_stmt->execute()) { |
||
| 281 | throw new \Exception("Unable to increment draft counter."); |
||
| 282 | } |
||
| 283 | |||
| 284 | $this->ResetDraftCache($draft->draft_id); |
||
| 285 | |||
| 286 | return $incrementedCounter; |
||
| 287 | } |
||
| 288 | |||
| 289 | //$next_pick can't be type-hinted - can be null |
||
| 290 | public function MoveDraftForward(Draft $draft, $next_pick) { |
||
| 291 | if ($next_pick !== null) { |
||
| 292 | $draft->draft_current_pick = (int)$next_pick->player_pick; |
||
| 293 | $draft->draft_current_round = (int)$next_pick->player_round; |
||
| 294 | |||
| 295 | $stmt = $this->app['db']->prepare("UPDATE draft SET draft_current_pick = ?, draft_current_round = ? WHERE draft_id = ?"); |
||
| 296 | $stmt->bindParam(1, $draft->draft_current_pick); |
||
| 297 | $stmt->bindParam(2, $draft->draft_current_round); |
||
| 298 | $stmt->bindParam(3, $draft->draft_id); |
||
| 299 | |||
| 300 | if (!$stmt->execute()) { |
||
| 301 | throw new \Exception("Unable to move draft forward."); |
||
| 302 | } |
||
| 303 | } else { |
||
| 304 | $draft->draft_status = 'complete'; |
||
| 305 | $stmt = $this->app['db']->prepare("UPDATE draft SET draft_status = ?, draft_end_time = UTC_TIMESTAMP() WHERE draft_id = ?"); |
||
| 306 | $stmt->bindParam(1, $draft->draft_status); |
||
| 307 | $stmt->bindParam(2, $draft->draft_id); |
||
| 308 | |||
| 309 | if (!$stmt->execute()) { |
||
| 310 | throw new \Exception("Unable to move draft forward."); |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | $this->ResetDraftCache($draft->draft_id); |
||
| 315 | |||
| 316 | return $draft; |
||
| 317 | } |
||
| 318 | |||
| 319 | //Used when we move a draft from "undrafted" to "in_progress": |
||
| 320 | //Resets the draft counter |
||
| 321 | //Sets the current pick and round to 1 |
||
| 322 | //Sets the draft start time to UTC now, nulls out end time |
||
| 323 | public function SetDraftInProgress(Draft $draft) { |
||
| 324 | $reset_stmt = $this->app['db']->prepare("UPDATE draft |
||
| 325 | SET draft_counter = 0, draft_current_pick = 1, draft_current_round = 1, |
||
| 326 | draft_start_time = UTC_TIMESTAMP(), draft_end_time = NULL |
||
| 327 | WHERE draft_id = ?"); |
||
| 328 | |||
| 329 | $reset_stmt->bindParam(1, $draft->draft_id); |
||
| 330 | |||
| 331 | if (!$reset_stmt->execute()) { |
||
| 332 | throw new \Exception("Unable to set draft to in progress."); |
||
| 333 | } |
||
| 334 | |||
| 335 | $this->ResetDraftCache($draft->draft_id); |
||
| 336 | |||
| 337 | return 0; |
||
| 338 | } |
||
| 339 | |||
| 340 | public function NameIsUnique($name, $id = null) { |
||
| 355 | } |
||
| 356 | |||
| 357 | public function DeleteDraft($draft_id) { |
||
| 358 | $delete_stmt = $this->app['db']->prepare("DELETE FROM draft WHERE draft_id = ?"); |
||
| 359 | $delete_stmt->bindParam(1, $draft_id); |
||
| 360 | |||
| 361 | if (!$delete_stmt->execute()) { |
||
| 362 | throw new \Exception("Unable to delete draft $draft_id."); |
||
| 363 | } |
||
| 364 | |||
| 365 | $this->UnsetCachedDraft($draft_id); |
||
| 366 | |||
| 367 | return; |
||
| 368 | } |
||
| 369 | |||
| 370 | private function ResetDraftCache($draft_id) { |
||
| 371 | $draft = $this->Load($draft_id, true); |
||
|
|
|||
| 372 | } |
||
| 373 | |||
| 374 | private function SetCachedDraft(Draft $draft) { |
||
| 376 | } |
||
| 377 | |||
| 378 | private function GetCachedDraft($draft_id) { |
||
| 379 | return $this->app['phpdraft.DatabaseCacheService']->GetCachedItem("draft$draft_id"); |
||
| 380 | } |
||
| 381 | |||
| 382 | private function UnsetCachedDraft($draft_id) { |
||
| 384 | } |
||
| 385 | |||
| 386 | private function ProtectPrivateDraft(Draft $draft) { |
||
| 387 | $draft->draft_sport = ''; |
||
| 388 | $draft->setting_up = ''; |
||
| 389 | $draft->in_progress = ''; |
||
| 390 | $draft->complete = ''; |
||
| 391 | $draft->draft_style = ''; |
||
| 392 | $draft->draft_rounds = ''; |
||
| 393 | $draft->draft_counter = ''; |
||
| 394 | $draft->draft_start_time = null; |
||
| 395 | $draft->draft_end_time = null; |
||
| 396 | $draft->draft_current_pick = ''; |
||
| 397 | $draft->draft_current_round = ''; |
||
| 398 | $draft->draft_create_time = ''; |
||
| 399 | $draft->draft_stats_generated = ''; |
||
| 400 | $draft->nfl_extended = null; |
||
| 401 | $draft->sports = null; |
||
| 402 | $draft->styles = null; |
||
| 403 | $draft->statuses = null; |
||
| 404 | $draft->teams = null; |
||
| 405 | $draft->positions = null; |
||
| 406 | $draft->using_depth_charts = null; |
||
| 407 | $draft->depthChartPositions = null; |
||
| 408 | |||
| 409 | return $draft; |
||
| 410 | } |
||
| 411 | |||
| 412 | private function FetchPublicDraftById($id) { |
||
| 413 | $draft = new Draft(); |
||
| 414 | |||
| 415 | $draft_stmt = $this->app['db']->prepare("SELECT d.*, u.Name AS commish_name FROM draft d |
||
| 416 | LEFT OUTER JOIN users u |
||
| 417 | ON d.commish_id = u.id |
||
| 418 | WHERE d.draft_id = ? LIMIT 1"); |
||
| 419 | |||
| 420 | $draft_stmt->setFetchMode(\PDO::FETCH_INTO, $draft); |
||
| 421 | |||
| 422 | $draft_stmt->bindParam(1, $id, \PDO::PARAM_INT); |
||
| 423 | |||
| 424 | if (!$draft_stmt->execute() || !$draft_stmt->fetch()) { |
||
| 425 | throw new \Exception("Unable to load draft"); |
||
| 426 | } |
||
| 427 | |||
| 428 | $draft->using_depth_charts = $draft->using_depth_charts == 1; |
||
| 429 | |||
| 430 | return $draft; |
||
| 431 | } |
||
| 432 | |||
| 433 | private function NormalizeDraftTimesAndStatuses($draft) { |
||
| 434 | $draft->setting_up = $this->app['phpdraft.DraftService']->DraftSettingUp($draft); |
||
| 435 | $draft->in_progress = $this->app['phpdraft.DraftService']->DraftInProgress($draft); |
||
| 436 | $draft->complete = $this->app['phpdraft.DraftService']->DraftComplete($draft); |
||
| 437 | |||
| 438 | $draft->display_status = $this->app['phpdraft.DraftService']->GetDraftStatusDisplay($draft); |
||
| 439 | |||
| 440 | $draft->draft_create_time = $this->app['phpdraft.UtilityService']->ConvertTimeForClientDisplay($draft->draft_create_time); |
||
| 441 | $draft->draft_start_time = $this->app['phpdraft.UtilityService']->ConvertTimeForClientDisplay($draft->draft_start_time); |
||
| 442 | $draft->draft_end_time = $this->app['phpdraft.UtilityService']->ConvertTimeForClientDisplay($draft->draft_end_time); |
||
| 443 | |||
| 444 | return $draft; |
||
| 445 | } |
||
| 446 | |||
| 447 | private function SetSecurityProperties($currentUser, $draft, $password) { |
||
| 465 | } |
||
| 466 | } |
||
| 467 |