LambdaStep   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 12
dl 0
loc 44
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A compensate() 0 4 1
A __construct() 0 7 1
A execute() 0 4 1
A stepName() 0 3 1
1
<?php
2
3
namespace MeadSteve\Tale\Steps;
4
5
class LambdaStep implements NamedStep
6
{
7
    /**
8
     * @var callable
9
     */
10
    private $executeHandler;
11
12
    /**
13
     * @var callable
14
     */
15
    private $compensateHandler;
16
17
    /**
18
     * @var string
19
     */
20
    private $name;
21
22
    public function __construct(callable $execute, callable $compensate = null, string $name = null)
23
    {
24
        $this->executeHandler = $execute;
25
        $this->compensateHandler = $compensate ?? function () {
26
        };
27
28
        $this->name = $name ?? "anonymous lambda";
29
    }
30
31
    public function execute($state)
32
    {
33
        $function = $this->executeHandler;
34
        return $function($state);
35
    }
36
37
    public function compensate($state): void
38
    {
39
        $function = $this->compensateHandler;
40
        $function($state);
41
    }
42
43
    /**
44
     * @return string the public name of this step
45
     */
46
    public function stepName(): string
47
    {
48
        return $this->name;
49
    }
50
}
51