Completed
Push — symfony3 ( 06e2ee...17e915 )
by Kamil
46:11 queued 26:57
created

GeneratePage   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 2
cbo 4
dl 0
loc 84
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 4 1
A specifyPrice() 0 4 1
A nameCode() 0 4 1
A removeVariant() 0 6 1
A getRouteName() 0 4 1
A getValidationMessage() 0 10 2
A getDefinedElements() 0 7 1
A getValidatedField() 0 8 3
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\Page\Admin\ProductVariant;
13
14
use Behat\Mink\Element\NodeElement;
15
use Behat\Mink\Exception\ElementNotFoundException;
16
use Sylius\Behat\Page\SymfonyPage;
17
18
/**
19
 * @author Łukasz Chruściel <[email protected]>
20
 */
21
class GeneratePage extends SymfonyPage implements GeneratePageInterface
22
{
23
    public function generate()
24
    {
25
        $this->getDocument()->pressButton('Generate');
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function specifyPrice($nth, $price)
32
    {
33
        $this->getDocument()->fillField(sprintf('sylius_product_generate_variants_variants_%s_price', $nth), $price);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function nameCode($nth, $code)
40
    {
41
        $this->getDocument()->fillField(sprintf('sylius_product_generate_variants_variants_%s_code', $nth), $code);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function removeVariant($nth)
48
    {
49
        $item = $this->getDocument()->find('css', sprintf('div[data-form-collection-index="%s"]', $nth));
50
51
        $item->clickLink('Delete');
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getRouteName()
58
    {
59
        return 'sylius_admin_product_variant_generate';
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     *
65
     * @throws ElementNotFoundException
66
     */
67
    public function getValidationMessage($element, $position)
68
    {
69
        $foundElement = $this->getElement($element, ['%position%' => $position]);
70
        $validatedField = $this->getValidatedField($foundElement);
71
        if (null === $validatedField) {
72
            throw new ElementNotFoundException($this->getSession(), 'Element', 'css', $foundElement);
0 ignored issues
show
Documentation introduced by
$foundElement is of type object<Behat\Mink\Element\NodeElement>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
        }
74
75
        return $validatedField->find('css', '.sylius-validation-error')->getText();
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    protected function getDefinedElements()
82
    {
83
        return array_merge(parent::getDefinedElements(), [
84
            'code' => '#sylius_product_generate_variants_variants_%position%_code',
85
            'price' => '#sylius_product_generate_variants_variants_%position%_price',
86
        ]);
87
    }
88
89
    /**
90
     * @param NodeElement $element
91
     *
92
     * @return NodeElement
0 ignored issues
show
Documentation introduced by
Should the return type not be NodeElement|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
93
     *
94
     * @throws ElementNotFoundException
95
     */
96
    private function getValidatedField(NodeElement $element)
97
    {
98
        while (null !== $element && !$element->hasClass('field')) {
99
            $element = $element->getParent();
100
        }
101
102
        return $element;
103
    }
104
}
105