Completed
Branch master (755019)
by Oliver
12:48 queued 05:47
created

OnEnterObserver::update()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4286
cc 3
eloc 4
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Metabor\Statemachine\Observer;
4
5
use MetaborStd\Statemachine\StatemachineInterface;
6
7
/**
8
 * Triggers automatically an event if the status changes and the new status has it.
9
 *
10
 * @author otischlinger
11
 */
12
class OnEnterObserver implements \SplObserver
13
{
14
    /**
15
     * @var string
16
     */
17
    private $eventName;
18
19
    /**
20
     * @param string $eventName
21
     */
22 1
    public function __construct($eventName = 'onEnter')
23
    {
24 1
        $this->eventName = $eventName;
25 1
    }
26
27
    /**
28
     * @see SplObserver::update()
29
     */
30
    public function update(\SplSubject $stateMachine)
31
    {
32
        if ($stateMachine instanceof StatemachineInterface) {
33
            if ($stateMachine->getCurrentState()->hasEvent($this->eventName)) {
34
                $stateMachine->triggerEvent($this->eventName);
35
            }
36
        }
37
    }
38
}
39