Complex classes like ManagingPromotionCouponsContext 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ManagingPromotionCouponsContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | final class ManagingPromotionCouponsContext implements Context |
||
29 | { |
||
30 | /** |
||
31 | * @var CreatePageInterface |
||
32 | */ |
||
33 | private $createPage; |
||
34 | |||
35 | /** |
||
36 | * @var GeneratePageInterface |
||
37 | */ |
||
38 | private $generatePage; |
||
39 | |||
40 | /** |
||
41 | * @var IndexPageInterface |
||
42 | */ |
||
43 | private $indexPage; |
||
44 | |||
45 | /** |
||
46 | * @var UpdatePageInterface |
||
47 | */ |
||
48 | private $updatePage; |
||
49 | |||
50 | /** |
||
51 | * @var CurrentPageResolverInterface |
||
52 | */ |
||
53 | private $currentPageResolver; |
||
54 | |||
55 | /** |
||
56 | * @var NotificationCheckerInterface |
||
57 | */ |
||
58 | private $notificationChecker; |
||
59 | |||
60 | /** |
||
61 | * @param CreatePageInterface $createPage |
||
62 | * @param GeneratePageInterface $generatePage |
||
63 | * @param IndexPageInterface $indexPage |
||
64 | * @param UpdatePageInterface $updatePage |
||
65 | * @param CurrentPageResolverInterface $currentPageResolver |
||
66 | * @param NotificationCheckerInterface $notificationChecker |
||
67 | */ |
||
68 | public function __construct( |
||
69 | CreatePageInterface $createPage, |
||
70 | GeneratePageInterface $generatePage, |
||
71 | IndexPageInterface $indexPage, |
||
72 | UpdatePageInterface $updatePage, |
||
73 | CurrentPageResolverInterface $currentPageResolver, |
||
74 | NotificationCheckerInterface $notificationChecker |
||
75 | ) { |
||
76 | $this->createPage = $createPage; |
||
77 | $this->generatePage = $generatePage; |
||
78 | $this->indexPage = $indexPage; |
||
79 | $this->updatePage = $updatePage; |
||
80 | $this->currentPageResolver = $currentPageResolver; |
||
81 | $this->notificationChecker = $notificationChecker; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @Given /^I browse coupons of (this promotion)$/ |
||
86 | * @Given /^I want to view all coupons of (this promotion)$/ |
||
87 | * @When /^I browse all coupons of ("[^"]+" promotion)$/ |
||
88 | */ |
||
89 | public function iWantToViewAllCouponsOfThisPromotion(PromotionInterface $promotion) |
||
90 | { |
||
91 | $this->indexPage->open(['promotionId' => $promotion->getId()]); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @Given /^I want to create a new coupon for (this promotion)$/ |
||
96 | */ |
||
97 | public function iWantToCreateANewCouponForThisPromotion(PromotionInterface $promotion) |
||
101 | |||
102 | /** |
||
103 | * @Given /^I want to modify the ("[^"]+" coupon) for (this promotion)$/ |
||
104 | */ |
||
105 | public function iWantToModifyTheCoupon(PromotionCouponInterface $coupon, PromotionInterface $promotion) |
||
106 | { |
||
107 | $this->updatePage->open(['id' => $coupon->getId(), 'promotionId' => $promotion->getId()]); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @Given /^I want to generate a new coupons for (this promotion)$/ |
||
112 | */ |
||
113 | public function iWantToGenerateANewCouponsForThisPromotion(PromotionInterface $promotion) |
||
114 | { |
||
115 | $this->generatePage->open(['promotionId' => $promotion->getId()]); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @When /^I specify its code length as (\d+)$/ |
||
120 | * @When I do not specify its code length |
||
121 | */ |
||
122 | public function iSpecifyItsCodeLengthAs($codeLength = null) |
||
123 | { |
||
124 | $this->generatePage->specifyCodeLength($codeLength); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @When /^I limit generated coupons usage to (\d+) times$/ |
||
129 | */ |
||
130 | public function iSetGeneratedCouponsUsageLimitTo($limit) |
||
131 | { |
||
132 | $this->generatePage->setUsageLimit($limit); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @When I make generated coupons valid until :date |
||
137 | */ |
||
138 | public function iMakeGeneratedCouponsValidUntil(\DateTimeInterface $date) |
||
139 | { |
||
140 | $this->generatePage->setExpiresAt($date); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @When I specify its code as :code |
||
145 | * @When I do not specify its code |
||
146 | */ |
||
147 | public function iSpecifyItsCodeAs($code = null) |
||
148 | { |
||
149 | $this->createPage->specifyCode($code); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @When I limit its usage to :limit times |
||
154 | */ |
||
155 | public function iLimitItsUsageLimitTo($limit) |
||
156 | { |
||
157 | $this->createPage->setUsageLimit($limit); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @When I change its usage limit to :limit |
||
162 | */ |
||
163 | public function iChangeItsUsageLimitTo($limit) |
||
164 | { |
||
165 | $this->updatePage->setUsageLimit($limit); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @When I specify its amount as :amount |
||
170 | * @When I do not specify its amount |
||
171 | */ |
||
172 | public function iSpecifyItsAmountAs($amount = null) |
||
173 | { |
||
174 | $this->generatePage->specifyAmount($amount); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @When I limit its per customer usage to :limit times |
||
179 | */ |
||
180 | public function iLimitItsPerCustomerUsageLimitTo($limit) |
||
181 | { |
||
182 | $this->createPage->setCustomerUsageLimit($limit); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @When I change its per customer usage limit to :limit |
||
187 | */ |
||
188 | public function iChangeItsPerCustomerUsageLimitTo($limit) |
||
189 | { |
||
190 | $this->updatePage->setCustomerUsageLimit($limit); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @When I make it valid until :date |
||
195 | */ |
||
196 | public function iMakeItValidUntil(\DateTimeInterface $date) |
||
197 | { |
||
198 | $this->createPage->setExpiresAt($date); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @When I change expires date to :date |
||
203 | */ |
||
204 | public function iChangeExpiresDateTo(\DateTimeInterface $date) |
||
205 | { |
||
206 | $this->updatePage->setExpiresAt($date); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @When I add it |
||
211 | * @When I try to add it |
||
212 | */ |
||
213 | public function iAddIt() |
||
217 | |||
218 | /** |
||
219 | * @When I save my changes |
||
220 | */ |
||
221 | public function iSaveMyChanges() |
||
222 | { |
||
223 | $this->updatePage->saveChanges(); |
||
225 | |||
226 | /** |
||
227 | * @When I generate it |
||
228 | * @When I try to generate it |
||
229 | */ |
||
230 | public function iGenerateIt() |
||
234 | |||
235 | /** |
||
236 | * @When /^I delete ("[^"]+" coupon) related to (this promotion)$/ |
||
237 | * @When /^I try to delete ("[^"]+" coupon) related to (this promotion)$/ |
||
238 | */ |
||
239 | public function iDeleteCouponRelatedToThisPromotion(PromotionCouponInterface $coupon, PromotionInterface $promotion) |
||
244 | |||
245 | /** |
||
246 | * @When I check (also) the :couponCode coupon |
||
247 | */ |
||
248 | public function iCheckTheCoupon(string $couponCode): void |
||
249 | { |
||
250 | $this->indexPage->checkResourceOnPage(['code' => $couponCode]); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @When I delete them |
||
255 | */ |
||
256 | public function iDeleteThem(): void |
||
260 | |||
261 | /** |
||
262 | * @Then /^there should be (\d+) coupon related to (this promotion)$/ |
||
263 | */ |
||
264 | public function thereShouldBeCouponRelatedTo($number, PromotionInterface $promotion) |
||
270 | |||
271 | /** |
||
272 | * @Then I should see a single coupon in the list |
||
273 | */ |
||
274 | public function iShouldSeeASingleCouponInTheList(): void |
||
278 | |||
279 | /** |
||
280 | * @Then /^there should be coupon with code "([^"]+)"$/ |
||
281 | */ |
||
282 | public function thereShouldBeCouponWithCode($code) |
||
286 | |||
287 | /** |
||
288 | * @Then this coupon should be valid until :date |
||
289 | */ |
||
290 | public function thisCouponShouldBeValidUntil(\DateTimeInterface $date) |
||
294 | |||
295 | /** |
||
296 | * @Then /^this coupon should have (\d+) usage limit$/ |
||
297 | */ |
||
298 | public function thisCouponShouldHaveUsageLimit($limit) |
||
302 | |||
303 | /** |
||
304 | * @Then /^("[^"]+" coupon) should be used (\d+) time(?:|s)$/ |
||
305 | */ |
||
306 | public function couponShouldHaveUsageLimit(PromotionCouponInterface $promotionCoupon, $used) |
||
310 | |||
311 | /** |
||
312 | * @Then /^this coupon should have (\d+) per customer usage limit$/ |
||
313 | */ |
||
314 | public function thisCouponShouldHavePerCustomerUsageLimit($limit) |
||
318 | |||
319 | /** |
||
320 | * @Then the code field should be disabled |
||
321 | */ |
||
322 | public function theCodeFieldShouldBeDisabled() |
||
326 | |||
327 | /** |
||
328 | * @Then /^I should be notified that coupon with this code already exists$/ |
||
329 | */ |
||
330 | public function iShouldBeNotifiedThatCouponWithThisCodeAlreadyExists() |
||
337 | |||
338 | /** |
||
339 | * @Then I should be notified that :element is required |
||
340 | */ |
||
341 | public function iShouldBeNotifiedThatIsRequired($element) |
||
348 | |||
349 | /** |
||
350 | * @Then I should be notified that generate amount is required |
||
351 | */ |
||
352 | public function iShouldBeNotifiedThatGenerateAmountIsRequired() |
||
356 | |||
357 | /** |
||
358 | * @Then I should be notified that generate code length is required |
||
359 | */ |
||
360 | public function iShouldBeNotifiedThatCodeLengthIsRequired() |
||
364 | |||
365 | /** |
||
366 | * @Then /^there should still be only one coupon with code "([^"]+)" related to (this promotion)$/ |
||
367 | */ |
||
368 | public function thereShouldStillBeOnlyOneCouponWithCodeRelatedTo($code, PromotionInterface $promotion) |
||
374 | |||
375 | /** |
||
376 | * @Then I should be notified that coupon usage limit must be at least one |
||
377 | */ |
||
378 | public function iShouldBeNotifiedThatCouponUsageLimitMustBeAtLeast() |
||
385 | |||
386 | /** |
||
387 | * @Then /^(this coupon) should no longer exist in the coupon registry$/ |
||
388 | */ |
||
389 | public function couponShouldNotExistInTheRegistry(PromotionCouponInterface $coupon) |
||
393 | |||
394 | /** |
||
395 | * @Then I should be notified that it has been successfully generated |
||
396 | */ |
||
397 | public function iShouldBeNotifiedThatItHasBeenSuccessfullyGenerated() |
||
401 | |||
402 | /** |
||
403 | * @Then I should be notified that it is in use and cannot be deleted |
||
404 | */ |
||
405 | public function iShouldBeNotifiedOfFailure() |
||
412 | |||
413 | /** |
||
414 | * @Then /^(this coupon) should still exist in the registry$/ |
||
415 | */ |
||
416 | public function couponShouldStillExistInTheRegistry(PromotionCouponInterface $coupon) |
||
420 | |||
421 | /** |
||
422 | * @Then /^I should be notified that generating (\d+) coupons with code length equal to (\d+) is not possible$/ |
||
423 | */ |
||
424 | public function iShouldBeNotifiedThatGeneratingCouponsWithCodeLengthIsNotPossible($amount, $codeLength) |
||
430 | |||
431 | /** |
||
432 | * @Then I should see the coupon :couponCode in the list |
||
433 | */ |
||
434 | public function iShouldSeeTheCouponInTheList(string $couponCode): void |
||
438 | } |
||
439 |