Completed
Push — checkout-consistent-prices ( af49ca )
by Kamil
13:24
created

VariantsElement   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A countVariantsOnPage() 0 7 1
A hasProductVariantWithCodePriceAndCurrentStock() 0 16 3
A hasProductWithGivenNameCodePriceAndCurrentStock() 0 13 5
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\Element\Product\ShowPage;
15
16
use Behat\Mink\Element\NodeElement;
17
use FriendsOfBehat\PageObjectExtension\Element\Element;
18
19
final class VariantsElement extends Element implements VariantsElementInterface
20
{
21
    public function countVariantsOnPage(): int
22
    {
23
        /** @var NodeElement $variants|array */
24
        $variants = $this->getDocument()->findAll('css', '#variants .variant');
25
26
        return \count($variants);
27
    }
28
29
    public function hasProductVariantWithCodePriceAndCurrentStock(string $name, string $code, string $price, string $currentStock): bool
30
    {
31
        /** @var NodeElement $variantRow */
32
        $variantRows = $this->getDocument()->findAll('css', '#variants .variant');
33
34
        /** @var NodeElement $variant */
35
        foreach ($variantRows as $variant) {
36
            if (
37
                $this->hasProductWithGivenNameCodePriceAndCurrentStock($variant, $name, $code, $price, $currentStock)
38
            ) {
39
                return true;
40
            }
41
        }
42
43
        return false;
44
    }
45
46
    private function hasProductWithGivenNameCodePriceAndCurrentStock(NodeElement $variant, string $name, string $code, string $price, string $currentStock): bool
47
    {
48
        if (
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $variant->find('c...xt() === $currentStock;.
Loading history...
49
            $variant->find('css', '.title span.variant-name')->getText() === $name &&
50
            $variant->find('css', '.title span.variant-code')->getText() === $code &&
51
            $variant->find('css', '.content .pricing tr:contains("WEB-US") td:nth-child(2)')->getText() === $price &&
52
            $variant->find('css', '.content span.current-stock-label span.current-stock')->getText() === $currentStock
53
        ) {
54
            return true;
55
        }
56
57
        return false;
58
    }
59
}
60