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