Completed
Pull Request — 1.0 (#62)
by Harald
11:03 queued 04:35
created

ContentMapper::mapObjectToUpdateStruct()   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 2
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
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
     */
32 21
    public function __construct(ContentObject $contentObject)
33
    {
34 21
        $this->contentObject = $contentObject;
35 21
    }
36
37
    /**
38
     * @param Content $content
39
     */
40 3
    public function contentToObject(Content $content)
41
    {
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 3
    }
50
51
52
    /**
53
     * Maps object data to create struct.
54
     *
55
     * @param ContentObject       $object       Content object to map from
56
     * @param ContentCreateStruct $createStruct Content create struct to map to
57
     *
58
     * @throws \InvalidArgumentException
59
     */
60 8
    public function mapObjectToCreateStruct(ContentObject $object, ContentCreateStruct $createStruct)
61
    {
62 8
        if ($object->getProperty('language')) {
63 8
            $createStruct->mainLanguageCode = $object->getProperty('language');
64 8
        }
65
66 8
        if ($object->getProperty('remote_id')) {
67 8
            $createStruct->remoteId = $object->getProperty('remote_id');
68 8
        }
69
70 8
        $this->assignStructValues($object, $createStruct);
71 8
    }
72
73
    /**
74
     * Maps object data to update struct.
75
     *
76
     * @param ContentObject       $object              Content object to map from
77
     * @param ContentUpdateStruct $contentUpdateStruct Content update struct to map to
78
     *
79
     * @throws \InvalidArgumentException
80
     */
81 16
    public function mapObjectToUpdateStruct(ContentObject $object, ContentUpdateStruct $contentUpdateStruct)
82
    {
83 16
        $this->assignStructValues($object, $contentUpdateStruct);
84 16
    }
85
86
    /**
87
     * Copies content object data from a struct.
88
     *
89
     * @param ContentObject $object Content object to get values from
90
     * @param object        $struct Struct to assign values to
91
     */
92 20
    private function assignStructValues(ContentObject $object, $struct)
93
    {
94 20
        foreach ($object->data as $key => $value) {
95 20
            if (is_array($value)) {
96 2
                $value = end($value);
97 2
            }
98 20
            $struct->setField($key, $value);
99 20
        }
100
101 20
        if ($object->getProperty('struct_callback')) {
102 1
            $callback = $object->getProperty('struct_callback');
103 1
            $callback($struct);
104 1
        }
105 20
    }
106
}
107