Test Failed
Push — main ( f1b82a...d2d042 )
by Bingo
06:24
created

ModificationCmdJsonConverter::toObject()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 21
rs 8.8333
cc 7
nc 7
nop 2
1
<?php
2
3
namespace Jabe\Engine\Impl\Json;
4
5
use Jabe\Engine\Impl\Cmd\{
6
    AbstractProcessInstanceModificationCommand,
7
    ActivityAfterInstantiationCmd,
8
    ActivityBeforeInstantiationCmd,
9
    ActivityCancellationCmd,
10
    ActivityInstanceCancellationCmd,
11
    TransitionInstanceCancellationCmd,
12
    TransitionInstantiationCmd
13
};
14
use Jabe\Engine\Impl\Util\JsonUtil;
15
16
class ModificationCmdJsonConverter extends JsonObjectConverter
17
{
18
    private static $INSTANCE;
19
    public const START_BEFORE = "startBeforeActivity";
20
    public const START_AFTER = "startAfterActivity";
21
    public const START_TRANSITION = "startTransition";
22
    public const CANCEL_ALL = "cancelAllForActivity";
23
    public const CANCEL_CURRENT = "cancelCurrentActiveActivityInstances";
24
    public const CANCEL_ACTIVITY_INSTANCES = "cancelActivityInstances";
25
    public const PROCESS_INSTANCE = "processInstances";
26
    public const CANCEL_TRANSITION_INSTANCES = "cancelTransitionInstances";
27
28
    public static function instance(): ModificationCmdJsonConverter
29
    {
30
        if (self::$INSTANCE == null) {
31
            self::$INSTANCE = new ModificationCmdJsonConverter();
32
        }
33
        return self::$INSTANCE;
34
    }
35
36
    public function toJsonObject(/*AbstractProcessInstanceModificationCommand*/$command, bool $isOrQueryActive = false): ?\stdClass
37
    {
38
        $json = JsonUtil::createObject();
39
40
        if ($command instanceof ActivityAfterInstantiationCmd) {
41
            JsonUtil::addField($json, self::START_AFTER, $command->getTargetElementId());
42
        } elseif ($command instanceof ActivityBeforeInstantiationCmd) {
43
            JsonUtil::addField($json, self::START_BEFORE, $command->getTargetElementId());
44
        } elseif ($command instanceof TransitionInstantiationCmd) {
45
            JsonUtil::addField($json, self::START_TRANSITION, $command->getTargetElementId());
46
        } elseif ($command instanceof ActivityCancellationCmd) {
47
            JsonUtil::addField($json, self::CANCEL_ALL, $command->getActivityId());
48
            JsonUtil::addField($json, self::CANCEL_CURRENT, $command->isCancelCurrentActiveActivityInstances());
49
        } elseif ($command instanceof ActivityInstanceCancellationCmd) {
50
            JsonUtil::addField($json, self::CANCEL_ACTIVITY_INSTANCES, $command->getActivityInstanceId());
51
            JsonUtil::addField($json, self::PROCESS_INSTANCE, $command->getProcessInstanceId());
52
        } elseif ($command instanceof TransitionInstanceCancellationCmd) {
53
            JsonUtil::addField($json, self::CANCEL_TRANSITION_INSTANCES, $command->getTransitionInstanceId());
54
            JsonUtil::addField($json, self::PROCESS_INSTANCE, $command->getProcessInstanceId());
55
        }
56
57
        return $json;
58
    }
59
60
    public function toObject(\stdClass $json, bool $isOrQuery = false)
61
    {
62
        $cmd = null;
63
64
        if (property_exists($json, self::START_BEFORE)) {
65
            $cmd = new ActivityBeforeInstantiationCmd(JsonUtil::getString($json, self::START_BEFORE));
0 ignored issues
show
Bug introduced by
The call to Jabe\Engine\Impl\Cmd\Act...ationCmd::__construct() has too few arguments starting with activityId. ( Ignorable by Annotation )

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

65
            $cmd = /** @scrutinizer ignore-call */ new ActivityBeforeInstantiationCmd(JsonUtil::getString($json, self::START_BEFORE));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
66
        } elseif (property_exists($json, self::START_AFTER)) {
67
            $cmd = new ActivityAfterInstantiationCmd(JsonUtil::getString($json, self::START_AFTER));
0 ignored issues
show
Bug introduced by
The call to Jabe\Engine\Impl\Cmd\Act...ationCmd::__construct() has too few arguments starting with activityId. ( Ignorable by Annotation )

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

67
            $cmd = /** @scrutinizer ignore-call */ new ActivityAfterInstantiationCmd(JsonUtil::getString($json, self::START_AFTER));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
68
        } elseif (property_exists($json, self::START_TRANSITION)) {
69
            $cmd = new TransitionInstantiationCmd(JsonUtil::getString($json, self::START_TRANSITION));
0 ignored issues
show
Bug introduced by
The call to Jabe\Engine\Impl\Cmd\Tra...ationCmd::__construct() has too few arguments starting with transitionId. ( Ignorable by Annotation )

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

69
            $cmd = /** @scrutinizer ignore-call */ new TransitionInstantiationCmd(JsonUtil::getString($json, self::START_TRANSITION));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
70
        } elseif (property_exists($json, self::CANCEL_ALL)) {
71
            $cmd = new ActivityCancellationCmd(JsonUtil::getString($json, self::CANCEL_ALL));
0 ignored issues
show
Bug introduced by
The call to Jabe\Engine\Impl\Cmd\Act...ationCmd::__construct() has too few arguments starting with activityId. ( Ignorable by Annotation )

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

71
            $cmd = /** @scrutinizer ignore-call */ new ActivityCancellationCmd(JsonUtil::getString($json, self::CANCEL_ALL));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
72
            $cancelCurrentActiveActivityInstances = JsonUtil::getBoolean($json, self::CANCEL_CURRENT);
73
            $cmd->setCancelCurrentActiveActivityInstances($cancelCurrentActiveActivityInstances);
74
        } elseif (property_exists($json, self::CANCEL_ACTIVITY_INSTANCES)) {
75
            $cmd = new ActivityInstanceCancellationCmd(JsonUtil::getString($json, self::PROCESS_INSTANCE), JsonUtil::getString($json, self::CANCEL_ACTIVITY_INSTANCES));
76
        } elseif (property_exists($json, self::CANCEL_TRANSITION_INSTANCES)) {
77
            $cmd = new TransitionInstanceCancellationCmd(JsonUtil::getString($json, self::PROCESS_INSTANCE), JsonUtil::getString($json, self::CANCEL_TRANSITION_INSTANCES));
78
        }
79
80
        return $cmd;
81
    }
82
}
83