Completed
Push — master ( 239c91...59afdf )
by Dmitry
02:55
created

CredentialsProvider   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 114
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getClientCredentials() 0 4 1
A getSellerCredentials() 0 4 1
A getAdminCredentials() 0 4 1
A restartBrowser() 0 6 1
A needPage() 0 9 2
A storeSession() 0 15 1
A retrieveSession() 0 20 2
A persistSessionSnapshots() 0 4 1
A readSessionSnapshots() 0 14 3
1
<?php
2
namespace hipanel\tests\_support\Helper;
3
4
use Codeception\Exception\ModuleException;
5
use Codeception\Lib\ModuleContainer;
6
use Codeception\Module\WebDriver;
7
use hipanel\helpers\Url;
8
9
/**
10
 * Class CredentialsProvider
11
 *
12
 * @author Dmytro Naumenko <[email protected]>
13
 */
14
class CredentialsProvider extends \Codeception\Module
15
{
16
    /**
17
     * @var array
18
     */
19
    private $sessionSnapshots = [];
20
21
    /**
22
     * @var string
23
     */
24
    private $filename = WEBAPP_ROOT_DIR . '/tests/_data/sessionSnapshots.data';
25
26
    public function __construct(ModuleContainer $moduleContainer, $config = null)
27
    {
28
        parent::__construct($moduleContainer, $config);
29
30
        $this->requiredFields = [
31
            // Client
32
            'client.id', 'client.login', 'client.password',
33
            'seller.id', 'seller.login', 'seller.password',
34
            'admin.id', 'admin.login', 'admin.password',
35
        ];
36
    }
37
38
    public function getClientCredentials(): array
39
    {
40
        return [$this->config['client.id'], $this->config['client.login'], $this->config['client.password']];
41
    }
42
43
44
    public function getSellerCredentials(): array
45
    {
46
        return [$this->config['seller.id'], $this->config['seller.login'], $this->config['seller.password']];
47
    }
48
49
    public function getAdminCredentials(): array
50
    {
51
        return [$this->config['admin.id'], $this->config['admin.login'], $this->config['admin.password']];
52
    }
53
54
    public function restartBrowser()
55
    {
56
        /** @var WebDriver $wd */
57
        $wd = $this->getModule('WebDriver');
58
        $wd->_restart();
59
    }
60
61
    public function needPage(string $url): void
62
    {
63
        /** @var WebDriver $wd */
64
        $wd = $this->getModule('WebDriver');
65
        $currentUrl = $wd->grabFromCurrentUrl();
66
        if ($currentUrl !== $url) {
67
            $wd->amOnPage($url);
68
        }
69
    }
70
71
    public function storeSession(string $name): void
72
    {
73
        /** @var WebDriver $wd */
74
        $wd = $this->getModule('WebDriver');
75
        $wd->saveSessionSnapshot($name);
76
77
        $reflection = new \ReflectionObject($wd);
78
        $property = $reflection->getProperty('sessionSnapshots');
79
        $property->setAccessible(true);
80
        $snapshots = $property->getValue($wd);
81
        $property->setAccessible(false);
82
83
        $this->sessionSnapshots[$name] = $snapshots[$name];
84
        $this->persistSessionSnapshots();
85
    }
86
87
    public function retrieveSession(string $name): bool
88
    {
89
        $this->readSessionSnapshots();
90
91
        /** @var WebDriver $wd */
92
        $wd = $this->getModule('WebDriver');
93
94
        try {
95
            $wd->_getCurrentUri();
96
        } catch (ModuleException $e) {
0 ignored issues
show
Bug introduced by
The class Codeception\Exception\ModuleException 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...
97
            $wd->amOnPage(Url::to('/site/healthcheck'));
98
        }
99
        $reflection = new \ReflectionObject($wd);
100
        $property = $reflection->getProperty('sessionSnapshots');
101
        $property->setAccessible(true);
102
        $property->setValue($wd, array_merge($property->getValue($wd), $this->sessionSnapshots));
103
        $property->setAccessible(false);
104
105
        return $wd->loadSessionSnapshot($name);
106
    }
107
108
    private function persistSessionSnapshots()
109
    {
110
        file_put_contents($this->filename, serialize($this->sessionSnapshots));
111
    }
112
113
    private function readSessionSnapshots()
114
    {
115
        if (!file_exists($this->filename)) {
116
            return;
117
        }
118
119
        $expires = strtotime('+1 day', filemtime($this->filename));
120
        $now = time();
121
        if ($now > $expires) {
122
            unlink($this->filename);
123
        } else {
124
            $this->sessionSnapshots = unserialize(file_get_contents($this->filename));
125
        }
126
    }
127
}
128