MethodLog::logControllerAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\SessionUnblocker;
5
6
use Magento\TestFramework\Helper\Bootstrap;
0 ignored issues
show
Bug introduced by
The type Magento\TestFramework\Helper\Bootstrap was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
class MethodLog
9
{
10
    /**
11
     * @var string[]
12
     */
13
    private $calls = [];
14
    /**
15
     * @var string[]
16
     */
17
    private $callDetails = [];
18
19
    private const SESSION_START = 'session_start';
20
21
    private const SESSION_MODIFY = 'session_modify';
22
23
    private const CONTROLLER_ACTION = 'controller_action';
24
25
    private const SESSION_WRITE_CLOSE = 'session_write_close';
26
27 6
    public static function instance(): self
28
    {
29
        // should only be used in integration test context
30 6
        if (class_exists(Bootstrap::class)) {
31 6
            return Bootstrap::getObjectManager()->get(MethodLog::class);
32
        }
33
34
        return new self;
35
    }
36
37 6
    public function logSessionStart(string $class, string $method, string $namespace): void
38
    {
39 6
        $this->log(self::SESSION_START, $class, $method, $namespace);
40 6
    }
41
42 6
    public function logSessionModify(string $class, string $method, string $namespace, string $key)
43
    {
44 6
        $this->log(self::SESSION_MODIFY, $class, $method, $namespace, $key);
45 6
    }
46
47 3
    public function logControllerAction(string $class, string $method)
48
    {
49 3
        $this->log(self::CONTROLLER_ACTION, $class, $method);
50 3
    }
51
52 3
    public function logWriteClose(string $class, string $method)
53
    {
54 3
        $this->log(self::SESSION_WRITE_CLOSE, $class, $method);
55 3
    }
56
57 6
    private function log(string $category, string $class, string $method, ...$args): void
58
    {
59 6
        $this->calls[] = $category;
60 6
        $this->callDetails[] = $class . '::' . $method . '(' . implode(',', $args) . ')';
61 6
    }
62
63 6
    public function asString(): string
64
    {
65 6
        return implode("\n", $this->callDetails);
66
    }
67
68 3
    public function hasSessionStartAfterAction(): bool
69
    {
70 3
        $actionPosition = array_search(self::CONTROLLER_ACTION, $this->calls);
71 3
        $sessionStartPositions = array_keys($this->calls, self::SESSION_START);
72 3
        if (empty($sessionStartPositions)) {
73
            return false;
74
        }
75 3
        return max($sessionStartPositions) > $actionPosition;
76
    }
77
78 3
    public function hasSessionStartBeforeAction(): bool
79
    {
80 3
        $actionPosition = array_search(self::CONTROLLER_ACTION, $this->calls);
81 3
        $sessionStartPositions = array_keys($this->calls, self::SESSION_START);
82 3
        if (empty($sessionStartPositions)) {
83
            return false;
84
        }
85 3
        return min($sessionStartPositions) < $actionPosition;
86
    }
87
88 3
    public function hasSessionWriteCloseBeforeAction(): bool
89
    {
90 3
        $actionPosition = array_search(self::CONTROLLER_ACTION, $this->calls);
91 3
        $writeClosePositions = array_keys($this->calls, self::SESSION_WRITE_CLOSE);
92 3
        if (empty($writeClosePositions)) {
93
            return false;
94
        }
95 3
        return max($writeClosePositions) < $actionPosition;
96
    }
97
98 3
    public function hasModifyAfterSessionWriteClose()
99
    {
100 3
        $writeClosePosition = array_search(self::SESSION_WRITE_CLOSE, $this->calls);
101 3
        $modifyPositions = array_keys($this->calls, self::SESSION_MODIFY);
102 3
        if (empty($modifyPositions)) {
103
            return false;
104
        }
105 3
        return max($modifyPositions) > $writeClosePosition;
106
    }
107
108 3
    public function reset(): void
109
    {
110 3
        $this->calls = [];
111 3
        $this->callDetails = [];
112 3
    }
113
}
114