Completed
Push — template-events-dynamic ( 9d0205 )
by Kamil
05:04
created

ManagingChannelsContext   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 517
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 8

Importance

Changes 0
Metric Value
wmc 55
lcom 3
cbo 8
dl 0
loc 517
rs 6
c 0
b 0
f 0

52 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A iWantToCreateANewChannel() 0 4 1
A iSpecifyItsCodeAs() 0 4 1
A iNameIt() 0 4 1
A iChooseAsABaseCurrency() 0 4 2
A iChooseAsADefaultLocale() 0 4 1
A iShouldBeNotifiedThatChannelWithThisCodeAlreadyExists() 0 4 1
A thereShouldStillBeOnlyOneChannelWithCode() 0 6 1
A iWantToBrowseChannels() 0 4 1
A iCheckTheChannel() 0 4 1
A iDeleteThem() 0 4 1
A iShouldSeeChannelsInTheList() 0 4 1
A theCodeFieldShouldBeDisabled() 0 4 1
A thisChannelShouldBeDisabled() 0 4 1
A thisChannelShouldBeEnabled() 0 4 1
A iDeleteChannel() 0 5 1
A thisChannelShouldNoLongerExistInTheRegistry() 0 4 1
A iShouldBeNotifiedThatItCannotBeDeleted() 0 7 1
A iMakeItAvailableIn() 0 7 1
A theChannelShouldBeAvailableIn() 0 6 1
A iAllowToPayingForThisChannel() 0 7 1
A payingInEuroShouldBePossibleForTheChannel() 0 6 1
A iSelectDefaultTaxZone() 0 7 1
A iRemoveItsDefaultTaxZone() 0 4 1
A iSelectTaxCalculationStrategy() 0 7 1
A theDefaultTaxZoneForTheChannelShouldBe() 0 6 1
A iChooseOperatingCountries() 0 4 1
A iSpecifyMenuTaxonAs() 0 4 1
A iChangeItsMenuTaxonTo() 0 4 1
A iAllowToSkipShippingStepIfOnlyOneShippingMethodIsAvailable() 0 4 1
A iAllowToSkipPaymentStepIfOnlyOnePaymentMethodIsAvailable() 0 4 1
A iAddIt() 0 4 1
A theChannelShouldAppearInTheRegistry() 0 6 1
A thisChannelShouldAppearInTheRegistry() 0 4 1
A iDescribeItAs() 0 4 1
A iSetItsHostnameAs() 0 4 1
A iSetItsContactEmailAs() 0 4 1
A iDefineItsColorAs() 0 4 1
A iEnableIt() 0 4 1
A iDisableIt() 0 7 1
A iShouldBeNotifiedThatAtLeastOneChannelHasToBeDefinedIsRequired() 0 7 1
A channelWithShouldNotBeAdded() 0 6 1
A iShouldBeNotifiedThatIsRequired() 0 10 1
A iWantToModifyChannel() 0 4 1
A thisChannelNameShouldBe() 0 9 1
A iSaveMyChanges() 0 4 1
A channelShouldNotHaveDefaultTaxZone() 0 6 1
A theTaxCalculationStrategyForTheChannelShouldBe() 0 6 1
A theBaseCurrencyFieldShouldBeDisabled() 0 4 1
A iShouldBeNotifiedThatTheDefaultLocaleHasToBeEnabled() 0 7 1
A thisChannelMenuTaxonShouldBe() 0 8 2
A assertChannelState() 0 9 2

How to fix   Complexity   

Complex Class

Complex classes like ManagingChannelsContext 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 ManagingChannelsContext, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Ui\Admin;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Behat\NotificationType;
18
use Sylius\Behat\Page\Admin\Channel\CreatePageInterface;
19
use Sylius\Behat\Page\Admin\Channel\IndexPageInterface;
20
use Sylius\Behat\Page\Admin\Channel\UpdatePageInterface;
21
use Sylius\Behat\Service\NotificationCheckerInterface;
22
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
23
use Sylius\Component\Core\Formatter\StringInflector;
24
use Sylius\Component\Core\Model\ChannelInterface;
25
use Sylius\Component\Currency\Model\CurrencyInterface;
26
use Webmozart\Assert\Assert;
27
28
final class ManagingChannelsContext implements Context
29
{
30
    /** @var IndexPageInterface */
31
    private $indexPage;
32
33
    /** @var CreatePageInterface */
34
    private $createPage;
35
36
    /** @var UpdatePageInterface */
37
    private $updatePage;
38
39
    /** @var CurrentPageResolverInterface */
40
    private $currentPageResolver;
41
42
    /** @var NotificationCheckerInterface */
43
    private $notificationChecker;
44
45
    public function __construct(
46
        IndexPageInterface $indexPage,
47
        CreatePageInterface $createPage,
48
        UpdatePageInterface $updatePage,
49
        CurrentPageResolverInterface $currentPageResolver,
50
        NotificationCheckerInterface $notificationChecker
51
    ) {
52
        $this->indexPage = $indexPage;
53
        $this->createPage = $createPage;
54
        $this->updatePage = $updatePage;
55
        $this->currentPageResolver = $currentPageResolver;
56
        $this->notificationChecker = $notificationChecker;
57
    }
58
59
    /**
60
     * @Given I want to create a new channel
61
     */
62
    public function iWantToCreateANewChannel()
63
    {
64
        $this->createPage->open();
65
    }
66
67
    /**
68
     * @When I specify its code as :code
69
     * @When I do not specify its code
70
     */
71
    public function iSpecifyItsCodeAs($code = null)
72
    {
73
        $this->createPage->specifyCode($code ?? '');
74
    }
75
76
    /**
77
     * @When I name it :name
78
     * @When I rename it to :name
79
     * @When I do not name it
80
     * @When I remove its name
81
     */
82
    public function iNameIt($name = null)
83
    {
84
        $this->createPage->nameIt($name ?? '');
85
    }
86
87
    /**
88
     * @When I choose :currency as the base currency
89
     * @When I do not choose base currency
90
     */
91
    public function iChooseAsABaseCurrency(?CurrencyInterface $currency = null)
92
    {
93
        $this->createPage->chooseBaseCurrency($currency ? $currency->getName() : null);
94
    }
95
96
    /**
97
     * @When I choose :locale as a default locale
98
     * @When I do not choose default locale
99
     */
100
    public function iChooseAsADefaultLocale($locale = null)
101
    {
102
        $this->createPage->chooseDefaultLocale($locale);
103
    }
104
105
    /**
106
     * @When I choose :firstCountry and :secondCountry as operating countries
107
     */
108
    public function iChooseOperatingCountries(string ...$countries): void
109
    {
110
        $this->createPage->chooseOperatingCountries($countries);
111
    }
112
113
    /**
114
     * @When I specify menu taxon as :menuTaxon
115
     */
116
    public function iSpecifyMenuTaxonAs(string $menuTaxon): void
117
    {
118
        $this->createPage->specifyMenuTaxon($menuTaxon);
119
    }
120
121
    /**
122
     * @When I change its menu taxon to :menuTaxon
123
     */
124
    public function iChangeItsMenuTaxonTo(string $menuTaxon): void
125
    {
126
        $this->updatePage->changeMenuTaxon($menuTaxon);
127
    }
128
129
    /**
130
     * @When I allow to skip shipping step if only one shipping method is available
131
     */
132
    public function iAllowToSkipShippingStepIfOnlyOneShippingMethodIsAvailable()
133
    {
134
        $this->createPage->allowToSkipShippingStep();
135
    }
136
137
    /**
138
     * @When I allow to skip payment step if only one payment method is available
139
     */
140
    public function iAllowToSkipPaymentStepIfOnlyOnePaymentMethodIsAvailable()
141
    {
142
        $this->createPage->allowToSkipPaymentStep();
143
    }
144
145
    /**
146
     * @When I add it
147
     * @When I try to add it
148
     */
149
    public function iAddIt()
150
    {
151
        $this->createPage->create();
152
    }
153
154
    /**
155
     * @Then I should see the channel :channelName in the list
156
     * @Then the channel :channelName should appear in the registry
157
     * @Then the channel :channelName should be in the registry
158
     */
159
    public function theChannelShouldAppearInTheRegistry(string $channelName): void
160
    {
161
        $this->iWantToBrowseChannels();
162
163
        Assert::true($this->indexPage->isSingleResourceOnPage(['nameAndDescription' => $channelName]));
164
    }
165
166
    /**
167
     * @Then /^(this channel) should still be in the registry$/
168
     */
169
    public function thisChannelShouldAppearInTheRegistry(ChannelInterface $channel)
170
    {
171
        $this->theChannelShouldAppearInTheRegistry($channel->getName());
172
    }
173
174
    /**
175
     * @When I describe it as :description
176
     */
177
    public function iDescribeItAs($description)
178
    {
179
        $this->createPage->describeItAs($description);
180
    }
181
182
    /**
183
     * @When I set its hostname as :hostname
184
     */
185
    public function iSetItsHostnameAs($hostname)
186
    {
187
        $this->createPage->setHostname($hostname);
188
    }
189
190
    /**
191
     * @When I set its contact email as :contactEmail
192
     */
193
    public function iSetItsContactEmailAs($contactEmail)
194
    {
195
        $this->createPage->setContactEmail($contactEmail);
196
    }
197
198
    /**
199
     * @When I define its color as :color
200
     */
201
    public function iDefineItsColorAs($color)
202
    {
203
        $this->createPage->defineColor($color);
204
    }
205
206
    /**
207
     * @When I enable it
208
     */
209
    public function iEnableIt()
210
    {
211
        $this->updatePage->enable();
212
    }
213
214
    /**
215
     * @When I disable it
216
     */
217
    public function iDisableIt()
218
    {
219
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
220
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
221
222
        $currentPage->disable();
223
    }
224
225
    /**
226
     * @Then I should be notified that at least one channel has to be defined
227
     */
228
    public function iShouldBeNotifiedThatAtLeastOneChannelHasToBeDefinedIsRequired()
229
    {
230
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
231
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
232
233
        Assert::same($currentPage->getValidationMessage('enabled'), 'Must have at least one enabled entity');
234
    }
235
236
    /**
237
     * @Then channel with :element :value should not be added
238
     */
239
    public function channelWithShouldNotBeAdded($element, $value)
240
    {
241
        $this->iWantToBrowseChannels();
242
243
        Assert::false($this->indexPage->isSingleResourceOnPage([$element => $value]));
244
    }
245
246
    /**
247
     * @Then /^I should be notified that ([^"]+) is required$/
248
     */
249
    public function iShouldBeNotifiedThatIsRequired($element)
250
    {
251
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
252
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
253
254
        Assert::same(
255
            $currentPage->getValidationMessage(StringInflector::nameToCode($element)),
256
            sprintf('Please enter channel %s.', $element)
257
        );
258
    }
259
260
    /**
261
     * @Given I am modifying a channel :channel
262
     * @When I want to modify a channel :channel
263
     * @When /^I want to modify (this channel)$/
264
     */
265
    public function iWantToModifyChannel(ChannelInterface $channel): void
266
    {
267
        $this->updatePage->open(['id' => $channel->getId()]);
268
    }
269
270
    /**
271
     * @Then /^(this channel) name should be "([^"]+)"$/
272
     * @Then /^(this channel) should still be named "([^"]+)"$/
273
     */
274
    public function thisChannelNameShouldBe(ChannelInterface $channel, $channelName)
275
    {
276
        $this->iWantToBrowseChannels();
277
278
        Assert::true($this->indexPage->isSingleResourceOnPage([
279
            'code' => $channel->getCode(),
280
            'nameAndDescription' => $channelName,
281
        ]));
282
    }
283
284
    /**
285
     * @When I save my changes
286
     * @When I try to save my changes
287
     */
288
    public function iSaveMyChanges()
289
    {
290
        $this->updatePage->saveChanges();
291
    }
292
293
    /**
294
     * @Then I should be notified that channel with this code already exists
295
     */
296
    public function iShouldBeNotifiedThatChannelWithThisCodeAlreadyExists()
297
    {
298
        Assert::same($this->createPage->getValidationMessage('code'), 'Channel code has to be unique.');
299
    }
300
301
    /**
302
     * @Then there should still be only one channel with :element :value
303
     */
304
    public function thereShouldStillBeOnlyOneChannelWithCode($element, $value)
305
    {
306
        $this->iWantToBrowseChannels();
307
308
        Assert::true($this->indexPage->isSingleResourceOnPage([$element => $value]));
309
    }
310
311
    /**
312
     * @When I browse channels
313
     * @When I want to browse channels
314
     */
315
    public function iWantToBrowseChannels(): void
316
    {
317
        $this->indexPage->open();
318
    }
319
320
    /**
321
     * @When I check (also) the :channelName channel
322
     */
323
    public function iCheckTheChannel(string $channelName): void
324
    {
325
        $this->indexPage->checkResourceOnPage(['nameAndDescription' => $channelName]);
326
    }
327
328
    /**
329
     * @When I delete them
330
     */
331
    public function iDeleteThem(): void
332
    {
333
        $this->indexPage->bulkDelete();
334
    }
335
336
    /**
337
     * @Then I should see a single channel in the list
338
     * @Then I should see :numberOfChannels channels in the list
339
     */
340
    public function iShouldSeeChannelsInTheList(int $numberOfChannels = 1): void
341
    {
342
        Assert::same($this->indexPage->countItems(), $numberOfChannels);
343
    }
344
345
    /**
346
     * @Then the code field should be disabled
347
     */
348
    public function theCodeFieldShouldBeDisabled()
349
    {
350
        Assert::true($this->updatePage->isCodeDisabled());
351
    }
352
353
    /**
354
     * @Then /^(this channel) should be disabled$/
355
     */
356
    public function thisChannelShouldBeDisabled(ChannelInterface $channel)
357
    {
358
        $this->assertChannelState($channel, false);
359
    }
360
361
    /**
362
     * @Then /^(this channel) should be enabled$/
363
     * @Then channel with name :channel should still be enabled
364
     */
365
    public function thisChannelShouldBeEnabled(ChannelInterface $channel)
366
    {
367
        $this->assertChannelState($channel, true);
368
    }
369
370
    /**
371
     * @When I delete channel :channel
372
     */
373
    public function iDeleteChannel(ChannelInterface $channel)
374
    {
375
        $this->indexPage->open();
376
        $this->indexPage->deleteResourceOnPage(['nameAndDescription' => $channel->getName()]);
377
    }
378
379
    /**
380
     * @Then the :channelName channel should no longer exist in the registry
381
     */
382
    public function thisChannelShouldNoLongerExistInTheRegistry($channelName)
383
    {
384
        Assert::false($this->indexPage->isSingleResourceOnPage(['nameAndDescription' => $channelName]));
385
    }
386
387
    /**
388
     * @Then I should be notified that it cannot be deleted
389
     */
390
    public function iShouldBeNotifiedThatItCannotBeDeleted()
391
    {
392
        $this->notificationChecker->checkNotification(
393
            'The channel cannot be deleted. At least one enabled channel is required.',
394
            NotificationType::failure()
395
        );
396
    }
397
398
    /**
399
     * @When I make it available (only) in :locale
400
     */
401
    public function iMakeItAvailableIn(string $localeName): void
402
    {
403
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
404
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
405
406
        $currentPage->chooseLocale($localeName);
407
    }
408
409
    /**
410
     * @Then the channel :channel should be available in :locale
411
     */
412
    public function theChannelShouldBeAvailableIn(ChannelInterface $channel, $locale)
413
    {
414
        $this->updatePage->open(['id' => $channel->getId()]);
415
416
        Assert::true($this->updatePage->isLocaleChosen($locale));
417
    }
418
419
    /**
420
     * @When I allow for paying in :currencyCode
421
     */
422
    public function iAllowToPayingForThisChannel($currencyCode)
423
    {
424
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
425
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
426
427
        $currentPage->chooseCurrency($currencyCode);
428
    }
429
430
    /**
431
     * @Then paying in :currencyCode should be possible for the :channel channel
432
     */
433
    public function payingInEuroShouldBePossibleForTheChannel($currencyCode, ChannelInterface $channel)
434
    {
435
        $this->updatePage->open(['id' => $channel->getId()]);
436
437
        Assert::true($this->updatePage->isCurrencyChosen($currencyCode));
438
    }
439
440
    /**
441
     * @When I select the :taxZone as default tax zone
442
     */
443
    public function iSelectDefaultTaxZone($taxZone)
444
    {
445
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
446
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
447
448
        $currentPage->chooseDefaultTaxZone($taxZone);
449
    }
450
451
    /**
452
     * @Given I remove its default tax zone
453
     */
454
    public function iRemoveItsDefaultTaxZone()
455
    {
456
        $this->updatePage->chooseDefaultTaxZone('');
457
    }
458
459
    /**
460
     * @When I select the :taxCalculationStrategy as tax calculation strategy
461
     */
462
    public function iSelectTaxCalculationStrategy($taxCalculationStrategy)
463
    {
464
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
465
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
466
467
        $currentPage->chooseTaxCalculationStrategy($taxCalculationStrategy);
468
    }
469
470
    /**
471
     * @Then the default tax zone for the :channel channel should be :taxZone
472
     */
473
    public function theDefaultTaxZoneForTheChannelShouldBe(ChannelInterface $channel, $taxZone)
474
    {
475
        $this->updatePage->open(['id' => $channel->getId()]);
476
477
        Assert::true($this->updatePage->isDefaultTaxZoneChosen($taxZone));
478
    }
479
480
    /**
481
     * @Given channel :channel should not have default tax zone
482
     */
483
    public function channelShouldNotHaveDefaultTaxZone(ChannelInterface $channel)
484
    {
485
        $this->updatePage->open(['id' => $channel->getId()]);
486
487
        Assert::false($this->updatePage->isAnyDefaultTaxZoneChosen());
488
    }
489
490
    /**
491
     * @Then the tax calculation strategy for the :channel channel should be :taxCalculationStrategy
492
     */
493
    public function theTaxCalculationStrategyForTheChannelShouldBe(ChannelInterface $channel, $taxCalculationStrategy)
494
    {
495
        $this->updatePage->open(['id' => $channel->getId()]);
496
497
        Assert::true($this->updatePage->isTaxCalculationStrategyChosen($taxCalculationStrategy));
498
    }
499
500
    /**
501
     * @Then the base currency field should be disabled
502
     */
503
    public function theBaseCurrencyFieldShouldBeDisabled()
504
    {
505
        Assert::true($this->updatePage->isBaseCurrencyDisabled());
506
    }
507
508
    /**
509
     * @Then I should be notified that the default locale has to be enabled
510
     */
511
    public function iShouldBeNotifiedThatTheDefaultLocaleHasToBeEnabled(): void
512
    {
513
        Assert::same(
514
            $this->updatePage->getValidationMessage('default_locale'),
515
            'Default locale has to be enabled.'
516
        );
517
    }
518
519
    /**
520
     * @Given /^(this channel) menu taxon should be "([^"]+)"$/
521
     * @Given the channel :channel should have :menuTaxon as a menu taxon
522
     */
523
    public function thisChannelMenuTaxonShouldBe(ChannelInterface $channel, string $menuTaxon): void
524
    {
525
        if (!$this->updatePage->isOpen(['id' => $channel->getId()])) {
526
            $this->updatePage->open(['id' => $channel->getId()]);
527
        }
528
529
        Assert::same($this->updatePage->getMenuTaxon(), $menuTaxon);
530
    }
531
532
    /**
533
     * @param bool $state
534
     */
535
    private function assertChannelState(ChannelInterface $channel, $state)
536
    {
537
        $this->iWantToBrowseChannels();
538
539
        Assert::true($this->indexPage->isSingleResourceOnPage([
540
            'nameAndDescription' => $channel->getName(),
541
            'enabled' => $state ? 'Enabled' : 'Disabled',
542
        ]));
543
    }
544
}
545