Completed
Push — symfony3 ( 405d0c...88ded0 )
by Kamil
32:03 queued 12:32
created

ManagingChannelsContext   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 475
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 9

Importance

Changes 0
Metric Value
wmc 41
lcom 3
cbo 9
dl 0
loc 475
rs 8.2769
c 0
b 0
f 0

41 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 iChooseAsADefaultCurrency() 0 4 1
A iChooseAsADefaultLocale() 0 4 1
A iAddIt() 0 4 1
A theChannelShouldAppearInTheRegistry() 0 9 1
A thisChannelShouldAppearInTheRegistry() 0 4 1
A iDescribeItAs() 0 4 1
A iSetItsHostnameAs() 0 4 1
A iDefineItsColorAs() 0 4 1
A iEnableIt() 0 4 1
A iDisableIt() 0 6 1
A iShouldBeNotifiedThatAtLeastOneChannelHasToBeDefinedIsRequired() 0 7 1
A channelWithShouldNotBeAdded() 0 9 1
A iShouldBeNotifiedThatIsRequired() 0 10 1
A iWantToModifyChannel() 0 4 1
A thisChannelNameShouldBe() 0 14 1
A iSaveMyChanges() 0 4 1
A iShouldBeNotifiedThatChannelWithThisCodeAlreadyExists() 0 4 1
A thereShouldStillBeOnlyOneChannelWithCode() 0 9 1
A iWantToBrowseChannels() 0 4 1
A iShouldSeeChannelsInTheList() 0 10 1
A theCodeFieldShouldBeDisabled() 0 7 1
A thisChannelShouldBeDisabled() 0 4 1
A thisChannelShouldBeEnabled() 0 4 1
A iDeleteChannel() 0 5 1
A thisChannelShouldNoLongerExistInTheRegistry() 0 7 1
A iShouldBeNotifiedThatItCannotBeDeleted() 0 7 1
A iMakeItAvailableIn() 0 6 1
A theChannelShouldBeAvailableIn() 0 9 1
A iAllowToPayingForThisChannel() 0 6 1
A payingInEuroShouldBePossibleForTheChannel() 0 9 1
A iSelectDefaultTaxZone() 0 6 1
A iRemoveItsDefaultTaxZone() 0 4 1
A iSelectTaxCalculationStrategy() 0 6 1
A theDefaultTaxZoneForTheChannelShouldBe() 0 9 1
A channelShouldNotHaveDefaultTaxZone() 0 9 1
A theTaxCalculationStrategyForTheChannelShouldBe() 0 9 1
A assertChannelState() 0 13 1

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
namespace Sylius\Behat\Context\Ui\Admin;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\NotificationType;
16
use Sylius\Behat\Page\Admin\Channel\CreatePageInterface;
17
use Sylius\Behat\Page\Admin\Channel\IndexPageInterface;
18
use Sylius\Behat\Page\Admin\Channel\UpdatePageInterface;
19
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
20
use Sylius\Behat\Service\NotificationCheckerInterface;
21
use Sylius\Component\Core\Formatter\StringInflector;
22
use Sylius\Component\Core\Model\ChannelInterface;
23
use Webmozart\Assert\Assert;
24
25
/**
26
 * @author Łukasz Chruściel <[email protected]>
27
 */
28
final class ManagingChannelsContext implements Context
29
{
30
    /**
31
     * @var IndexPageInterface
32
     */
33
    private $indexPage;
34
35
    /**
36
     * @var CreatePageInterface
37
     */
38
    private $createPage;
39
40
    /**
41
     * @var UpdatePageInterface
42
     */
43
    private $updatePage;
44
45
    /**
46
     * @var CurrentPageResolverInterface
47
     */
48
    private $currentPageResolver;
49
50
    /**
51
     * @var NotificationCheckerInterface
52
     */
53
    private $notificationChecker;
54
55
    /**
56
     * @param IndexPageInterface $indexPage
57
     * @param CreatePageInterface $createPage
58
     * @param UpdatePageInterface $updatePage
59
     * @param CurrentPageResolverInterface $currentPageResolver
60
     * @param NotificationCheckerInterface $notificationChecker
61
     */
62
    public function __construct(
63
        IndexPageInterface $indexPage,
64
        CreatePageInterface $createPage,
65
        UpdatePageInterface $updatePage,
66
        CurrentPageResolverInterface $currentPageResolver,
67
        NotificationCheckerInterface $notificationChecker
68
    ) {
69
        $this->indexPage = $indexPage;
70
        $this->createPage = $createPage;
71
        $this->updatePage = $updatePage;
72
        $this->currentPageResolver = $currentPageResolver;
73
        $this->notificationChecker = $notificationChecker;
74
    }
75
76
    /**
77
     * @Given I want to create a new channel
78
     */
79
    public function iWantToCreateANewChannel()
80
    {
81
        $this->createPage->open();
82
    }
83
84
    /**
85
     * @When I specify its code as :code
86
     * @When I do not specify its code
87
     */
88
    public function iSpecifyItsCodeAs($code = null)
89
    {
90
        $this->createPage->specifyCode($code);
91
    }
92
93
    /**
94
     * @When I name it :name
95
     * @When I rename it to :name
96
     * @When I do not name it
97
     * @When I remove its name
98
     */
99
    public function iNameIt($name = null)
100
    {
101
        $this->createPage->nameIt($name);
102
    }
103
104
    /**
105
     * @When I choose :currency as a default currency
106
     * @When I do not choose default currency
107
     */
108
    public function iChooseAsADefaultCurrency($currency = null)
109
    {
110
        $this->createPage->chooseDefaultCurrency($currency);
111
    }
112
113
    /**
114
     * @When I choose :locale as a default locale
115
     * @When I do not choose default locale
116
     */
117
    public function iChooseAsADefaultLocale($locale = null)
118
    {
119
        $this->createPage->chooseDefaultLocale($locale);
120
    }
121
122
    /**
123
     * @When I add it
124
     * @When I try to add it
125
     */
126
    public function iAddIt()
127
    {
128
        $this->createPage->create();
129
    }
130
131
    /**
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($channelName)
136
    {
137
        $this->iWantToBrowseChannels();
138
139
        Assert::true($this->indexPage->isSingleResourceOnPage(
140
            ['nameAndDescription' => $channelName]),
141
            sprintf('Channel with name %s has not been found.', $channelName)
142
        );
143
    }
144
145
    /**
146
     * @Then /^(this channel) should still be in the registry$/
147
     */
148
    public function thisChannelShouldAppearInTheRegistry(ChannelInterface $channel)
149
    {
150
        $this->theChannelShouldAppearInTheRegistry($channel->getName());
151
    }
152
153
    /**
154
     * @When I describe it as :description
155
     */
156
    public function iDescribeItAs($description)
157
    {
158
        $this->createPage->describeItAs($description);
159
    }
160
161
    /**
162
     * @When I set its hostname as :hostname
163
     */
164
    public function iSetItsHostnameAs($hostname)
165
    {
166
        $this->createPage->setHostname($hostname);
167
    }
168
169
    /**
170
     * @When I define its color as :color
171
     */
172
    public function iDefineItsColorAs($color)
173
    {
174
        $this->createPage->defineColor($color);
175
    }
176
177
    /**
178
     * @When I enable it
179
     */
180
    public function iEnableIt()
181
    {
182
        $this->updatePage->enable();
183
    }
184
185
    /**
186
     * @When I disable it
187
     */
188
    public function iDisableIt()
189
    {
190
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
191
192
        $currentPage->disable();
193
    }
194
195
    /**
196
     * @Then I should be notified that at least one channel has to be defined
197
     */
198
    public function iShouldBeNotifiedThatAtLeastOneChannelHasToBeDefinedIsRequired()
199
    {
200
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
201
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
202
203
        Assert::same($currentPage->getValidationMessage('enabled'), 'Must have at least one enabled entity');
204
    }
205
206
    /**
207
     * @Then channel with :element :value should not be added
208
     */
209
    public function channelWithShouldNotBeAdded($element, $value)
210
    {
211
        $this->iWantToBrowseChannels();
212
213
        Assert::false(
214
            $this->indexPage->isSingleResourceOnPage([$element => $value]),
215
            sprintf('Channel with %s "%s" was created, but it should not.', $element, $value)
216
        );
217
    }
218
219
    /**
220
     * @Then /^I should be notified that ([^"]+) is required$/
221
     */
222
    public function iShouldBeNotifiedThatIsRequired($element)
223
    {
224
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
225
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
226
227
        Assert::same(
228
            $currentPage->getValidationMessage(StringInflector::nameToCode($element)),
229
            sprintf('Please enter channel %s.', $element)
230
        );
231
    }
232
233
    /**
234
     * @Given I want to modify a channel :channel
235
     * @Given /^I want to modify (this channel)$/
236
     */
237
    public function iWantToModifyChannel(ChannelInterface $channel)
238
    {
239
        $this->updatePage->open(['id' => $channel->getId()]);
240
    }
241
242
    /**
243
     * @Then /^(this channel) name should be "([^"]+)"$/
244
     * @Then /^(this channel) should still be named "([^"]+)"$/
245
     */
246
    public function thisChannelNameShouldBe(ChannelInterface $channel, $channelName)
247
    {
248
        $this->iWantToBrowseChannels();
249
250
        Assert::true(
251
            $this->indexPage->isSingleResourceOnPage(
252
                [
253
                    'code' => $channel->getCode(),
254
                    'nameAndDescription' => $channelName,
255
                ]
256
            ),
257
            sprintf('Channel name %s has not been assigned properly.', $channelName)
258
        );
259
    }
260
261
    /**
262
     * @When I save my changes
263
     * @When I try to save my changes
264
     */
265
    public function iSaveMyChanges()
266
    {
267
        $this->updatePage->saveChanges();
268
    }
269
270
    /**
271
     * @Then I should be notified that channel with this code already exists
272
     */
273
    public function iShouldBeNotifiedThatChannelWithThisCodeAlreadyExists()
274
    {
275
        Assert::same($this->createPage->getValidationMessage('code'), 'Channel code has to be unique.');
276
    }
277
278
    /**
279
     * @Then there should still be only one channel with :element :value
280
     */
281
    public function thereShouldStillBeOnlyOneChannelWithCode($element, $value)
282
    {
283
        $this->iWantToBrowseChannels();
284
285
        Assert::true(
286
            $this->indexPage->isSingleResourceOnPage([$element => $value]),
287
            sprintf('Channel with %s %s cannot be found.', $element, $value)
288
        );
289
    }
290
291
    /**
292
     * @When /^I want to browse channels$/
293
     */
294
    public function iWantToBrowseChannels()
295
    {
296
        $this->indexPage->open();
297
    }
298
299
    /**
300
     * @Then I should see :numberOfChannels channels in the list
301
     */
302
    public function iShouldSeeChannelsInTheList($numberOfChannels)
303
    {
304
        $foundRows = $this->indexPage->countItems();
305
306
        Assert::eq(
307
            (int) $numberOfChannels,
308
            $foundRows,
309
            sprintf('%s rows with channels should appear on page, %s rows has been found', $numberOfChannels, $foundRows)
310
        );
311
    }
312
313
    /**
314
     * @Then the code field should be disabled
315
     */
316
    public function theCodeFieldShouldBeDisabled()
317
    {
318
        Assert::true(
319
            $this->updatePage->isCodeDisabled(),
320
            'Code should be immutable, but it does not.'
321
        );
322
    }
323
324
    /**
325
     * @Then /^(this channel) should be disabled$/
326
     */
327
    public function thisChannelShouldBeDisabled(ChannelInterface $channel)
328
    {
329
        $this->assertChannelState($channel, false);
330
    }
331
332
    /**
333
     * @Then /^(this channel) should be enabled$/
334
     * @Then channel with name :channel should still be enabled
335
     */
336
    public function thisChannelShouldBeEnabled(ChannelInterface $channel)
337
    {
338
        $this->assertChannelState($channel, true);
339
    }
340
341
    /**
342
     * @When I delete channel :channel
343
     */
344
    public function iDeleteChannel(ChannelInterface $channel)
345
    {
346
        $this->indexPage->open();
347
        $this->indexPage->deleteResourceOnPage(['nameAndDescription' => $channel->getName()]);
348
    }
349
350
    /**
351
     * @Then the :channelName channel should no longer exist in the registry
352
     */
353
    public function thisChannelShouldNoLongerExistInTheRegistry($channelName)
354
    {
355
        Assert::false(
356
            $this->indexPage->isSingleResourceOnPage(['nameAndDescription' => $channelName]),
357
            sprintf('Channel with name %s exists but should not.', $channelName)
358
        );
359
    }
360
361
    /**
362
     * @Then I should be notified that it cannot be deleted
363
     */
364
    public function iShouldBeNotifiedThatItCannotBeDeleted()
365
    {
366
        $this->notificationChecker->checkNotification(
367
            "The channel cannot be deleted. At least one enabled channel is required.",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal The channel cannot be de...ed channel is required. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
368
            NotificationType::failure()
369
        );
370
    }
371
372
    /**
373
     * @When I make it available in :locale
374
     */
375
    public function iMakeItAvailableIn($locale)
376
    {
377
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
378
379
        $currentPage->chooseLocale($locale);
380
    }
381
382
    /**
383
     * @Then the channel :channel should be available in :locale
384
     */
385
    public function theChannelShouldBeAvailableIn(ChannelInterface $channel, $locale)
386
    {
387
        $this->updatePage->open(['id' => $channel->getId()]);
388
389
        Assert::true(
390
            $this->updatePage->isLocaleChosen($locale),
391
            sprintf('Language %s should be selected but it is not', $locale)
392
        );
393
    }
394
395
    /**
396
     * @When I allow for paying in :currencyCode
397
     */
398
    public function iAllowToPayingForThisChannel($currencyCode)
399
    {
400
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
401
402
        $currentPage->chooseCurrency($currencyCode);
403
    }
404
405
    /**
406
     * @Then paying in :currencyCode should be possible for the :channel channel
407
     */
408
    public function payingInEuroShouldBePossibleForTheChannel($currencyCode, ChannelInterface $channel)
409
    {
410
        $this->updatePage->open(['id' => $channel->getId()]);
411
412
        Assert::true(
413
            $this->updatePage->isCurrencyChosen($currencyCode),
414
            sprintf('Currency %s should be selected but it is not', $currencyCode)
415
        );
416
    }
417
418
    /**
419
     * @When I select the :taxZone as default tax zone
420
     */
421
    public function iSelectDefaultTaxZone($taxZone)
422
    {
423
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
424
425
        $currentPage->chooseDefaultTaxZone($taxZone);
426
    }
427
428
    /**
429
     * @Given I remove its default tax zone
430
     */
431
    public function iRemoveItsDefaultTaxZone()
432
    {
433
        $this->updatePage->chooseDefaultTaxZone(null);
434
    }
435
436
    /**
437
     * @When I select the :taxCalculationStrategy as tax calculation strategy
438
     */
439
    public function iSelectTaxCalculationStrategy($taxCalculationStrategy)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $taxCalculationStrategy exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
440
    {
441
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
442
443
        $currentPage->chooseTaxCalculationStrategy($taxCalculationStrategy);
444
    }
445
446
    /**
447
     * @Then the default tax zone for the :channel channel should be :taxZone
448
     */
449
    public function theDefaultTaxZoneForTheChannelShouldBe(ChannelInterface $channel, $taxZone)
450
    {
451
        $this->updatePage->open(['id' => $channel->getId()]);
452
453
        Assert::true(
454
            $this->updatePage->isDefaultTaxZoneChosen($taxZone),
455
            sprintf('Default tax zone %s should be selected, but it is not', $taxZone)
456
        );
457
    }
458
459
    /**
460
     * @Given channel :channel should not have default tax zone
461
     */
462
    public function channelShouldNotHaveDefaultTaxZone(ChannelInterface $channel)
463
    {
464
        $this->updatePage->open(['id' => $channel->getId()]);
465
466
        Assert::false(
467
            $this->updatePage->isAnyDefaultTaxZoneChosen(),
468
            'Channel should not have default tax zone, but it has.'
469
        );
470
    }
471
472
    /**
473
     * @Then the tax calculation strategy for the :channel channel should be :taxCalculationStrategy
474
     */
475
    public function theTaxCalculationStrategyForTheChannelShouldBe(ChannelInterface $channel, $taxCalculationStrategy)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $taxCalculationStrategy exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
476
    {
477
        $this->updatePage->open(['id' => $channel->getId()]);
478
479
        Assert::true(
480
            $this->updatePage->isTaxCalculationStrategyChosen($taxCalculationStrategy),
481
            sprintf('Tax calculation strategy %s should be selected, but it is not', $taxCalculationStrategy)
482
        );
483
    }
484
485
    /**
486
     * @param ChannelInterface $channel
487
     * @param bool $state
488
     */
489
    private function assertChannelState(ChannelInterface $channel, $state)
490
    {
491
        $this->iWantToBrowseChannels();
492
493
        Assert::true(
494
            $this->indexPage->isSingleResourceOnPage(
495
                [
496
                    'nameAndDescription' => $channel->getName(),
497
                    'enabled' => $state,
498
                ]
499
            ), sprintf('Channel with name %s and state %s has not been found.', $channel->getName(), $state)
500
        );
501
    }
502
}
503