FlowIdTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 6 2
A getId() 0 7 2
1
<?php
2
3
/*
4
 * This file is part of NodalFlow.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/NodalFlow
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\NodalFlow\Flows;
11
12
use Exception;
13
use fab2s\NodalFlow\Nodes\NodeInterface;
14
use fab2s\SoUuid\SoUuid;
15
16
/**
17
 * Trait FlowIdTrait
18
 */
19
trait FlowIdTrait
20
{
21
    /**
22
     * This Flow / Node id
23
     *
24
     * @var string
25
     */
26
    protected $id;
27
28
    /**
29
     * Current nonce, fully valid within each thread
30
     *
31
     * @var int
32
     */
33
    protected static $nonce = 0;
34
35
    /**
36
     * We need to reset the id when being cloned
37
     * to guarantee immutable uniqueness
38
     */
39
    public function __clone()
40
    {
41
        $this->id = null;
42
        // need to detach node from carrier
43
        if ($this instanceof NodeInterface) {
44
            $this->setCarrier(null);
45
        }
46
    }
47
48
    /**
49
     * Return the immutable unique Flow / Node id
50
     * Since this method is not used in the actual
51
     * flow execution loop, but only when an interruption
52
     * is raised, it's not a performance issue to add an if.
53
     * And it's more convenient to lazy generate as this
54
     * trait does not need any init/construct logic.
55
     *
56
     * @throws Exception
57
     *
58
     * @return string Immutable unique id
59
     */
60
    public function getId(): string
61
    {
62
        if ($this->id === null) {
63
            return $this->id = SoUuid::generate()->getString();
64
        }
65
66
        return $this->id;
67
    }
68
}
69