Completed
Pull Request — develop (#27)
by Carlo
03:15
created

DriverSwitcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 40
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
getElementObject() 0 1 ?
getDriver() 0 1 ?
B switchToIFrame() 0 21 5
A switchToWindow() 0 4 1
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
        if ($iFrame instanceof Element) {
34
            $reflectedElement = new \ReflectionClass($iFrame);
35
            if ($reflectedElement->hasMethod('getCssId') === false) {
36
                throw new \ReflectionException("getCssId method not in Element " . get_class($iFrame));
37
            }
38
            $iFrameId = $iFrame->getCssId();
39
            $this->getDriver()->switchToIFrame($iFrameId);
40
        }
41
    }
42
43
    /**
44
     * @param null|string $name
45
     */
46
    public function switchToWindow($name = null)
47
    {
48
        $this->getDriver()->switchToWindow($name);
49
    }
50
}
51