Completed
Push — master ( 9a9a76...da4573 )
by Andrii
12:36
created

Client::findId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace hipanel\tests\_support\Step\Acceptance;
4
5
use Codeception\Scenario;
6
use hipanel\tests\_support\AcceptanceTester;
7
use hipanel\tests\_support\Page\Login;
8
9
class Client extends AcceptanceTester
0 ignored issues
show
Bug introduced by
There is one abstract method getScenario in this class; you could implement it, or declare this class as abstract.
Loading history...
10
{
11
    public function __construct(Scenario $scenario)
12
    {
13
        parent::__construct($scenario);
14
        $this->initCredentials();
15
    }
16
17
    public $id;
18
    protected $username;
19
    protected $password;
20
21
    public function getUsername(): string
22
    {
23
        return $this->username;
24
    }
25
    
26
    public function login()
27
    {
28
        $this->loadOrLogin();
29
30
        return $this->findId();
31
    }
32
33
    protected function loadOrLogin()
34
    {
35
        $sessionName = 'login-' . $this->getClientType();
36
        try {
37
            if ($this->retrieveSession($sessionName)) {
38
                return $this;
39
            }
40
        } catch (\Facebook\WebDriver\Exception\UnknownServerException $exception) {
0 ignored issues
show
Bug introduced by
The class Facebook\WebDriver\Excep...\UnknownServerException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
41
            // User is already logged in, but trying to open a session on a page that is not loaded
42
            return $this;
43
        }
44
45
        $this->restartBrowser();
46
        $hiam = new Login($this);
47
        $hiam->login($this->username, $this->password);
48
        $this->storeSession($sessionName);
49
50
        return $this;
51
    }
52
53
    protected function findId(): self
54
    {
55
        if (empty($this->id)) {
56
            $this->amOnPage('/site/healthcheck');
57
            $this->id = $this->grabTextFrom('userId');
58
            if (!$this->id) {
59
                throw new \Exception('failed detect user ID');
60
            }
61
        }
62
63
        return $this;
64
    }
65
66
    protected $clientType;
67
68
    protected function getClientType(): string
69
    {
70
        if ($this->clientType === null) {
71
            $this->clientType = strtolower((new \ReflectionClass($this))->getShortName());
72
        }
73
74
        return $this->clientType;
75
    }
76
77
    protected function initCredentials()
78
    {
79
        $func = 'get' . $this->getClientType() . 'Credentials';
80
        [$this->id, $this->username, $this->password] = $this->{$func}();
81
    }
82
}
83