Completed
Push — to-be-a-hat-or-not-to-be-kuhwa ( 7c7f7b...b72886 )
by Kamil
20:35
created

ManagingTaxonsContext   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 427
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 6
dl 0
loc 427
rs 8.2608
c 0
b 0
f 0

40 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A iWantToCreateANewTaxon() 0 4 1
A iWantToModifyATaxon() 0 6 1
A iSpecifyItsCodeAs() 0 4 1
A iNameItIn() 0 4 1
A iRenameItIn() 0 4 1
A iChangeItsDescriptionToIn() 0 4 1
A iSpecifyItsPermalinkAs() 0 4 1
A iChangeItsPermalinkToIn() 0 4 1
A iDescribeItAs() 0 4 1
A iChooseAsAParentTaxon() 0 4 1
A iChangeItsParentTaxonTo() 0 4 1
A iDoNotSpecifyItsCode() 0 4 1
A iDoNotSpecifyItsName() 0 4 1
A iDeleteTaxonNamed() 0 5 1
A iAddIt() 0 4 1
A iSaveMyChanges() 0 4 1
A theTaxonShouldAppearInTheRegistry() 0 8 1
A thisTaxonElementShouldBe() 0 7 1
A theCodeFieldShouldBeDisabled() 0 7 1
A thisTaxonShouldBelongsTo() 0 7 1
A iShouldBeNotifiedThatTaxonWithThisCodeAlreadyExists() 0 7 1
A iShouldBeNotifiedThatIsRequired() 0 7 1
A thereShouldStillBeOnlyOneTaxonWithCode() 0 7 1
A taxonNamedShouldNotBeAdded() 0 8 1
A iShouldSeeTaxonsInTheList() 0 10 1
A iShouldSeeTheTaxonNamedInTheList() 0 8 1
A iAttachImageWithACode() 0 7 1
A iAttachImageWithoutACode() 0 4 1
A thisTaxonShouldHaveAnImageWithCode() 0 7 1
A thisTaxonShouldNotHaveAnImageWithCode() 0 7 1
A iRemoveAnImageWithACode() 0 4 1
A iRemoveTheFirstImage() 0 4 1
A thisTaxonShouldNotHaveImages() 0 10 1
A iChangeItsImageToPathForTheCode() 0 4 1
A iShouldBeNotifiedThatTheImageWithThisCodeAlreadyExists() 0 4 1
A iShouldBeNotifiedThatAnImageCodeIsRequired() 0 7 1
A thereShouldStillBeOnlyOneImageInThisTaxon() 0 10 1
A theImageCodeFieldShouldBeDisabled() 0 7 1
A iShouldBeNotifiedThatTheFirstImageShouldHaveAnUniqueCode() 0 9 1

How to fix   Complexity   

Complex Class

Complex classes like ManagingTaxonsContext 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 ManagingTaxonsContext, 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\Page\Admin\Taxon\CreatePageInterface;
16
use Sylius\Behat\Page\Admin\Taxon\UpdatePageInterface;
17
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
18
use Sylius\Behat\Service\SharedStorageInterface;
19
use Sylius\Component\Core\Model\TaxonInterface;
20
use Webmozart\Assert\Assert;
21
22
/**
23
 * @author Arkadiusz Krakowiak <[email protected]>
24
 */
25
final class ManagingTaxonsContext implements Context
26
{
27
    /**
28
     * @var SharedStorageInterface
29
     */
30
    private $sharedStorage;
31
32
    /**
33
     * @var CreatePageInterface
34
     */
35
    private $createPage;
36
37
    /**
38
     * @var UpdatePageInterface
39
     */
40
    private $updatePage;
41
42
    /**
43
     * @var CurrentPageResolverInterface
44
     */
45
    private $currentPageResolver;
46
47
    /**
48
     * @param SharedStorageInterface $sharedStorage
49
     * @param CreatePageInterface $createPage
50
     * @param UpdatePageInterface $updatePage
51
     * @param CurrentPageResolverInterface $currentPageResolver
52
     */
53
    public function __construct(
54
        SharedStorageInterface $sharedStorage,
55
        CreatePageInterface $createPage,
56
        UpdatePageInterface $updatePage,
57
        CurrentPageResolverInterface $currentPageResolver
58
    ) {
59
        $this->sharedStorage = $sharedStorage;
60
        $this->createPage = $createPage;
61
        $this->updatePage = $updatePage;
62
        $this->currentPageResolver = $currentPageResolver;
63
    }
64
65
    /**
66
     * @Given I want to create a new taxon
67
     * @Given I want to see all taxons in store
68
     */
69
    public function iWantToCreateANewTaxon()
70
    {
71
        $this->createPage->open();
72
    }
73
74
    /**
75
     * @Given /^I want to modify the ("[^"]+" taxon)$/
76
     */
77
    public function iWantToModifyATaxon(TaxonInterface $taxon)
78
    {
79
        $this->sharedStorage->set('taxon', $taxon);
80
81
        $this->updatePage->open(['id' => $taxon->getId()]);
82
    }
83
84
    /**
85
     * @When I specify its code as :code
86
     */
87
    public function iSpecifyItsCodeAs($code)
88
    {
89
        $this->createPage->specifyCode($code);
90
    }
91
92
    /**
93
     * @When I name it :name in :language
94
     */
95
    public function iNameItIn($name, $language)
96
    {
97
        $this->createPage->nameIt($name, $language);
98
    }
99
100
    /**
101
     * @When I rename it to :name in :language
102
     */
103
    public function iRenameItIn($name, $language)
104
    {
105
        $this->updatePage->nameIt($name, $language);
106
    }
107
108
    /**
109
     * @When I change its description to :description in :language
110
     */
111
    public function iChangeItsDescriptionToIn($description, $language)
112
    {
113
        $this->updatePage->describeItAs($description, $language);
114
    }
115
116
    /**
117
     * @When I specify its permalink as :permalink in :language
118
     */
119
    public function iSpecifyItsPermalinkAs($permalink, $language)
120
    {
121
        $this->createPage->specifyPermalink($permalink, $language);
122
    }
123
124
    /**
125
     * @When I change its permalink to :permalink in :language
126
     */
127
    public function iChangeItsPermalinkToIn($permalink, $language)
128
    {
129
        $this->updatePage->specifyPermalink($permalink, $language);
130
    }
131
132
    /**
133
     * @When I describe it as :description in :language
134
     */
135
    public function iDescribeItAs($description, $language)
136
    {
137
        $this->createPage->describeItAs($description, $language);
138
    }
139
140
    /**
141
     * @Given /^I choose ("[^"]+" as a parent taxon)$/
142
     */
143
    public function iChooseAsAParentTaxon(TaxonInterface $taxon)
144
    {
145
        $this->createPage->chooseParent($taxon);
146
    }
147
148
    /**
149
     * @Given /^I change its (parent taxon to "[^"]+")$/
150
     */
151
    public function iChangeItsParentTaxonTo(TaxonInterface $taxon)
152
    {
153
        $this->updatePage->chooseParent($taxon);
154
    }
155
156
    /**
157
     * @When I do not specify its code
158
     */
159
    public function iDoNotSpecifyItsCode()
160
    {
161
        // Intentionally left blank to fulfill context expectation
162
    }
163
164
    /**
165
     * @When I do not specify its name
166
     */
167
    public function iDoNotSpecifyItsName()
168
    {
169
        // Intentionally left blank to fulfill context expectation
170
    }
171
172
    /**
173
     * @When I delete taxon named :name
174
     */
175
    public function iDeleteTaxonNamed($name)
176
    {
177
        $this->createPage->open();
178
        $this->createPage->deleteTaxonOnPageByName($name);
179
    }
180
181
    /**
182
     * @When I add it
183
     * @When I try to add it
184
     */
185
    public function iAddIt()
186
    {
187
        $this->createPage->create();
188
    }
189
190
    /**
191
     * @When I save my changes
192
     * @When I try to save my changes
193
     */
194
    public function iSaveMyChanges()
195
    {
196
        $this->updatePage->saveChanges();
197
    }
198
199
    /**
200
     * @Then /^the ("[^"]+" taxon) should appear in the registry$/
201
     */
202
    public function theTaxonShouldAppearInTheRegistry(TaxonInterface $taxon)
203
    {
204
        $this->updatePage->open(['id' => $taxon->getId()]);
205
        Assert::true(
206
            $this->updatePage->hasResourceValues(['code' => $taxon->getCode()]),
207
            sprintf('Taxon %s should be in the registry.', $taxon->getName())
208
        );
209
    }
210
211
    /**
212
     * @Then this taxon :element should be :value
213
     */
214
    public function thisTaxonElementShouldBe($element, $value)
215
    {
216
        Assert::true(
217
            $this->updatePage->hasResourceValues([$element => $value]),
218
            sprintf('Taxon with %s should have %s value.', $element, $value)
219
        );
220
    }
221
222
    /**
223
     * @Then the code field should be disabled
224
     */
225
    public function theCodeFieldShouldBeDisabled()
226
    {
227
        Assert::true(
228
            $this->updatePage->isCodeDisabled(),
229
            'Code field should be disabled but it is not.'
230
        );
231
    }
232
233
    /**
234
     * @Then /^this taxon should (belongs to "[^"]+")$/
235
     */
236
    public function thisTaxonShouldBelongsTo(TaxonInterface $taxon)
237
    {
238
        Assert::true(
239
            $this->updatePage->hasResourceValues(['parent' => $taxon->getId()]),
240
            sprintf('Current taxon should have %s parent taxon.', $taxon->getName())
241
        );
242
    }
243
244
    /**
245
     * @Then I should be notified that taxon with this code already exists
246
     */
247
    public function iShouldBeNotifiedThatTaxonWithThisCodeAlreadyExists()
248
    {
249
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
250
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
251
252
        Assert::same($currentPage->getValidationMessage('code'), 'Taxon with given code already exists.');
253
    }
254
255
    /**
256
     * @Then I should be notified that :element is required
257
     */
258
    public function iShouldBeNotifiedThatIsRequired($element)
259
    {
260
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
261
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
262
263
        Assert::same($currentPage->getValidationMessage($element), sprintf('Please enter taxon %s.', $element));
264
    }
265
266
    /**
267
     * @Then /^there should still be only one taxon with code "([^"]+)"$/
268
     */
269
    public function thereShouldStillBeOnlyOneTaxonWithCode($code)
270
    {
271
        Assert::true(
272
            $this->updatePage->hasResourceValues(['code' => $code]),
273
            sprintf('Taxon with code %s cannot be found.', $code)
274
        );
275
    }
276
277
    /**
278
     * @Then /^Taxon named "([^"]+)" should not be added$/
279
     * @Then the taxon named :name should no longer exist in the registry
280
     */
281
    public function taxonNamedShouldNotBeAdded($name)
282
    {
283
        Assert::eq(
284
            0,
285
            $this->createPage->countTaxonsByName($name),
286
            sprintf('Taxon %s should not exist.', $name)
287
        );
288
    }
289
290
    /**
291
     * @Then /^I should see (\d+) taxons on the list$/
292
     */
293
    public function iShouldSeeTaxonsInTheList($number)
294
    {
295
        $taxonsOnPage = $this->createPage->countTaxons();
296
297
        Assert::same(
298
            (int) $number,
299
            $taxonsOnPage,
300
            sprintf('On list should be %d taxons but get %d.', $number, $taxonsOnPage)
301
        );
302
    }
303
304
    /**
305
     * @Then I should see the taxon named :name in the list
306
     */
307
    public function iShouldSeeTheTaxonNamedInTheList($name)
308
    {
309
        Assert::eq(
310
            1,
311
            $this->createPage->countTaxonsByName($name),
312
            sprintf('Taxon %s does not exist or multiple taxons with this name exist.', $name)
313
        );
314
    }
315
316
    /**
317
     * @When I attach the :path image with a code :code
318
     */
319
    public function iAttachImageWithACode($path, $code)
320
    {
321
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
322
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
323
324
        $currentPage->attachImage($path, $code);
325
    }
326
327
    /**
328
     * @When I attach the :path image without a code
329
     */
330
    public function iAttachImageWithoutACode($path)
331
    {
332
        $this->updatePage->attachImage($path);
333
    }
334
335
    /**
336
     * @Then /^this taxon should have(?:| also) an image with a code "([^"]*)"$/
337
     */
338
    public function thisTaxonShouldHaveAnImageWithCode($code)
339
    {
340
        Assert::true(
341
            $this->updatePage->isImageWithCodeDisplayed($code),
342
            sprintf('Image with a code %s should have been displayed.', $code)
343
        );
344
    }
345
346
    /**
347
     * @Then /^this taxon should not have(?:| also) an image with a code "([^"]*)"$/
348
     */
349
    public function thisTaxonShouldNotHaveAnImageWithCode($code)
350
    {
351
        Assert::false(
352
            $this->updatePage->isImageWithCodeDisplayed($code),
353
            sprintf('Image with a code %s should not have been displayed.', $code)
354
        );
355
    }
356
357
    /**
358
     * @When /^I remove(?:| also) an image with a code "([^"]*)"$/
359
     */
360
    public function iRemoveAnImageWithACode($code)
361
    {
362
        $this->updatePage->removeImageWithCode($code);
363
    }
364
365
    /**
366
     * @When I remove the first image
367
     */
368
    public function iRemoveTheFirstImage()
369
    {
370
        $this->updatePage->removeFirstImage();
371
    }
372
373
    /**
374
     * @Then /^(this taxon) should not have any images$/
375
     */
376
    public function thisTaxonShouldNotHaveImages(TaxonInterface $taxon)
377
    {
378
        $this->iWantToModifyATaxon($taxon);
379
380
        Assert::eq(
381
            0,
382
            $this->updatePage->countImages(),
383
            'This taxon has %2$s, but it should not have.'
384
        );
385
    }
386
387
    /**
388
     * @When I change the image with the :code code to :path
389
     */
390
    public function iChangeItsImageToPathForTheCode($path, $code)
391
    {
392
        $this->updatePage->changeImageWithCode($code, $path);
393
    }
394
395
    /**
396
     * @Then I should be notified that the image with this code already exists
397
     */
398
    public function iShouldBeNotifiedThatTheImageWithThisCodeAlreadyExists()
399
    {
400
        Assert::same($this->updatePage->getValidationMessageForImage(), 'Image code must be unique within this taxon.');
401
    }
402
403
    /**
404
     * @Then I should be notified that an image code is required
405
     */
406
    public function iShouldBeNotifiedThatAnImageCodeIsRequired()
407
    {
408
        Assert::same(
409
            $this->updatePage->getValidationMessageForImage(),
410
            'Please enter an image code.'
411
        );
412
    }
413
414
    /**
415
     * @Then there should still be only one image in the :taxon taxon
416
     */
417
    public function thereShouldStillBeOnlyOneImageInThisTaxon(TaxonInterface $taxon)
418
    {
419
        $this->iWantToModifyATaxon($taxon);
420
421
        Assert::eq(
422
            1,
423
            $this->updatePage->countImages(),
424
            'This taxon has %2$s images, but it should have only one.'
425
        );
426
    }
427
428
    /**
429
     * @Then the image code field should be disabled
430
     */
431
    public function theImageCodeFieldShouldBeDisabled()
432
    {
433
        Assert::true(
434
            $this->updatePage->isImageCodeDisabled(),
435
            'Image code field should be disabled but it is not.'
436
        );
437
    }
438
439
    /**
440
     * @Then I should be notified that the :imageNumber image should have an unique code
441
     */
442
    public function iShouldBeNotifiedThatTheFirstImageShouldHaveAnUniqueCode($imageNumber)
443
    {
444
        preg_match_all('!\d+!', $imageNumber, $matches);
445
446
        Assert::same(
447
            $this->updatePage->getValidationMessageForImageAtPlace(((int) $matches[0][0]) - 1),
448
            'Image code must be unique within this taxon.'
449
        );
450
    }
451
}
452