ContentMapper   A
last analyzed

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