GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SettingModifier::handleElementSetting()   C
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 22
nc 9
nop 2
1
<?php
2
3
namespace Magium\Magento\Actions\Admin\Configuration;
4
5
use Facebook\WebDriver\WebDriverElement;
6
use Facebook\WebDriver\WebDriverSelect;
7
use Magium\Magento\AbstractMagentoTestCase;
8
use Magium\Magento\Navigators\Admin\SystemConfiguration;
9
use Magium\Magento\Themes\Admin\ThemeConfiguration;
10
use Magium\WebDriver\FastSelectElement;
11
use Magium\WebDriver\WebDriver;
12
13
class SettingModifier
14
{
15
16
    const ACTION = 'Admin\Configuration\SettingModifier';
17
18
    const SETTING_OPTION_YES = 1;
19
    const SETTING_OPTION_NO = 0;
20
21
    protected $webDriver;
22
    protected $testCase;
23
    protected $systemConfigurationNavigator;
24
    protected $themeConfiguration;
25
    protected $save;
26
27
    protected $dataChanged = false;
28
29
    /**
30
     * SettingModifier constructor.
31
     * @param $webDriver
32
     * @param $testCase
33
     * @param $systemConfigurationNavigator
34
     * @param $themeConfiguration
35
     */
36
    public function __construct(
37
        WebDriver                       $webDriver,
38
        AbstractMagentoTestCase         $testCase,
39
        SystemConfiguration             $systemConfigurationNavigator,
40
        ThemeConfiguration              $themeConfiguration,
41
        Save                            $save
42
    )
43
    {
44
        $this->webDriver = $webDriver;
45
        $this->testCase = $testCase;
46
        $this->systemConfigurationNavigator = $systemConfigurationNavigator;
47
        $this->themeConfiguration = $themeConfiguration;
48
        $this->save = $save;
49
    }
50
51
    /**
52
     * Changes a system configuration setting based off of the identifier.  The identifier can be specified in 4
53
     * possible ways.
54
     *
55
     * * Tab/Section::setting_id (will ensure that the correct section is navigated to)
56
     * * setting_id (will not navigate (no section information)
57
     * * Tab/Section::name=Text Of Setting (will ensure that the correct section is navigated to, uses setting name (exact), not ID)
58
     * * name=Text Of Setting (will not navigate, uses setting name (exact) not ID)
59
     *
60
     * @param $identifier
61
     * @param $value
62
     * @throws \Magium\InvalidInstructionException
63
     */
64
65
    public function set($identifier, $value, $save = false)
66
    {
67
        $parts = explode('::', $identifier);
68
        $setting = null;
0 ignored issues
show
Unused Code introduced by
$setting is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
        if (count($parts) === 2) {
70
            $this->systemConfigurationNavigator->navigateTo($parts[0]);
71
            $setting = $parts[1];
72
        } else {
73
            $setting = $parts[0];
74
        }
75
        $matches = null;
76
        if (preg_match('/^label=(.+)$/', $setting, $matches)) {
77
            $xpath = '';
78
            if (count($xpath) === 2) {
79
                $nav = explode('/', $parts[0]);
80
                $xpath = $this->themeConfiguration->getSystemConfigSectionDisplayCheckXpath($nav[1]) . '/descendant::';
81
            }
82
            $xpath .= $this->themeConfiguration->getSystemConfigSettingLabelXpath($matches[1]);
83
            $labelElement = $this->webDriver->byXpath($xpath);
84
            $setting = $labelElement->getAttribute('for');
85
        }
86
87
        $this->handleElementSetting($setting, $value);
88
        if ($save) {
89
            $this->save->save();
90
        }
91
    }
92
93
    protected function handleElementSetting($elementId, $value)
94
    {
95
        $element = $this->webDriver->byId($elementId);
96
97
        if (strtolower($element->getTagName()) === 'select') {
98
            if (!is_array($value)) {
99
                $value = [$value];
100
            }
101
            $xpath = sprintf('//select[@id="%s"]', $elementId);
102
            $select = new FastSelectElement($this->webDriver, $xpath);
103
//            $options = $select->getAllSelectedOptions();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
            $select->clearSelectedOptions();
105
            foreach ($value as $v) {
106
                $xpath = sprintf('//select[@id="%s"]', $elementId);
107
                $matches = null;
108
                if (preg_match('/^label=(.+)$/', $v, $matches)) {
109
                    $xpath .= sprintf('/descendant::option[.="%s"]', str_replace('"', '\"', $matches[1]));
110
                    $this->webDriver->byXpath($xpath)->click();
111
                } else {
112
                    $xpath .= sprintf('/descendant::option[@value="%s"]', str_replace('"', '\"', $v));
113
                    $this->webDriver->byXpath($xpath)->click();
114
                }
115
            }
116
117
        } else if (strtolower($element->getTagName()) === 'input') {
118
            if ($element->getAttribute('value') != $value) {
119
                $element->clear();
120
                $element->sendKeys($value);
121
                $this->dataChanged = true;
122
            }
123
        }
124
    }
125
}