Passed
Pull Request — develop (#28)
by Carlo
05:26
created

DriverSwitcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 1
cbo 0
dl 0
loc 49
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
getElementObject() 0 1 ?
getDriver() 0 1 ?
A switchToIFrame() 0 14 3
A switchToWindow() 0 4 1
A switchToIFrameByCssId() 0 11 3
1
<?php
2
namespace Magefix\Plugin;
3
4
use SensioLabs\Behat\PageObjectExtension\PageObject\Element;
5
6
/**
7
 * Class DriverSwitcher
8
 * @package Magefix\Plugin
9
 * @author  Carlo Tasca <[email protected]>
10
 */
11
trait DriverSwitcher
12
{
13
    public abstract function getElementObject($element);
14
15
    public abstract function getDriver();
16
17
    /**
18
     * @param string | Element $iFrameElement
19
     * @throws \ReflectionException
20
     */
21
    public function switchToIFrame($iFrameElement)
22
    {
23
        $iFrame = null;
24
25
        if (is_string($iFrameElement)) {
26
            $iFrame = $this->getElementObject($iFrameElement);
27
        }
28
29
        if (is_object($iFrameElement)) {
30
            $iFrame = $iFrameElement;
31
        }
32
33
        $this->switchToIFrameByCssId($iFrame);
34
    }
35
36
    /**
37
     * @param null|string $name
38
     */
39
    public function switchToWindow($name = null)
40
    {
41
        $this->getDriver()->switchToWindow($name);
42
    }
43
44
    /**
45
     * @param $iFrame
46
     * @throws \ReflectionException
47
     */
48
    private function switchToIFrameByCssId($iFrame)
49
    {
50
        if ($iFrame instanceof Element) {
51
            $reflectedElement = new \ReflectionClass($iFrame);
52
            if ($reflectedElement->hasMethod('getCssId') === false) {
53
                throw new \ReflectionException("getCssId method not in Element " . get_class($iFrame));
54
            }
55
            $iFrameId = $iFrame->getCssId();
56
            $this->getDriver()->switchToIFrame($iFrameId);
57
        }
58
    }
59
}
60