Completed
Push — master ( 23d5f0...daf9a4 )
by Kamil
05:58
created

ManagingChannelsContext   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 504
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 8

Importance

Changes 0
Metric Value
wmc 53
lcom 3
cbo 8
dl 0
loc 504
rs 6.96
c 0
b 0
f 0

51 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 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 iDefineItsTypeAs() 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 iChangeItsTypeTo() 0 4 1
A channelShouldNotHaveDefaultTaxZone() 0 6 1
A theTaxCalculationStrategyForTheChannelShouldBe() 0 6 1
A theBaseCurrencyFieldShouldBeDisabled() 0 4 1
A iShouldBeNotifiedThatTheDefaultLocaleHasToBeEnabled() 0 7 1
A thisChannelTypeShouldBe() 0 4 1
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\UpdatePageInterface;
20
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
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 allow to skip shipping step if only one shipping method is available
107
     */
108
    public function iAllowToSkipShippingStepIfOnlyOneShippingMethodIsAvailable()
109
    {
110
        $this->createPage->allowToSkipShippingStep();
111
    }
112
113
    /**
114
     * @When I allow to skip payment step if only one payment method is available
115
     */
116
    public function iAllowToSkipPaymentStepIfOnlyOnePaymentMethodIsAvailable()
117
    {
118
        $this->createPage->allowToSkipPaymentStep();
119
    }
120
121
    /**
122
     * @When I add it
123
     * @When I try to add it
124
     */
125
    public function iAddIt()
126
    {
127
        $this->createPage->create();
128
    }
129
130
    /**
131
     * @Then I should see the channel :channelName in the list
132
     * @Then the channel :channelName should appear in the registry
133
     * @Then the channel :channelName should be in the registry
134
     */
135
    public function theChannelShouldAppearInTheRegistry(string $channelName): void
136
    {
137
        $this->iWantToBrowseChannels();
138
139
        Assert::true($this->indexPage->isSingleResourceOnPage(['nameAndDescription' => $channelName]));
140
    }
141
142
    /**
143
     * @Then /^(this channel) should still be in the registry$/
144
     */
145
    public function thisChannelShouldAppearInTheRegistry(ChannelInterface $channel)
146
    {
147
        $this->theChannelShouldAppearInTheRegistry($channel->getName());
148
    }
149
150
    /**
151
     * @When I describe it as :description
152
     */
153
    public function iDescribeItAs($description)
154
    {
155
        $this->createPage->describeItAs($description);
156
    }
157
158
    /**
159
     * @When I set its hostname as :hostname
160
     */
161
    public function iSetItsHostnameAs($hostname)
162
    {
163
        $this->createPage->setHostname($hostname);
164
    }
165
166
    /**
167
     * @When I set its contact email as :contactEmail
168
     */
169
    public function iSetItsContactEmailAs($contactEmail)
170
    {
171
        $this->createPage->setContactEmail($contactEmail);
172
    }
173
174
    /**
175
     * @When I define its color as :color
176
     */
177
    public function iDefineItsColorAs($color)
178
    {
179
        $this->createPage->defineColor($color);
180
    }
181
182
    /**
183
     * @When I enable it
184
     */
185
    public function iEnableIt()
186
    {
187
        $this->updatePage->enable();
188
    }
189
190
    /**
191
     * @When I disable it
192
     */
193
    public function iDisableIt()
194
    {
195
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
196
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
197
198
        $currentPage->disable();
199
    }
200
201
    /**
202
     * @Then I should be notified that at least one channel has to be defined
203
     */
204
    public function iShouldBeNotifiedThatAtLeastOneChannelHasToBeDefinedIsRequired()
205
    {
206
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
207
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
208
209
        Assert::same($currentPage->getValidationMessage('enabled'), 'Must have at least one enabled entity');
210
    }
211
212
    /**
213
     * @Then channel with :element :value should not be added
214
     */
215
    public function channelWithShouldNotBeAdded($element, $value)
216
    {
217
        $this->iWantToBrowseChannels();
218
219
        Assert::false($this->indexPage->isSingleResourceOnPage([$element => $value]));
220
    }
221
222
    /**
223
     * @Then /^I should be notified that ([^"]+) is required$/
224
     */
225
    public function iShouldBeNotifiedThatIsRequired($element)
226
    {
227
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
228
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
229
230
        Assert::same(
231
            $currentPage->getValidationMessage(StringInflector::nameToCode($element)),
232
            sprintf('Please enter channel %s.', $element)
233
        );
234
    }
235
236
    /**
237
     * @Given I am modifying a channel :channel
238
     * @When I want to modify a channel :channel
239
     * @When /^I want to modify (this channel)$/
240
     */
241
    public function iWantToModifyChannel(ChannelInterface $channel): void
242
    {
243
        $this->updatePage->open(['id' => $channel->getId()]);
244
    }
245
246
    /**
247
     * @Then /^(this channel) name should be "([^"]+)"$/
248
     * @Then /^(this channel) should still be named "([^"]+)"$/
249
     */
250
    public function thisChannelNameShouldBe(ChannelInterface $channel, $channelName)
251
    {
252
        $this->iWantToBrowseChannels();
253
254
        Assert::true($this->indexPage->isSingleResourceOnPage([
255
            'code' => $channel->getCode(),
256
            'nameAndDescription' => $channelName,
257
        ]));
258
    }
259
260
    /**
261
     * @When I save my changes
262
     * @When I try to save my changes
263
     */
264
    public function iSaveMyChanges()
265
    {
266
        $this->updatePage->saveChanges();
267
    }
268
269
    /**
270
     * @When /^I define its type as (mobile|website|pos)$/
271
     */
272
    public function iDefineItsTypeAs(string $type): void
273
    {
274
        $this->createPage->setType($type);
275
    }
276
277
    /**
278
     * @Then I should be notified that channel with this code already exists
279
     */
280
    public function iShouldBeNotifiedThatChannelWithThisCodeAlreadyExists()
281
    {
282
        Assert::same($this->createPage->getValidationMessage('code'), 'Channel code has to be unique.');
283
    }
284
285
    /**
286
     * @Then there should still be only one channel with :element :value
287
     */
288
    public function thereShouldStillBeOnlyOneChannelWithCode($element, $value)
289
    {
290
        $this->iWantToBrowseChannels();
291
292
        Assert::true($this->indexPage->isSingleResourceOnPage([$element => $value]));
293
    }
294
295
    /**
296
     * @When I browse channels
297
     * @When I want to browse channels
298
     */
299
    public function iWantToBrowseChannels(): void
300
    {
301
        $this->indexPage->open();
302
    }
303
304
    /**
305
     * @When I check (also) the :channelName channel
306
     */
307
    public function iCheckTheChannel(string $channelName): void
308
    {
309
        $this->indexPage->checkResourceOnPage(['nameAndDescription' => $channelName]);
310
    }
311
312
    /**
313
     * @When I delete them
314
     */
315
    public function iDeleteThem(): void
316
    {
317
        $this->indexPage->bulkDelete();
318
    }
319
320
    /**
321
     * @Then I should see a single channel in the list
322
     * @Then I should see :numberOfChannels channels in the list
323
     */
324
    public function iShouldSeeChannelsInTheList(int $numberOfChannels = 1): void
325
    {
326
        Assert::same($this->indexPage->countItems(), $numberOfChannels);
327
    }
328
329
    /**
330
     * @Then the code field should be disabled
331
     */
332
    public function theCodeFieldShouldBeDisabled()
333
    {
334
        Assert::true($this->updatePage->isCodeDisabled());
335
    }
336
337
    /**
338
     * @Then /^(this channel) should be disabled$/
339
     */
340
    public function thisChannelShouldBeDisabled(ChannelInterface $channel)
341
    {
342
        $this->assertChannelState($channel, false);
343
    }
344
345
    /**
346
     * @Then /^(this channel) should be enabled$/
347
     * @Then channel with name :channel should still be enabled
348
     */
349
    public function thisChannelShouldBeEnabled(ChannelInterface $channel)
350
    {
351
        $this->assertChannelState($channel, true);
352
    }
353
354
    /**
355
     * @When I delete channel :channel
356
     */
357
    public function iDeleteChannel(ChannelInterface $channel)
358
    {
359
        $this->indexPage->open();
360
        $this->indexPage->deleteResourceOnPage(['nameAndDescription' => $channel->getName()]);
361
    }
362
363
    /**
364
     * @Then the :channelName channel should no longer exist in the registry
365
     */
366
    public function thisChannelShouldNoLongerExistInTheRegistry($channelName)
367
    {
368
        Assert::false($this->indexPage->isSingleResourceOnPage(['nameAndDescription' => $channelName]));
369
    }
370
371
    /**
372
     * @Then I should be notified that it cannot be deleted
373
     */
374
    public function iShouldBeNotifiedThatItCannotBeDeleted()
375
    {
376
        $this->notificationChecker->checkNotification(
377
            'The channel cannot be deleted. At least one enabled channel is required.',
378
            NotificationType::failure()
379
        );
380
    }
381
382
    /**
383
     * @When I make it available (only) in :locale
384
     */
385
    public function iMakeItAvailableIn(string $localeName): void
386
    {
387
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
388
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
389
390
        $currentPage->chooseLocale($localeName);
391
    }
392
393
    /**
394
     * @Then the channel :channel should be available in :locale
395
     */
396
    public function theChannelShouldBeAvailableIn(ChannelInterface $channel, $locale)
397
    {
398
        $this->updatePage->open(['id' => $channel->getId()]);
399
400
        Assert::true($this->updatePage->isLocaleChosen($locale));
401
    }
402
403
    /**
404
     * @When I allow for paying in :currencyCode
405
     */
406
    public function iAllowToPayingForThisChannel($currencyCode)
407
    {
408
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
409
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
410
411
        $currentPage->chooseCurrency($currencyCode);
412
    }
413
414
    /**
415
     * @Then paying in :currencyCode should be possible for the :channel channel
416
     */
417
    public function payingInEuroShouldBePossibleForTheChannel($currencyCode, ChannelInterface $channel)
418
    {
419
        $this->updatePage->open(['id' => $channel->getId()]);
420
421
        Assert::true($this->updatePage->isCurrencyChosen($currencyCode));
422
    }
423
424
    /**
425
     * @When I select the :taxZone as default tax zone
426
     */
427
    public function iSelectDefaultTaxZone($taxZone)
428
    {
429
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
430
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
431
432
        $currentPage->chooseDefaultTaxZone($taxZone);
433
    }
434
435
    /**
436
     * @Given I remove its default tax zone
437
     */
438
    public function iRemoveItsDefaultTaxZone()
439
    {
440
        $this->updatePage->chooseDefaultTaxZone('');
441
    }
442
443
    /**
444
     * @When I select the :taxCalculationStrategy as tax calculation strategy
445
     */
446
    public function iSelectTaxCalculationStrategy($taxCalculationStrategy)
447
    {
448
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
449
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
450
451
        $currentPage->chooseTaxCalculationStrategy($taxCalculationStrategy);
452
    }
453
454
    /**
455
     * @Then the default tax zone for the :channel channel should be :taxZone
456
     */
457
    public function theDefaultTaxZoneForTheChannelShouldBe(ChannelInterface $channel, $taxZone)
458
    {
459
        $this->updatePage->open(['id' => $channel->getId()]);
460
461
        Assert::true($this->updatePage->isDefaultTaxZoneChosen($taxZone));
462
    }
463
464
    /**
465
     * @When /^I change its type to (mobile|website|pos)$/
466
     */
467
    public function iChangeItsTypeTo(string $type): void
468
    {
469
        $this->updatePage->changeType($type);
470
    }
471
472
    /**
473
     * @Given channel :channel should not have default tax zone
474
     */
475
    public function channelShouldNotHaveDefaultTaxZone(ChannelInterface $channel)
476
    {
477
        $this->updatePage->open(['id' => $channel->getId()]);
478
479
        Assert::false($this->updatePage->isAnyDefaultTaxZoneChosen());
480
    }
481
482
    /**
483
     * @Then the tax calculation strategy for the :channel channel should be :taxCalculationStrategy
484
     */
485
    public function theTaxCalculationStrategyForTheChannelShouldBe(ChannelInterface $channel, $taxCalculationStrategy)
486
    {
487
        $this->updatePage->open(['id' => $channel->getId()]);
488
489
        Assert::true($this->updatePage->isTaxCalculationStrategyChosen($taxCalculationStrategy));
490
    }
491
492
    /**
493
     * @Then the base currency field should be disabled
494
     */
495
    public function theBaseCurrencyFieldShouldBeDisabled()
496
    {
497
        Assert::true($this->updatePage->isBaseCurrencyDisabled());
498
    }
499
500
    /**
501
     * @Then I should be notified that the default locale has to be enabled
502
     */
503
    public function iShouldBeNotifiedThatTheDefaultLocaleHasToBeEnabled(): void
504
    {
505
        Assert::same(
506
            $this->updatePage->getValidationMessage('default_locale'),
507
            'Default locale has to be enabled.'
508
        );
509
    }
510
511
    /**
512
     * @Then /^this channel type should be (mobile|website|pos)$/
513
     */
514
    public function thisChannelTypeShouldBe(string $type): void
515
    {
516
        Assert::same($this->updatePage->getType(), $type);
517
    }
518
519
    /**
520
     * @param bool $state
521
     */
522
    private function assertChannelState(ChannelInterface $channel, $state)
523
    {
524
        $this->iWantToBrowseChannels();
525
526
        Assert::true($this->indexPage->isSingleResourceOnPage([
527
            'nameAndDescription' => $channel->getName(),
528
            'enabled' => $state ? 'Enabled' : 'Disabled',
529
        ]));
530
    }
531
}
532