SessionStoragePlugin   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 48
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A beforeInit() 0 6 2
A beforeSetData() 0 22 4
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\SessionUnblocker\Plugin;
5
6
use IntegerNet\SessionUnblocker\MethodLog;
7
use Magento\Framework\Session\Storage;
8
use Magento\Framework\Session\StorageInterface;
9
10
/**
11
 * This plugin is currently only used for testing and debugging. It can detect, when sessions are started and modified.
12
 */
13
class SessionStoragePlugin
14
{
15
    /**
16
     * @var bool
17
     */
18
    private $doLogMethods = false;
19
20
    /**
21
     * @var MethodLog
22
     */
23
    private $methodLog;
24
25 6
    public function __construct(MethodLog $methodLog, bool $doLogMethods = false)
26
    {
27 6
        $this->doLogMethods = $doLogMethods;
28 6
        $this->methodLog = $methodLog;
29 6
    }
30
31 6
    public function beforeSetData(Storage $subject, $key, $value = null)
32
    {
33 6
        if ($this->doLogMethods) {
34 6
            if (is_array($key)) {
35 3
                $this->methodLog->logSessionModify(
36 3
                    get_parent_class($subject),
37 3
                    'setData',
38 3
                    $subject->getNamespace(),
39
40 3
                    '[...]'
41
                );
42 6
            } elseif ($subject->getData($key) !== $value) {
43 6
                $this->methodLog->logSessionModify(
44 6
                    get_parent_class($subject),
45 6
                    'setData',
46 6
                    $subject->getNamespace(),
47 4
                    $key
48
                );
49
            }
50
        }
51
52 6
        return [$key, $value];
53
    }
54
55 6
    public function beforeInit(StorageInterface $subject, array $data)
56
    {
57 6
        if ($this->doLogMethods) {
58 6
            MethodLog::instance()->logSessionStart(get_parent_class($subject), 'init', $subject->getNamespace());
59
        }
60 6
        return [$data];
61
    }
62
}