Completed
Push — 1.0 ( 2117b2...747417 )
by Valentin
09:28 queued 05:49
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 1
Metric Value
c 1
b 0
f 1
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
     * 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 8
    public function mapObjectToCreateStruct(ContentObject $object, ContentCreateStruct $createStruct)
60
    {
61 8
        if ($object->getProperty('language')) {
62 8
            $createStruct->mainLanguageCode = $object->getProperty('language');
63 8
        }
64
65 8
        if ($object->getProperty('remote_id')) {
66 8
            $createStruct->remoteId = $object->getProperty('remote_id');
67 8
        }
68
69 8
        $this->assignStructFieldValues($object, $createStruct);
70 8
    }
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 16
    public function mapObjectToUpdateStruct(ContentObject $object, ContentUpdateStruct $contentUpdateStruct)
81
    {
82 16
        $this->assignStructFieldValues($object, $contentUpdateStruct);
83 16
    }
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 20
    private function assignStructFieldValues(ContentObject $object, $struct)
92
    {
93 20
        foreach ($object->data as $key => $value) {
94 20
            if (is_array($value)) {
95 2
                $value = end($value);
96 2
            }
97
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