Total Complexity | 55 |
Total Lines | 314 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Donor 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 Donor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Donor extends Entity |
||
27 | { |
||
28 | use EntityMeta; |
||
29 | |||
30 | /** |
||
31 | * Donor ID |
||
32 | * |
||
33 | * @var int |
||
34 | * @since 1.0 |
||
35 | * |
||
36 | * @Id |
||
37 | * @GeneratedValue |
||
38 | * @Column(type="integer", name="donor_id") |
||
39 | */ |
||
40 | protected $id = 0; |
||
41 | |||
42 | /** |
||
43 | * Donor name |
||
44 | * |
||
45 | * @var string |
||
46 | * @since 1.0 |
||
47 | * |
||
48 | * @Column(type="string", name="donor_name") |
||
49 | */ |
||
50 | protected $name; |
||
51 | |||
52 | /** |
||
53 | * Donor gender |
||
54 | * |
||
55 | * @var string |
||
56 | * @since 1.0 |
||
57 | * |
||
58 | * @Column(type="string", name="donor_gender") |
||
59 | */ |
||
60 | protected $gender; |
||
61 | |||
62 | /** |
||
63 | * Donor birthdate |
||
64 | * |
||
65 | * @var string |
||
66 | * @since 1.0 |
||
67 | * |
||
68 | * @Column(type="string", name="donor_birthdate") |
||
69 | */ |
||
70 | protected $birthdate; |
||
71 | |||
72 | /** |
||
73 | * Donor blood group |
||
74 | * |
||
75 | * @var string |
||
76 | * @since 1.0 |
||
77 | * |
||
78 | * @Column(type="string", name="donor_blood_group") |
||
79 | */ |
||
80 | protected $blood_group; |
||
81 | |||
82 | /** |
||
83 | * Donor district |
||
84 | * |
||
85 | * @var District |
||
86 | * @since 1.0 |
||
87 | * |
||
88 | * @ManyToOne(targetEntity="EBloodBank\Models\District", inversedBy="donors") |
||
89 | * @JoinColumn(name="donor_district_id", referencedColumnName="district_id") |
||
90 | */ |
||
91 | protected $district; |
||
92 | |||
93 | /** |
||
94 | * Donor creation datetime |
||
95 | * |
||
96 | * @var string |
||
97 | * @since 1.0 |
||
98 | * |
||
99 | * @Column(type="datetime", name="donor_created_at") |
||
100 | */ |
||
101 | protected $created_at; |
||
102 | |||
103 | /** |
||
104 | * Donor created by |
||
105 | * |
||
106 | * @var User |
||
107 | * @since 1.0 |
||
108 | * |
||
109 | * @ManyToOne(targetEntity="EBloodBank\Models\User") |
||
110 | * @JoinColumn(name="donor_created_by", referencedColumnName="user_id") |
||
111 | */ |
||
112 | protected $created_by; |
||
113 | |||
114 | /** |
||
115 | * Donor status |
||
116 | * |
||
117 | * @var string |
||
118 | * @since 1.0 |
||
119 | * |
||
120 | * @Column(type="string", name="donor_status") |
||
121 | */ |
||
122 | protected $status; |
||
123 | |||
124 | /** |
||
125 | * Donor meta |
||
126 | * |
||
127 | * @var array |
||
128 | * @since 1.0 |
||
129 | * |
||
130 | * @Column(type="json", name="donor_meta") |
||
131 | */ |
||
132 | protected $meta = []; |
||
133 | |||
134 | /** |
||
135 | * @return bool |
||
136 | * @since 1.0 |
||
137 | */ |
||
138 | public function isExists() |
||
139 | { |
||
140 | $id = (int) $this->get('id'); |
||
141 | return ! empty($id); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return bool |
||
146 | * @since 1.0 |
||
147 | */ |
||
148 | public function isPending() |
||
149 | { |
||
150 | return 'pending' === $this->get('status'); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @return bool |
||
155 | * @since 1.0 |
||
156 | */ |
||
157 | public function isApproved() |
||
158 | { |
||
159 | return 'approved' === $this->get('status'); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return string |
||
164 | * @since 1.0 |
||
165 | */ |
||
166 | public function getGenderTitle() |
||
167 | { |
||
168 | $genders = EBB\getGenders(); |
||
|
|||
169 | $gender = $this->get('gender'); |
||
170 | if (isset($genders[$gender])) { |
||
171 | $gender = $genders[$gender]; |
||
172 | } |
||
173 | return $gender; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @return string |
||
178 | * @since 1.0 |
||
179 | */ |
||
180 | public function getAge($format = '%y') |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @return void |
||
194 | * @since 1.6 |
||
195 | * |
||
196 | * @PrePersist |
||
197 | */ |
||
198 | public function doActionOnPrePersist() |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return mixed |
||
205 | * @since 1.0 |
||
206 | * @static |
||
207 | */ |
||
208 | public static function sanitize($key, $value) |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @throws \InvalidArgumentException |
||
230 | * @return bool |
||
231 | * @since 1.0 |
||
232 | * @static |
||
233 | */ |
||
234 | public static function validate($key, $value) |
||
235 | { |
||
236 | switch ($key) { |
||
237 | case 'id': |
||
238 | if (! EBB\isValidID($value)) { |
||
239 | throw new InvalidArgumentException(__('Invalid donor ID.')); |
||
240 | } |
||
241 | break; |
||
242 | case 'name': |
||
243 | if (! is_string($value) || empty($value)) { |
||
244 | throw new InvalidArgumentException(__('Invalid donor name.')); |
||
245 | } |
||
246 | break; |
||
247 | case 'gender': |
||
248 | if (! array_key_exists($value, EBB\getGenders())) { |
||
249 | throw new InvalidArgumentException(__('Invalid donor gender.')); |
||
250 | } |
||
251 | break; |
||
252 | case 'blood_group': |
||
253 | if (! in_array($value, EBB\getBloodGroups(), true)) { |
||
254 | throw new InvalidArgumentException(__('Invalid donor blood group.')); |
||
255 | } |
||
256 | break; |
||
257 | case 'district': |
||
258 | if (! $value instanceof District || ! $value->isExists()) { |
||
259 | throw new InvalidArgumentException(__('Invalid donor district.')); |
||
260 | } |
||
261 | break; |
||
262 | case 'created_by': |
||
263 | if (! $value instanceof User || ! $value->isExists()) { |
||
264 | throw new InvalidArgumentException(__('Invalid donor originator.')); |
||
265 | } |
||
266 | break; |
||
267 | case 'status': |
||
268 | if (! is_string($value) || empty($value)) { |
||
269 | throw new InvalidArgumentException(__('Invalid donor status.')); |
||
270 | } |
||
271 | break; |
||
272 | } |
||
273 | |||
274 | return true; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @return mixed |
||
279 | * @since 1.0 |
||
280 | * @static |
||
281 | */ |
||
282 | public static function sanitizeMeta(string $key, $value) |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @throws \InvalidArgumentException |
||
304 | * @return bool |
||
305 | * @since 1.0 |
||
306 | * @static |
||
307 | */ |
||
308 | public static function validateMeta(string $key, $value) |
||
342 |