| Total Complexity | 54 |
| Total Lines | 226 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DraftValidator 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 DraftValidator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class DraftValidator { |
||
| 12 | private $app; |
||
| 13 | |||
| 14 | public function __construct(Application $app) { |
||
| 15 | $this->app = $app; |
||
| 16 | } |
||
| 17 | |||
| 18 | public function IsDraftViewableForUser($draft_id, Request $request) { |
||
| 19 | $draft = $this->app['phpdraft.DraftRepository']->Load($draft_id); |
||
| 20 | $current_user = $this->app['phpdraft.LoginUserService']->GetUserFromHeaderToken($request); |
||
| 21 | $draft_password = $request->headers->get(DRAFT_PASSWORD_HEADER, ''); |
||
|
|
|||
| 22 | |||
| 23 | if ($current_user != null && ($draft->commish_id == $current_user->id || $this->app['phpdraft.LoginUserService']->CurrentUserIsAdmin($current_user))) { |
||
| 24 | return true; |
||
| 25 | } |
||
| 26 | |||
| 27 | if (empty($draft->draft_password) || ($draft->draft_password == $draft_password)) { |
||
| 28 | return true; |
||
| 29 | } |
||
| 30 | |||
| 31 | return false; |
||
| 32 | } |
||
| 33 | |||
| 34 | public function IsDraftEditableForUser(Draft $draft, LoginUser $current_user) { |
||
| 35 | if (!empty($current_user) && !empty($draft) && $draft->commish_id == $current_user->id) { |
||
| 36 | return true; |
||
| 37 | } |
||
| 38 | |||
| 39 | if (!empty($current_user) && $this->app['phpdraft.LoginUserService']->CurrentUserIsAdmin($current_user)) { |
||
| 40 | return true; |
||
| 41 | } |
||
| 42 | |||
| 43 | return false; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function IsDraftValidForCreateAndUpdate(Draft $draft) { |
||
| 47 | $valid = true; |
||
| 48 | $errors = array(); |
||
| 49 | $draft_sports = $this->app['phpdraft.DraftDataRepository']->GetSports(); |
||
| 50 | $draft_styles = $this->app['phpdraft.DraftDataRepository']->GetStyles(); |
||
| 51 | |||
| 52 | if (empty($draft->commish_id) |
||
| 53 | || empty($draft->draft_name) |
||
| 54 | || empty($draft->draft_sport) |
||
| 55 | || empty($draft->draft_style)) { |
||
| 56 | $errors[] = "One or more missing fields."; |
||
| 57 | $valid = false; |
||
| 58 | } |
||
| 59 | |||
| 60 | if (!empty($draft->draft_password) && strlen($draft->draft_password) > 255) { |
||
| 61 | $errors[] = "Password is above maximum length."; |
||
| 62 | $valid = false; |
||
| 63 | } |
||
| 64 | |||
| 65 | if (strlen($draft->draft_name) > 255) { |
||
| 66 | $errors[] = "Draft name is above maximum length"; |
||
| 67 | $valid = false; |
||
| 68 | } |
||
| 69 | |||
| 70 | if (strlen($draft->draft_sport) < 3 || strlen($draft->draft_sport) > 4 || strlen($draft_sports[$draft->draft_sport]) == 0) { |
||
| 71 | $errors[] = "Draft sport is an invalid value."; |
||
| 72 | $valid = false; |
||
| 73 | } |
||
| 74 | |||
| 75 | if (!array_key_exists($draft->draft_style, $draft_styles)) { |
||
| 76 | |||
| 77 | $errors[] = "Draft style is an invalid value."; |
||
| 78 | $valid = false; |
||
| 79 | } |
||
| 80 | |||
| 81 | if (!$this->app['phpdraft.DraftRepository']->NameIsUnique($draft->draft_name, $draft->draft_id)) { |
||
| 82 | $errors[] = "Draft name is already taken, please choose a different name."; |
||
| 83 | $valid = false; |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($draft->draft_rounds < 1 || $draft->draft_rounds > 30) { |
||
| 87 | $errors[] = "Invalid number of draft rounds."; |
||
| 88 | $valid = false; |
||
| 89 | } |
||
| 90 | |||
| 91 | return new PhpDraftResponse($valid, $errors); |
||
| 92 | } |
||
| 93 | |||
| 94 | public function IsDraftStatusValid(Draft $draft, $old_status) { |
||
| 95 | $valid = true; |
||
| 96 | $errors = array(); |
||
| 97 | $draft_statuses = $this->app['phpdraft.DraftDataRepository']->GetStatuses(); |
||
| 98 | |||
| 99 | if ($old_status == "complete") { |
||
| 100 | $valid = false; |
||
| 101 | $errors[] = "The draft is completed, therefore its status cannot be changed."; |
||
| 102 | } |
||
| 103 | |||
| 104 | if ($draft->draft_status == "complete") { |
||
| 105 | $valid = false; |
||
| 106 | $errors[] = "You cannot set the draft as completed manually."; |
||
| 107 | } |
||
| 108 | |||
| 109 | if (empty($draft->draft_status)) { |
||
| 110 | $errors[] = "One or more missing fields."; |
||
| 111 | $valid = false; |
||
| 112 | } |
||
| 113 | |||
| 114 | if (!array_key_exists($draft->draft_status, $draft_statuses)) { |
||
| 115 | $errors[] = "Draft status is an invalid value."; |
||
| 116 | $valid = false; |
||
| 117 | } |
||
| 118 | |||
| 119 | if ($draft->draft_status == "in_progress" && !$this->app['phpdraft.ManagerRepository']->DraftHasManagers($draft->draft_id)) { |
||
| 120 | $valid = false; |
||
| 121 | $errors[] = "A draft must have at least 2 managers before it can begin."; |
||
| 122 | } |
||
| 123 | |||
| 124 | return new PhpDraftResponse($valid, $errors); |
||
| 125 | } |
||
| 126 | |||
| 127 | public function IsDraftSettingUp(Draft $draft) { |
||
| 128 | $valid = true; |
||
| 129 | $errors = array(); |
||
| 130 | $draft_statuses = $this->app['phpdraft.DraftDataRepository']->GetStatuses(); |
||
| 131 | $current_status_text = strtolower($draft_statuses[$draft->draft_status]); |
||
| 132 | $is_setting_up = $this->app['phpdraft.DraftService']->DraftSettingUp($draft); |
||
| 133 | |||
| 134 | if (!$is_setting_up) { |
||
| 135 | $valid = false; |
||
| 136 | $errors[] = "Unable to work on draft #$draft->draft_id: draft is $current_status_text"; |
||
| 137 | } |
||
| 138 | |||
| 139 | return new PhpDraftResponse($valid, $errors); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function IsDraftSettingUpOrInProgress(Draft $draft) { |
||
| 156 | } |
||
| 157 | |||
| 158 | public function IsDraftInProgress(Draft $draft) { |
||
| 159 | $valid = true; |
||
| 160 | $errors = array(); |
||
| 161 | $draft_statuses = $this->app['phpdraft.DraftDataRepository']->GetStatuses(); |
||
| 162 | $current_status_text = strtolower($draft_statuses[$draft->draft_status]); |
||
| 163 | $is_in_progress = $this->app['phpdraft.DraftService']->DraftInProgress($draft); |
||
| 164 | |||
| 165 | if (!$is_in_progress) { |
||
| 166 | $valid = false; |
||
| 167 | $errors[] = "Unable to work on draft #$draft->draft_id: draft is $current_status_text"; |
||
| 168 | } |
||
| 169 | |||
| 170 | return new PhpDraftResponse($valid, $errors); |
||
| 171 | } |
||
| 172 | |||
| 173 | public function IsDraftInProgressOrComplete(Draft $draft) { |
||
| 174 | $valid = true; |
||
| 175 | $errors = array(); |
||
| 176 | $draft_statuses = $this->app['phpdraft.DraftDataRepository']->GetStatuses(); |
||
| 177 | $current_status_text = strtolower($draft_statuses[$draft->draft_status]); |
||
| 178 | $is_in_progress = $this->app['phpdraft.DraftService']->DraftInProgress($draft); |
||
| 179 | $is_complete = $this->app['phpdraft.DraftService']->DraftComplete($draft); |
||
| 180 | |||
| 181 | if (!$is_complete && !$is_in_progress) { |
||
| 182 | $valid = false; |
||
| 183 | $errors[] = "Unable to work on draft #$draft->draft_id: draft is $current_status_text"; |
||
| 184 | } |
||
| 185 | |||
| 186 | return new PhpDraftResponse($valid, $errors); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function IsDraftInProgressOrCompletedInLessThanTen(Draft $draft) { |
||
| 222 | } |
||
| 223 | |||
| 224 | public function IsDraftComplete(Draft $draft) { |
||
| 225 | $valid = true; |
||
| 226 | $errors = array(); |
||
| 237 | } |
||
| 238 | } |
||
| 239 |