Completed
Push — 1.0 ( fb098d...df18cb )
by Valentin
03:59
created

ContentMapper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 13
c 4
b 0
f 1
lcom 1
cbo 4
dl 0
loc 104
ccs 48
cts 48
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A contentToObject() 0 10 2
A mapObjectToCreateStruct() 0 14 1
A mapObjectToUpdateStruct() 0 13 1
A assignStructFieldValues() 0 10 3
A arrayToStruct() 0 8 3
A callStruct() 0 7 2
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\Values\Mapper;
10
11
use eZ\Publish\API\Repository\Values\Content\Content;
12
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
13
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct;
14
use Transfer\EzPlatform\Repository\Values\ContentObject;
15
16
/**
17
 * User mapper.
18
 *
19
 * @author Harald Tollefsen <[email protected]>
20
 */
21
class ContentMapper
22
{
23
    /**
24
     * @var ContentObject
25
     */
26
    public $contentObject;
27
28
    /**
29
     * @param ContentObject $contentObject
30
     */
31 22
    public function __construct(ContentObject $contentObject)
32
    {
33 22
        $this->contentObject = $contentObject;
34 22
    }
35
36
    /**
37
     * @param Content $content
38
     */
39 3
    public function contentToObject(Content $content)
40
    {
41 3
        $this->contentObject->setProperty('id', $content->id);
42 3
        $this->contentObject->setProperty('remote_id', $content->contentInfo->remoteId);
43 3
        $this->contentObject->setProperty('content_info', $content->contentInfo);
44 3
        $this->contentObject->setProperty('version_info', $content->versionInfo);
45 3
        foreach ($content->getFields() as $field) {
46 3
            $this->contentObject->data[$field->fieldDefIdentifier][$field->languageCode] = $field->value;
47 3
        }
48 3
    }
49
50
    /**
51
     * @param ContentCreateStruct $createStruct
52
     *
53
     * @throws \InvalidArgumentException
54
     */
55 9
    public function mapObjectToCreateStruct(ContentCreateStruct $createStruct)
56
    {
57
        // Name collection (ez => transfer)
58
        $keys = array(
59 9
            'mainLanguageCode' => 'main_language_code',
60 9
            'remoteId' => 'remote_id',
61 9
        );
62
63 9
        $this->arrayToStruct($createStruct, $keys);
64
65 9
        $this->assignStructFieldValues($createStruct);
66
67 9
        $this->callStruct($createStruct);
68 9
    }
69
70
    /**
71
     * @param ContentUpdateStruct $updateStruct
72
     */
73 17
    public function mapObjectToUpdateStruct(ContentUpdateStruct $updateStruct)
74
    {
75
        // Name collection (ez => transfer)
76
        $keys = array(
77 17
            'creatorId' => 'creator_id',
78 17
        );
79
80 17
        $this->arrayToStruct($updateStruct, $keys);
81
82 17
        $this->assignStructFieldValues($updateStruct);
83
84 17
        $this->callStruct($updateStruct);
85 17
    }
86
87
    /**
88
     * @param ContentCreateStruct|ContentUpdateStruct $struct
89
     */
90 21
    private function assignStructFieldValues($struct)
91
    {
92 21
        foreach ($this->contentObject->data as $key => $value) {
93 21
            if (is_array($value)) {
94 2
                $value = end($value);
95 2
            }
96
97 21
            $struct->setField($key, $value);
98 21
        }
99 21
    }
100
101
    /**
102
     * @param ContentCreateStruct|ContentUpdateStruct $struct
103
     * @param array                                   $keys
104
     */
105 21
    private function arrayToStruct($struct, $keys)
106
    {
107 21
        foreach ($keys as $ezKey => $transferKey) {
108 21
            if ($this->contentObject->getProperty($transferKey)) {
109 9
                $struct->$ezKey = $this->contentObject->getProperty($transferKey);
110 9
            }
111 21
        }
112 21
    }
113
114
    /**
115
     * @param ContentCreateStruct|ContentUpdateStruct $struct
116
     */
117 21
    private function callStruct($struct)
118
    {
119 21
        if ($this->contentObject->getProperty('struct_callback')) {
120 1
            $callback = $this->contentObject->getProperty('struct_callback');
121 1
            $callback($struct);
122 1
        }
123 21
    }
124
}
125