Operation::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style introduced by
Header blocks must be separated by a single blank line
Loading history...
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Johmanx10\Transaction;
8
9
class Operation implements DescribableOperationInterface
10
{
11
    /** @var callable */
12
    private $operation;
13
14
    /** @var callable|null */
15
    private $rollback;
16
17
    /** @var string */
18
    private $description;
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param callable      $operation
24
     * @param callable|null $rollback
25
     * @param string|null   $description
26
     */
27 1
    public function __construct(
28
        callable $operation,
29
        callable $rollback = null,
30
        string $description = null
31
    ) {
32 1
        $this->operation   = $operation;
33 1
        $this->rollback    = $rollback;
34 1
        $this->description = $description ?? sprintf(
35 1
            'Generic operation %s',
36 1
            spl_object_hash($this)
37
        );
38 1
    }
39
40
    /**
41
     * Execute the operation.
42
     *
43
     * @return void
44
     */
45 1
    public function __invoke(): void
46
    {
47 1
        call_user_func($this->operation);
48 1
    }
49
50
    /**
51
     * Apply the rollback mechanism.
52
     *
53
     * @return void
54
     */
55 1
    public function rollback(): void
56
    {
57 1
        if ($this->rollback !== null) {
58 1
            call_user_func($this->rollback);
59
        }
60 1
    }
61
62
    /**
63
     * Describe the current operation.
64
     *
65
     * @return string
66
     */
67 1
    public function __toString(): string
68
    {
69 1
        return $this->description;
70
    }
71
}
72