Passed
Push — master ( 6296de...3eb4db )
by Kirill
03:34
created

State::getVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Debug;
13
14
use Spiral\Debug\Exception\StateException;
15
use Spiral\Logger\Event\LogEvent;
16
17
/**
18
 * Describes current process state.
19
 */
20
final class State implements StateInterface
21
{
22
    /** @var array */
23
    private $tags = [];
24
25
    /** @var array */
26
    private $extras = [];
27
28
    /** @var array */
29
    private $logEvents = [];
30
31
    /**
32
     * @param array $tags
33
     */
34
    public function setTags(array $tags): void
35
    {
36
        $setTags = [];
37
        foreach ($tags as $key => $value) {
38
            if (!is_string($value)) {
39
                throw new StateException(sprintf(
40
                    'Invalid tag value, string expected got %s',
41
                    is_object($value) ? get_class($value) : gettype($value)
42
                ));
43
            }
44
45
            $setTags[(string)$key] = $value;
46
        }
47
48
        $this->tags = $setTags;
49
    }
50
51
    /**
52
     * @param string $key
53
     * @param string $value
54
     */
55
    public function setTag(string $key, string $value): void
56
    {
57
        $this->tags[$key] = $value;
58
    }
59
60
    /**
61
     * Get current key-value description.
62
     *
63
     * @return array
64
     */
65
    public function getTags(): array
66
    {
67
        return $this->tags;
68
    }
69
70
    /**
71
     * @param array $extras
72
     */
73
    public function setVariables(array $extras): void
74
    {
75
        $this->extras = $extras;
76
    }
77
78
    /**
79
     * @param string $key
80
     * @param        $value
81
     */
82
    public function setVariable(string $key, $value): void
83
    {
84
        $this->extras[$key] = $value;
85
    }
86
87
    /**
88
     * Get current state metadata. Arbitrary array form.
89
     *
90
     * @return array
91
     */
92
    public function getVariables(): array
93
    {
94
        return $this->extras;
95
    }
96
97
    /**
98
     * @param LogEvent ...$events
99
     */
100
    public function addLogEvent(LogEvent ...$events): void
101
    {
102
        $this->logEvents = array_merge($this->logEvents, $events);
103
    }
104
105
    /**
106
     * @return LogEvent[]
107
     */
108
    public function getLogEvents(): array
109
    {
110
        return $this->logEvents;
111
    }
112
113
    /**
114
     * Reset the state.
115
     */
116
    public function reset(): void
117
    {
118
        $this->tags = [];
119
        $this->extras = [];
120
        $this->logEvents = [];
121
    }
122
}
123