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

ModificationBatchConfigurationJsonConverter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 36
c 1
b 0
f 0
dl 0
loc 61
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A readProcessInstanceIds() 0 3 1
A toObject() 0 19 1
A toJsonObject() 0 11 1
A readIdMappings() 0 3 1
A instance() 0 6 2
1
<?php
2
3
namespace Jabe\Engine\Impl\Json;
4
5
use Jabe\Engine\Impl\ModificationBatchConfiguration;
6
use Jabe\Engine\Impl\Batch\{
7
    DeploymentMappingJsonConverter,
8
    DeploymentMappings
9
};
10
use Jabe\Engine\Impl\Cmd\AbstractProcessInstanceModificationCommand;
11
use Jabe\Engine\Impl\Util\JsonUtil;
12
13
class ModificationBatchConfigurationJsonConverter extends JsonObjectConverter
14
{
15
    private static $INSTANCE;
16
    public const INSTRUCTIONS = "instructions";
17
    public const PROCESS_INSTANCE_IDS = "processInstanceIds";
18
    public const PROCESS_INSTANCE_ID_MAPPINGS = "processInstanceIdMappings";
19
    public const SKIP_LISTENERS = "skipListeners";
20
    public const SKIP_IO_MAPPINGS = "skipIoMappings";
21
    public const PROCESS_DEFINITION_ID = "processDefinitionId";
22
23
    public static function instance(): ModificationBatchConfigurationJsonConverter
24
    {
25
        if (self::$INSTANCE == null) {
26
            self::$INSTANCE = new ModificationBatchConfigurationJsonConverter();
27
        }
28
        return self::$INSTANCE;
29
    }
30
31
    public function toJsonObject(/*ModificationBatchConfiguration*/$configuration, bool $isOrQueryActive = false): ?\stdClass
32
    {
33
        $json = JsonUtil::createObject();
34
        JsonUtil::addListField($json, self::INSTRUCTIONS, ModificationCmdJsonConverter::instance(), $configuration->getInstructions());
35
        JsonUtil::addListField($json, self::PROCESS_INSTANCE_IDS, $configuration->getIds());
36
        JsonUtil::addListField($json, self::PROCESS_INSTANCE_ID_MAPPINGS, DeploymentMappingJsonConverter::instance(), $configuration->getIdMappings());
37
        JsonUtil::addField($json, self::PROCESS_DEFINITION_ID, $configuration->getProcessDefinitionId());
38
        JsonUtil::addField($json, self::SKIP_LISTENERS, $configuration->isSkipCustomListeners());
39
        JsonUtil::addField($json, self::SKIP_IO_MAPPINGS, $configuration->isSkipIoMappings());
40
41
        return $json;
42
    }
43
44
    public function toObject(\stdClass $json, bool $isOrQuery = false)
45
    {
46
        $processInstanceIds = $this->readProcessInstanceIds($json);
47
        $mappings = $this->readIdMappings($json);
48
        $processDefinitionId = JsonUtil::getString($json, self::PROCESS_DEFINITION_ID);
49
        $instructions = JsonUtil::asList(
50
            JsonUtil::getArray($json, self::INSTRUCTIONS),
51
            ModificationCmdJsonConverter::instance()
52
        );
53
        $skipCustomListeners = JsonUtil::getBoolean($json, self::SKIP_LISTENERS);
54
        $skipIoMappings = JsonUtil::getBoolean($json, self::SKIP_IO_MAPPINGS);
55
56
        return new ModificationBatchConfiguration(
57
            $processInstanceIds,
58
            $mappings,
59
            $processDefinitionId,
60
            $instructions,
61
            $skipCustomListeners,
62
            $skipIoMappings
63
        );
64
    }
65
66
    protected function readProcessInstanceIds($jsonObject): array
67
    {
68
        return JsonUtil::asStringList(JsonUtil::getArray($jsonObject, self::PROCESS_INSTANCE_IDS));
69
    }
70
71
    protected function readIdMappings(\stdClass $json): DeploymentMappings
72
    {
73
        return JsonUtil::asList(JsonUtil::getArray($json, self::PROCESS_INSTANCE_ID_MAPPINGS), DeploymentMappingJsonConverter::instance(), DeploymentMappings::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Jabe\Engine\Impl\...loymentMappings::class) could return the type array which is incompatible with the type-hinted return Jabe\Engine\Impl\Batch\DeploymentMappings. Consider adding an additional type-check to rule them out.
Loading history...
74
    }
75
}
76