Completed
Pull Request — 1.0 (#63)
by Harald
04:05
created

ContentMapper::contentToObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 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 3
     * @param ContentObject $contentObject
31
     */
32 3
    public function __construct(ContentObject $contentObject)
33 3
    {
34
        $this->contentObject = $contentObject;
35
    }
36
37
    /**
38 3
     * @param Content $content
39
     */
40 3
    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
        }
49
    }
50
51
    /**
52
     * Maps object data to create struct.
53
     *
54
     * @param ContentObject       $object       Content object to map from
55
     * @param ContentCreateStruct $createStruct Content create struct to map to
56
     *
57
     * @throws \InvalidArgumentException
58
     */
59
    public function mapObjectToContentStruct(ContentObject $object, ContentCreateStruct $createStruct)
60
    {
61
        $this->assignStructFieldValues($object, $createStruct);
62
63
        if ($object->getProperty('language')) {
64
            $createStruct->mainLanguageCode = $object->getProperty('language');
65
        }
66
67
        if ($object->getProperty('remote_id')) {
68
            $createStruct->remoteId = $object->getProperty('remote_id');
69
        }
70
    }
71
72
    /**
73
     * Maps object data to update struct.
74
     *
75
     * @param ContentObject       $object              Content object to map from
76
     * @param ContentUpdateStruct $contentUpdateStruct Content update struct to map to
77
     *
78
     * @throws \InvalidArgumentException
79
     */
80
    public function mapObjectToUpdateStruct(ContentObject $object, ContentUpdateStruct $contentUpdateStruct)
81
    {
82
        $this->assignStructFieldValues($object, $contentUpdateStruct);
83
    }
84
85
    /**
86
     * Copies content object data from a struct.
87
     *
88
     * @param ContentObject $object Content object to get values from
89
     * @param object        $struct Struct to assign values to
90
     */
91
    private function assignStructFieldValues(ContentObject $object, $struct)
92
    {
93
        foreach ($object->data as $key => $value) {
94
            if (is_array($value)) {
95
                $value = end($value);
96
            }
97
98
            $struct->setField($key, $value);
99
        }
100
    }
101
}
102