|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Metabor\Statemachine\Observer; |
|
4
|
|
|
|
|
5
|
|
|
use Metabor\Statemachine\Statemachine; |
|
6
|
|
|
use Metabor\StringConverter; |
|
7
|
|
|
use MetaborStd\Statemachine\StatemachineInterface; |
|
8
|
|
|
use MetaborStd\Statemachine\TransitionInterface; |
|
9
|
|
|
use MetaborStd\StringConverterInterface; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
use Psr\Log\LogLevel; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author otischlinger |
|
15
|
|
|
*/ |
|
16
|
|
|
class TransitionLogger implements \SplObserver |
|
17
|
|
|
{ |
|
18
|
|
|
const CONTEXT_SUBJECT = 'subject'; |
|
19
|
|
|
const CONTEXT_CURRENT_STATE = 'currentState'; |
|
20
|
|
|
const CONTEXT_LAST_STATE = 'lastState'; |
|
21
|
|
|
const CONTEXT_TRANSITION = 'transition'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var LoggerInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $logger; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $loggerLevel; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var StringConverterInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $stringConverter; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param LoggerInterface $logger |
|
40
|
|
|
* @param string $loggerLevel |
|
41
|
|
|
* @param StringConverterInterface $stringConverter |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function __construct(LoggerInterface $logger, $loggerLevel = LogLevel::INFO, StringConverterInterface $stringConverter = null) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$this->logger = $logger; |
|
46
|
1 |
|
$this->loggerLevel = $loggerLevel; |
|
47
|
1 |
|
if ($stringConverter) { |
|
48
|
|
|
$this->stringConverter = $stringConverter; |
|
49
|
|
|
} else { |
|
50
|
1 |
|
$this->stringConverter = new StringConverter(); |
|
51
|
|
|
} |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param StatemachineInterface $stateMachine |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
1 |
|
protected function createLoggerContext(StatemachineInterface $stateMachine) |
|
60
|
|
|
{ |
|
61
|
1 |
|
$context = array(); |
|
62
|
1 |
|
$context[self::CONTEXT_SUBJECT] = $stateMachine->getSubject(); |
|
63
|
1 |
|
$context[self::CONTEXT_CURRENT_STATE] = $stateMachine->getCurrentState(); |
|
64
|
1 |
|
if ($stateMachine instanceof Statemachine) { |
|
65
|
1 |
|
$context[self::CONTEXT_LAST_STATE] = $stateMachine->getLastState(); |
|
66
|
1 |
|
$context[self::CONTEXT_TRANSITION] = $stateMachine->getSelectedTransition(); |
|
67
|
1 |
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
return $context; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $context |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
1 |
|
protected function createLoggerMessage(array $context) |
|
78
|
|
|
{ |
|
79
|
1 |
|
$message = 'Transition'; |
|
80
|
|
|
|
|
81
|
1 |
|
if (isset($context[self::CONTEXT_SUBJECT])) { |
|
82
|
1 |
|
$message .= ' for "' . $this->stringConverter->convertToString($context[self::CONTEXT_SUBJECT]) . '"'; |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
if (isset($context[self::CONTEXT_LAST_STATE])) { |
|
86
|
|
|
$message .= ' from "' . $this->stringConverter->convertToString($context[self::CONTEXT_LAST_STATE]) . '"'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
if (isset($context[self::CONTEXT_CURRENT_STATE])) { |
|
90
|
1 |
|
$message .= ' to "' . $this->stringConverter->convertToString($context[self::CONTEXT_CURRENT_STATE]) . '"'; |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
if (isset($context[self::CONTEXT_TRANSITION])) { |
|
94
|
|
|
/* @var $transition TransitionInterface */ |
|
95
|
|
|
$transition = $context[self::CONTEXT_TRANSITION]; |
|
96
|
|
|
$eventName = $transition->getEventName(); |
|
97
|
|
|
$condition = $transition->getConditionName(); |
|
98
|
|
|
|
|
99
|
|
|
if ($eventName || $condition) { |
|
100
|
|
|
$message .= ' with'; |
|
101
|
|
|
if ($eventName) { |
|
102
|
|
|
$message .= ' event "' . $eventName . '"'; |
|
103
|
|
|
} |
|
104
|
|
|
if ($eventName) { |
|
105
|
|
|
$message .= ' condition "' . $condition . '"'; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
return $message; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @see SplObserver::update() |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public function update(\SplSubject $stateMachine) |
|
117
|
|
|
{ |
|
118
|
1 |
|
if ($stateMachine instanceof StatemachineInterface) { |
|
119
|
1 |
|
$context = $this->createLoggerContext($stateMachine); |
|
120
|
1 |
|
$message = $this->createLoggerMessage($context); |
|
121
|
1 |
|
$this->logger->log($this->loggerLevel, $message, $context); |
|
122
|
1 |
|
} |
|
123
|
1 |
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|