AuthenticateTwitter::execute()   B
last analyzed

Complexity

Conditions 4
Paths 24

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.5806
cc 4
eloc 19
nc 24
nop 1
1
<?php
2
3
namespace Magium\Twitter\Actions;
4
5
use Facebook\WebDriver\Exception\NoSuchElementException;
6
use Magium\AbstractTestCase;
7
use Magium\Commands\Open;
8
use Magium\Twitter\Themes\Twitter;
9
use Magium\Twitter\Identities\Twitter as TwitterIdentity;
10
use Magium\WebDriver\WebDriver;
11
12
class AuthenticateTwitter
13
{
14
    const ACTION = 'Magium\Twitter\Actions\AuthenticateTwitter';
15
    
16
    protected $webDriver;
17
    protected $theme;
18
    protected $identity;
19
    protected $open;
20
    
21
    public function __construct(
22
        WebDriver $webDriver,
23
        Open $open,
24
        Twitter $theme,
25
        TwitterIdentity $identity
26
    )
27
    {
28
        $this->open = $open;
29
        $this->webDriver    = $webDriver;
30
        $this->theme        = $theme;
31
        $this->identity     = $identity;
32
    }
33
    
34
    public function execute($remember = false)
35
    {
36
        $url = $this->webDriver->getCurrentURL();
37
        if ($url !== $this->theme->getTwitterUrl()) {
38
            $this->open->open($this->theme->getTwitterUrl());
39
        }
40
41
        try {
42
            $element = $this->webDriver->byXpath($this->theme->getLoginButtonXpath());
43
            $element->click();
44
45
            $element = $this->webDriver->byXpath($this->theme->getSiteUsernameXpath());
46
            $element->clear();
47
            $element->sendKeys($this->identity->getUsername());
48
49
            $element = $this->webDriver->byXpath($this->theme->getSitePasswordXpath());
50
            $element->clear();
51
            $element->sendKeys($this->identity->getPassword());
52
53
            if ($remember) {
54
                $element = $this->webDriver->byXpath($this->theme->getRememberXpath());
55
                $element->click();
56
            }
57
58
        } catch (NoSuchElementException $e) {
59
            // Presuming that we're logged in
60
        }
61
        $element = $this->webDriver->byXpath($this->theme->getSiteSignInXpath());
62
        $element->click();
63
    }
64
65
}