Passed
Pull Request — 1.0 (#76)
by Harald
06:37
created

FieldDefinitionSubManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 89
ccs 35
cts 35
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addFieldsToCreateStruct() 0 9 2
B createOrUpdateFieldDefinitions() 0 23 4
A createFieldDefinition() 0 7 1
A updateFieldDefinition() 0 7 1
1
<?php
2
3
/**
4
 * This file is part of Transfer.
5
 *
6
 * For the full copyright and license information, please view the LICENSE file located
7
 * in the root directory.
8
 */
9
namespace Transfer\EzPlatform\Repository\Manager\Sub;
10
11
use eZ\Publish\API\Repository\ContentTypeService;
12
use eZ\Publish\API\Repository\Values\ContentType\ContentTypeCreateStruct;
13
use eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft;
14
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
15
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct;
16
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionUpdateStruct;
17
use Transfer\EzPlatform\Repository\Values\FieldDefinitionObject;
18
19
/**
20
 * Content type manager.
21
 *
22
 * @internal
23
 *
24
 * @author Harald Tollefsen <[email protected]>
25
 */
26
class FieldDefinitionSubManager
27
{
28
    /**
29
     * @var ContentTypeService Content type service
30
     */
31
    private $contentTypeService;
32
33
    /**
34
     * FieldDefinitionSubManager constructor.
35
     *
36
     * @param ContentTypeService $contentTypeService
37
     */
38 6
    public function __construct(ContentTypeService $contentTypeService)
39
    {
40 6
        $this->contentTypeService = $contentTypeService;
41 6
    }
42
43
    /**
44
     * @param ContentTypeCreateStruct $createStruct
45
     * @param FieldDefinitionObject[] $fields
46
     */
47 4
    public function addFieldsToCreateStruct(ContentTypeCreateStruct $createStruct, array $fields)
48
    {
49 4
        foreach ($fields as $field) {
50
            /* @var FieldDefinitionObject $field */
51 4
            $fieldCreateStruct = $this->contentTypeService->newFieldDefinitionCreateStruct($field->data['identifier'], $field->data['type']);
52 4
            $field->getMapper()->mapObjectToCreateStruct($fieldCreateStruct);
53 4
            $createStruct->addFieldDefinition($fieldCreateStruct);
54 4
        }
55 4
    }
56
57
    /**
58
     * Creating new and updates existing field definitions.
59
     * NOTE: Will NOT delete field definitions which no longer exist.
60
     *
61
     * @param FieldDefinitionObject[] $updatedFieldDefinitions
62
     * @param FieldDefinition[]       $existingFieldDefinitions
63
     * @param ContentTypeDraft        $contentTypeDraft
64
     */
65 33
    public function createOrUpdateFieldDefinitions(array $updatedFieldDefinitions, array $existingFieldDefinitions, ContentTypeDraft $contentTypeDraft)
66
    {
67 33
        foreach ($updatedFieldDefinitions as $updatedField) {
68
69
            // Updating existing field definitions
70 33
            foreach ($existingFieldDefinitions as $existingField) {
71 33
                if ($existingField->identifier == $updatedField->data['identifier']) {
72 33
                    $this->contentTypeService->updateFieldDefinition(
73 33
                        $contentTypeDraft,
74 33
                        $existingField,
75 33
                        $this->updateFieldDefinition($updatedField)
76 33
                    );
77 33
                    continue 2;
78
                }
79 33
            }
80
81
            // Creating new field definitions
82 1
            $this->contentTypeService->addFieldDefinition(
83 1
                $contentTypeDraft,
84 1
                $this->createFieldDefinition($updatedField)
85 1
            );
86 33
        }
87 33
    }
88
89
    /**
90
     * @param FieldDefinitionObject $field
91
     *
92
     * @return FieldDefinitionCreateStruct
93
     */
94 2
    private function createFieldDefinition(FieldDefinitionObject $field)
95
    {
96 1
        $definition = $this->contentTypeService->newFieldDefinitionCreateStruct($field->data['identifier'], $field->data['type']);
97 1
        $field->getMapper()->mapObjectToCreateStruct($definition);
98
99 2
        return $definition;
100
    }
101
102
    /**
103
     * @param FieldDefinitionObject $field
104
     *
105
     * @return FieldDefinitionUpdateStruct
106
     */
107 33
    private function updateFieldDefinition(FieldDefinitionObject $field)
108
    {
109 33
        $definition = $this->contentTypeService->newFieldDefinitionUpdateStruct();
110 33
        $field->getMapper()->mapObjectToUpdateStruct($definition);
111
112 33
        return $definition;
113
    }
114
}
115