Test Failed
Push — main ( 92cbf8...131025 )
by Bingo
08:08
created

DeploymentBuilderImpl::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 11
rs 9.9666
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Jabe\Engine\Impl\Repository;
4
5
use Jabe\Engine\ProcessEngineException;
6
use Jabe\Engine\Exception\NotValidException;
7
use Jabe\Engine\Impl\{
8
    ProcessEngineLogger,
9
    RepositoryServiceImpl
10
};
11
use Jabe\Engine\Impl\Bpmn\Deployer\BpmnDeployer;
12
use Jabe\Engine\Impl\Cmd\CommandLogger;
13
use Jabe\Engine\Impl\Persistence\Entity\{
14
    DeploymentEntity,
15
    ResourceEntity
16
};
17
use Jabe\Engine\Impl\Util\{
18
    CollectionUtil,
19
    EnsureUtil,
20
    IoUtil,
21
    ReflectUtil,
22
    StringUtil
23
};
24
use Jabe\Engine\Repository\{
25
    DeploymentInterface,
26
    DeploymentBuilderInterface,
27
    DeploymentWithDefinitionsInterface
28
};
29
use Jabe\Model\Bpmn\{
30
    Bpmn,
31
    BpmnModelInstanceInterface
32
};
33
34
class DeploymentBuilderImpl implements DeploymentBuilderInterface, \Serializable
35
{
36
    //private final static CommandLogger LOG = ProcessEngineLogger.CMD_LOGGER;
37
38
    protected $repositoryService;
39
    protected $deployment;
40
    protected $isDuplicateFilterEnabled = false;
41
    protected $deployChangedOnly = false;
42
    protected $processDefinitionsActivationDate;
43
44
    protected $nameFromDeployment;
45
    protected $deployments = [];
46
    protected $deploymentResourcesById = [];
47
    protected $deploymentResourcesByName = [];
48
49
    public function __construct(RepositoryServiceImpl $repositoryService)
50
    {
51
        $this->deployment = new DeploymentEntity();
52
        $this->repositoryService = $repositoryService;
53
    }
54
55
    public function serialize()
56
    {
57
        return json_encode([
58
            'deployment' => serialize($this->deployment),
59
            'isDuplicateFilterEnabled' => $this->isDuplicateFilterEnabled,
60
            'deployChangedOnly' => $this->deployChangedOnly,
61
            'processDefinitionsActivationDate' => $this->processDefinitionsActivationDate,
62
            'nameFromDeployment' => $this->nameFromDeployment,
63
            'deployments' => $this->deployments,
64
            'deploymentResourcesById' => $this->deploymentResourcesById,
65
            'deploymentResourcesByName' => $this->deploymentResourcesByName
66
        ]);
67
    }
68
69
    public function unserialize($data)
70
    {
71
        $json = json_decode($data);
72
        $this->deployment = unserialize($json->deployment);
73
        $this->isDuplicateFilterEnabled = $json->isDuplicateFilterEnabled;
74
        $this->deployChangedOnly = $json->deployChangedOnly;
75
        $this->processDefinitionsActivationDate = $json->processDefinitionsActivationDate;
76
        $this->nameFromDeployment = $json->nameFromDeployment;
77
        $this->deployments = $json->deployments;
78
        $this->deploymentResourcesById = $json->deploymentResourcesById;
79
        $this->deploymentResourcesByName = $json->deploymentResourcesByName;
80
    }
81
82
    public function addInputStream(string $resourceName, $inputStream): DeploymentBuilderInterface
83
    {
84
        EnsureUtil::ensureNotNull("inputStream for resource '" . $resourceName . "' is null", "inputStream", $inputStream);
85
        $bytes = IoUtil::readInputStream($inputStream, $resourceName);
86
87
        return $this->addBytes($resourceName, $bytes);
88
    }
89
90
    public function addClasspathResource(string $resource): DeploymentBuilderInterface
91
    {
92
        $inputStream = ReflectUtil::getResourceAsStream($resource);
93
        EnsureUtil::ensureNotNull("resource '" . $resource . "' not found", "inputStream", $inputStream);
94
        return $this->addInputStream($resource, $inputStream);
95
    }
96
97
    public function addString(string $resourceName, string $text): DeploymentBuilderInterface
98
    {
99
        EnsureUtil::ensureNotNull("text", "text", $text);
100
101
        $bytes = $text;
102
103
        return $this->addBytes($resourceName, $bytes);
104
    }
105
106
    /*public DeploymentBuilder addModelInstance(string $resourceName, CmmnModelInstance modelInstance) {
107
        EnsureUtil::ensureNotNull("modelInstance", modelInstance);
108
109
        validateResouceName(resourceName, CmmnDeployer.CMMN_RESOURCE_SUFFIXES);
110
111
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
112
        Cmmn.writeModelToStream(outputStream, modelInstance);
113
114
        return addBytes(resourceName, outputStream.toByteArray());
115
    }*/
116
117
    public function addModelInstance(string $resourceName, BpmnModelInstanceInterface $modelInstance): DeploymentBuilderInterface
118
    {
119
        EnsureUtil::ensureNotNull("modelInstance", "modelInstance", $modelInstance);
120
121
        $this->validateResouceName($resourceName, BpmnDeployer::BPMN_RESOURCE_SUFFIXES);
122
123
        $path = tempnam(sys_get_temp_dir(), 'bpmn');
124
        $outputStream = fopen($path, 'a+');
125
126
        Bpmn::writeModelToStream($outputStream, $modelInstance);
127
128
        return $this->addBytes($resourceName, file_get_contents($path));
129
    }
130
131
    /*public DeploymentBuilder addModelInstance(string $resourceName, DmnModelInstance modelInstance) {
132
        EnsureUtil::ensureNotNull("modelInstance", modelInstance);
133
134
        validateResouceName(resourceName, DecisionDefinitionDeployer.DMN_RESOURCE_SUFFIXES);
135
136
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
137
        Dmn.writeModelToStream(outputStream, modelInstance);
138
139
        return addBytes(resourceName, outputStream.toByteArray());
140
    }*/
141
142
    private function validateResouceName(string $resourceName, array $resourceSuffixes): void
143
    {
144
        if (!StringUtil::hasAnySuffix($resourceName, $resourceSuffixes)) {
0 ignored issues
show
Bug introduced by
The method hasAnySuffix() does not exist on Jabe\Engine\Impl\Util\StringUtil. ( Ignorable by Annotation )

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

144
        if (!StringUtil::/** @scrutinizer ignore-call */ hasAnySuffix($resourceName, $resourceSuffixes)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
145
            //LOG.warnDeploymentResourceHasWrongName(resourceName, resourceSuffixes);
146
        }
147
    }
148
149
    protected function addBytes(string $resourceName, string $bytes): DeploymentBuilderInterface
150
    {
151
        $resource = new ResourceEntity();
152
        $resource->setBytes($bytes);
153
        $resource->setName($resourceName);
154
        $this->deployment->addResource($resource);
155
156
        return $this;
157
    }
158
159
    /*public function addZipInputStream(ZipInputStream zipInputStream): DeploymentBuilderInterface
160
    {
161
        try {
162
            ZipEntry entry = zipInputStream.getNextEntry();
163
            while (entry !== null) {
164
                if (!entry.isDirectory()) {
165
                    String entryName = entry.getName();
166
                    addInputStream(entryName, zipInputStream);
167
                }
168
                entry = zipInputStream.getNextEntry();
169
            }
170
        } catch (Exception e) {
171
            throw new ProcessEngineException("problem reading zip input stream", e);
172
        }
173
        return $this;
174
    }*/
175
176
    public function addDeploymentResources(string $deploymentId): DeploymentBuilderInterface
177
    {
178
        EnsureUtil::ensureNotNull(NotValidException::class, "deploymentId", $deploymentId);
179
        $this->deployments[] = $deploymentId;
180
        return $this;
181
    }
182
183
    public function addDeploymentResourceById(string $deploymentId, string $resourceId): DeploymentBuilderInterface
184
    {
185
        EnsureUtil::ensureNotNull(NotValidException::class, "deploymentId", $deploymentId);
186
        EnsureUtil::ensureNotNull(NotValidException::class, "resourceId", $resourceId);
187
188
        CollectionUtil::addToMapOfSets($this->deploymentResourcesById, $deploymentId, $resourceId);
189
190
        return $this;
191
    }
192
193
    public function addDeploymentResourcesById(string $deploymentId, array $resourceIds): DeploymentBuilderInterface
194
    {
195
        EnsureUtil::ensureNotNull(NotValidException::class, "deploymentId", $deploymentId);
196
197
        EnsureUtil::ensureNotNull(NotValidException::class, "resourceIds", $resourceIds);
198
        EnsureUtil::ensureNotEmpty(NotValidException::class, "resourceIds", $resourceIds);
199
        EnsureUtil::ensureNotContainsNull(NotValidException::class, "resourceIds", $resourceIds);
200
201
        CollectionUtil::addCollectionToMapOfSets($this->deploymentResourcesById, $deploymentId, $resourceIds);
202
203
        return $this;
204
    }
205
206
    public function addDeploymentResourceByName(string $deploymentId, string $resourceName): DeploymentBuilderInterface
207
    {
208
        EnsureUtil::ensureNotNull(NotValidException::class, "deploymentId", $deploymentId);
209
        EnsureUtil::ensureNotNull(NotValidException::class, "resourceName", $resourceName);
210
211
        CollectionUtil::addToMapOfSets($this->deploymentResourcesByName, $deploymentId, $resourceName);
212
213
        return $this;
214
    }
215
216
    public function addDeploymentResourcesByName(string $deploymentId, array $resourceNames): DeploymentBuilderInterface
217
    {
218
        EnsureUtil::ensureNotNull(NotValidException::class, "deploymentId", $deploymentId);
219
220
        EnsureUtil::ensureNotNull(NotValidException::class, "resourceNames", $resourceNames);
221
        EnsureUtil::ensureNotEmpty(NotValidException::class, "resourceNames", $resourceNames);
222
        EnsureUtil::ensureNotContainsNull(NotValidException::class, "resourceNames", $resourceNames);
223
224
        CollectionUtil::addCollectionToMapOfSets($this->deploymentResourcesByName, $deploymentId, $resourceNames);
225
226
        return $this;
227
    }
228
229
    public function name(string $name): DeploymentBuilderInterface
230
    {
231
        if (!empty($this->nameFromDeployment)) {
232
            $message = sprintf("Cannot set the deployment name to '%s', because the property 'nameForDeployment' has been already set to '%s'.", $name, $nameFromDeployment);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $nameFromDeployment seems to be never defined.
Loading history...
233
            throw new NotValidException($message);
234
        }
235
        $this->deployment->setName($name);
236
        return $this;
237
    }
238
239
    public function nameFromDeployment(string $deploymentId): DeploymentBuilderInterface
240
    {
241
        $name = $this->deployment->getName();
242
        if (!empty($name)) {
243
            $message = sprintf("Cannot set the given deployment id '%s' to get the name from it, because the deployment name has been already set to '%s'.", $deploymentId, $name);
244
            throw new NotValidException($message);
245
        }
246
        $this->nameFromDeployment = $deploymentId;
247
        return $this;
248
    }
249
250
    public function enableDuplicateFiltering(bool $deployChangedOnly = false): DeploymentBuilderInterface
251
    {
252
        $this->isDuplicateFilterEnabled = true;
253
        $this->deployChangedOnly = $deployChangedOnly;
254
        return $this;
255
    }
256
257
    public function activateProcessDefinitionsOn(string $date): DeploymentBuilderInterface
258
    {
259
        $this->processDefinitionsActivationDate = $date;
260
        return $this;
261
    }
262
263
    public function source(string $source): DeploymentBuilderInterface
264
    {
265
        $this->deployment->setSource($source);
266
        return $this;
267
    }
268
269
    public function tenantId(string $tenantId): DeploymentBuilderInterface
270
    {
271
        $this->deployment->setTenantId($tenantId);
272
        return $this;
273
    }
274
275
    public function deploy(): DeploymentInterface
276
    {
277
        return $this->deployWithResult();
278
    }
279
280
    public function deployWithResult(): DeploymentWithDefinitionsInterface
281
    {
282
        return $this->repositoryService->deployWithResult($this);
283
    }
284
285
    public function getResourceNames(): array
286
    {
287
        if (empty($this->deployment->getResources())) {
288
            return [];
289
        } else {
290
            return array_keys($this->deployment->getResources());
291
        }
292
    }
293
294
    // getters and setters //////////////////////////////////////////////////////
295
296
    public function getDeployment(): DeploymentEntity
297
    {
298
        return $this->deployment;
299
    }
300
301
    public function isDuplicateFilterEnabled(): bool
302
    {
303
        return $this->isDuplicateFilterEnabled;
304
    }
305
306
    public function isDeployChangedOnly(): bool
307
    {
308
        return $this->deployChangedOnly;
309
    }
310
311
    public function getProcessDefinitionsActivationDate(): string
312
    {
313
        return $this->processDefinitionsActivationDate;
314
    }
315
316
    public function getNameFromDeployment(): string
317
    {
318
        return $this->nameFromDeployment;
319
    }
320
321
    public function getDeployments(): array
322
    {
323
        return $this->deployments;
324
    }
325
326
    public function getDeploymentResourcesById(): array
327
    {
328
        return $this->deploymentResourcesById;
329
    }
330
331
    public function getDeploymentResourcesByName(): array
332
    {
333
        return $this->deploymentResourcesByName;
334
    }
335
}
336