FieldDefinitionSubManager   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

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

6 Methods

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