Completed
Push — 1.0 ( df18cb...d1b573 )
by Valentin
01:34
created

FieldDefinitionSubManager::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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