CredentialsProvider::readSessionSnapshots()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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