Passed
Push — 6.4 ( 3d0a37...1ed39e )
by Christian
14:28 queued 13s
created

getConstraints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 21
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 24
rs 9.584
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Flow\DataAbstractionLayer\FieldSerializer;
4
5
use Shopware\Core\Content\Flow\DataAbstractionLayer\Field\FlowTemplateConfigField;
6
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InvalidSerializerFieldException;
7
use Shopware\Core\Framework\DataAbstractionLayer\Field\Field;
8
use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
9
use Shopware\Core\Framework\DataAbstractionLayer\Write\DataStack\KeyValuePair;
10
use Shopware\Core\Framework\DataAbstractionLayer\Write\EntityExistence;
11
use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteParameterBag;
12
use Shopware\Core\Framework\Validation\Constraint\Uuid;
13
use Symfony\Component\Validator\Constraints\Collection;
14
use Symfony\Component\Validator\Constraints\NotBlank;
15
use Symfony\Component\Validator\Constraints\Optional;
16
use Symfony\Component\Validator\Constraints\Type;
17
18
/**
19
 * @package business-ops
20
 *
21
 * @internal
22
 */
23
class FlowTemplateConfigFieldSerializer extends JsonFieldSerializer
0 ignored issues
show
Deprecated Code introduced by
The class Shopware\Core\Framework\...zer\JsonFieldSerializer has been deprecated: tag:v6.5.0 - reason:becomes-internal - Will be internal ( Ignorable by Annotation )

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

23
class FlowTemplateConfigFieldSerializer extends /** @scrutinizer ignore-deprecated */ JsonFieldSerializer
Loading history...
24
{
25
    public function encode(
26
        Field $field,
27
        EntityExistence $existence,
28
        KeyValuePair $data,
29
        WriteParameterBag $parameters
30
    ): \Generator {
31
        if (!$field instanceof FlowTemplateConfigField) {
32
            throw new InvalidSerializerFieldException(FlowTemplateConfigField::class, $field);
33
        }
34
35
        $this->validateIfNeeded($field, $existence, $data, $parameters);
36
37
        $value = $data->getValue();
38
39
        if (!\is_array($value)) {
40
            yield $field->getStorageName() => null;
41
42
            return;
43
        }
44
45
        $value = array_merge([
46
            'description' => null,
47
            'sequences' => [],
48
        ], $value);
49
50
        $sequences = $value['sequences'];
51
52
        $value['sequences'] = array_map(function ($item) {
53
            return array_merge([
54
                'parentId' => null,
55
                'ruleId' => null,
56
                'position' => 1,
57
                'displayGroup' => 1,
58
                'trueCase' => 0,
59
            ], $item);
60
        }, $sequences);
61
62
        yield $field->getStorageName() => JsonFieldSerializer::encodeJson($value);
63
    }
64
65
    protected function getConstraints(Field $field): array
66
    {
67
        return [
68
            new Collection([
69
                'allowExtraFields' => true,
70
                'allowMissingFields' => false,
71
                'fields' => [
72
                    'eventName' => [new NotBlank(), new Type('string')],
73
                    'description' => [new Type('string')],
74
                    'sequences' => [
75
                        [
76
                            new Optional(
77
                                new Collection([
78
                                    'allowExtraFields' => true,
79
                                    'allowMissingFields' => false,
80
                                    'fields' => [
81
                                        'id' => [new NotBlank(), new Uuid()],
82
                                        'actionName' => [new NotBlank(), new Type('string')],
83
                                        'parentId' => [new Uuid()],
84
                                        'ruleId' => [new Uuid()],
85
                                        'position' => [new Type('numeric')],
86
                                        'trueCase' => [new Type('boolean')],
87
                                        'displayGroup' => [new Type('numeric')],
88
                                        'config' => [new Type('array')],
89
                                    ],
90
                                ])
91
                            ),
92
                        ],
93
                    ],
94
                ],
95
            ]),
96
        ];
97
    }
98
}
99