getCanonicalName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Jabe\Impl\Pvm\Runtime\Operation;
4
5
use Jabe\Delegate\ExecutionListenerInterface;
6
use Jabe\Persistence\Entity\ExecutionEntity;
7
use Jabe\Impl\Pvm\PvmActivityInterface;
8
use Jabe\Impl\Pvm\Process\{
9
    ActivityImpl,
10
    ScopeImpl
11
};
12
use Jabe\Impl\Pvm\Runtime\{
13
    CompensationBehavior,
14
    PvmExecutionImpl
15
};
16
use Jabe\Impl\Core\Model\CoreModelElement;
17
use Jabe\Impl\Core\Instance\CoreExecution;
18
19
class PvmAtomicOperationDeleteCascadeFireActivityEnd extends PvmAtomicOperationActivityInstanceEnd
20
{
21
    protected function eventNotificationsStarted(CoreExecution $execution): CoreExecution
22
    {
23
        $execution->setCanceled(true);
24
        return parent::eventNotificationsStarted($execution);
25
    }
26
27
    protected function getScope(CoreExecution $execution): CoreModelElement
28
    {
29
        $activity = $execution->getActivity();
30
31
        if ($activity !== null) {
32
            return $activity;
33
        } else {
34
            // TODO: when can this happen?
35
            $parent = $execution->getParent();
36
            if ($parent !== null) {
37
                return $this->getScope($execution->getParent());
38
            }
39
            return $execution->getProcessDefinition();
40
        }
41
    }
42
43
    protected function getEventName(): string
44
    {
45
        return ExecutionListenerInterface::EVENTNAME_END;
46
    }
47
48
    protected function eventNotificationsCompleted(CoreExecution $execution): void
49
    {
50
        $activity = $execution->getActivity();
51
52
        if (
53
            $execution->isScope()
54
            && ($this->executesNonScopeActivity($execution) || $this->isAsyncBeforeActivity($execution))
55
            && !CompensationBehavior::executesNonScopeCompensationHandler($execution)
56
        ) {
57
            $execution->removeAllTasks();
58
            // case this is a scope execution and the activity is not a scope
59
            $execution->leaveActivityInstance();
60
            $execution->setActivity(getFlowScopeActivity($activity));
0 ignored issues
show
Bug introduced by
The function getFlowScopeActivity was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
            $execution->setActivity(/** @scrutinizer ignore-call */ getFlowScopeActivity($activity));
Loading history...
61
            $execution->performOperation(self::deleteCascadeFireActivityEnd());
62
        } else {
63
            if ($execution->isScope()) {
64
                if ($execution instanceof ExecutionEntity && !$execution->isProcessInstanceExecution() && $execution->isCanceled()) {
65
                    // execution was canceled and output mapping for activity is marked as skippable
66
                    $execution->setSkipIoMappings($execution->isSkipIoMappings() || $execution->getProcessEngine()->getProcessEngineConfiguration()->isSkipOutputMappingOnCanceledActivities());
67
                }
68
                $execution->destroy();
69
            }
70
71
            // remove this execution and its concurrent parent (if exists)
72
            $execution->remove();
73
74
            $continueRemoval = !$execution->isDeleteRoot();
75
76
            if ($continueRemoval) {
77
                $propagatingExecution = $execution->getParent();
78
                if ($propagatingExecution !== null && !$propagatingExecution->isScope() && !$propagatingExecution->hasChildren()) {
79
                    $propagatingExecution->remove();
80
                    $continueRemoval = !$propagatingExecution->isDeleteRoot();
81
                    $propagatingExecution = $propagatingExecution->getParent();
82
                }
83
84
                if ($continueRemoval) {
85
                    if ($propagatingExecution !== null) {
86
                        // continue deletion with the next scope execution
87
                        // set activity on parent in case the parent is an inactive scope execution and activity has been set to 'null'.
88
                        if ($propagatingExecution->getActivity() === null && $activity !== null && $activity->getFlowScope() !== null) {
89
                            $propagatingExecution->setActivity($this->getFlowScopeActivity($activity));
90
                        }
91
                    }
92
                }
93
            }
94
        }
95
    }
96
97
    protected function executesNonScopeActivity(PvmExecutionImpl $execution): bool
98
    {
99
        $activity = $execution->getActivity();
100
        return $activity !== null && !$activity->isScope();
101
    }
102
103
    protected function isAsyncBeforeActivity(PvmExecutionImpl $execution): bool
104
    {
105
        return !empty($execution->getActivityId()) && empty($execution->getActivityInstanceId());
106
    }
107
108
    protected function getFlowScopeActivity(PvmActivityInterface $activity): ActivityImpl
109
    {
110
        $flowScope = $activity->getFlowScope();
111
        $flowScopeActivity = null;
112
        if ($flowScope->getProcessDefinition() != $flowScope) {
113
            $flowScopeActivity = $flowScope;
114
        }
115
        return $flowScopeActivity;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $flowScopeActivity returns the type null which is incompatible with the type-hinted return Jabe\Impl\Pvm\Process\ActivityImpl.
Loading history...
116
    }
117
118
    public function getCanonicalName(): string
119
    {
120
        return "delete-cascade-fire-activity-end";
121
    }
122
}
123