OnEnterObserver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 34
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A update() 0 13 4
1
<?php
2
3
namespace Metabor\Statemachine\Observer;
4
5
use Metabor\Statemachine\Statemachine;
6
use MetaborStd\Statemachine\StatemachineInterface;
7
8
/**
9
 * Triggers automatically an event if the status changes and the new status has it.
10
 *
11
 * @author otischlinger
12
 */
13
class OnEnterObserver implements \SplObserver
14
{
15
    const DEFAULT_EVENT_NAME = 'onEnter';
16
17
    /**
18
     * @var string
19
     */
20
    private $eventName;
21
22
    /**
23
     * @param string $eventName
24
     */
25 2
    public function __construct($eventName = self::DEFAULT_EVENT_NAME)
26
    {
27 2
        $this->eventName = $eventName;
28 2
    }
29
30
    /**
31
     * @see SplObserver::update()
32
     */
33 2
    public function update(\SplSubject $stateMachine)
34
    {
35 2
        if ($stateMachine instanceof StatemachineInterface && $stateMachine->getCurrentState()->hasEvent($this->eventName)) {
36 2
            if ($stateMachine instanceof Statemachine) {
37 2
                $autoreleaseLock = $stateMachine->isAutoreleaseLock();
38 2
                $stateMachine->setAutoreleaseLock(false);
39 2
                $stateMachine->triggerEvent($this->eventName, $stateMachine->getCurrentContext());
40 2
                $stateMachine->setAutoreleaseLock($autoreleaseLock);
41 2
            } else {
42
                $stateMachine->triggerEvent($this->eventName);
43
            }
44 2
        }
45 2
    }
46
}
47