Completed
Pull Request — 1.0 (#53)
by Harald
05:27
created

ContentObject   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 86.96%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 2
cbo 5
dl 0
loc 101
ccs 40
cts 46
cp 0.8696
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getMapper() 0 8 2
A __construct() 0 14 2
A setParentLocations() 0 7 2
B addParentLocation() 0 16 5
A convertToLocationObject() 0 18 4
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;
11
12
use eZ\Publish\API\Repository\Values\Content\Location;
13
use Transfer\EzPlatform\Exception\InvalidDataStructureException;
14
use Transfer\EzPlatform\Repository\Values\Mapper\ContentMapper;
15
16
/*
17
18
** Available keys: **
19
20
    $data = [
21
        my_first_field_identifier => my_first_field_value
22
        my_second_field_identifier => my_second_field_value
23
        ...
24
    ],
25
    $properties = [
26
        id                      => int
27
        name                    => string
28
        remote_id               => string
29
        content_info            => \eZ\Publish\API\Repository\Values\Content\ContentInfo
30
        version_info            => \eZ\Publish\API\Repository\Values\Content\VersionInfo
31
        main_object             => bool
32
        parent_locations        => int|Location|LocationObject
33
        content_type_identifier => string
34
        language                => string
35
        main_location_id        => int
36
        action                  => int {@link see \Transfer\EzPlatform\Data\Action\Enum\Action}
37
    ]
38
39
40
** Required on `create`:
41
**** Required by transfer:
42
    * content_type_identifier
43
    * $data with keys matching the required fields of the ContentType
44
45
**** Required by eZ:
46
    * `content_type_identifier`
47
    * `language`
48
    Fields matching the required fields of the ContentType
49
50
** Required on `update`:
51
**** Required by transfer:
52
    One of `id`, or `remote_id` must be present
53
    Atleast one fieldtype
54
55
**** Required by eZ:
56
    One of `id`, or `remote_id` must be present
57
    Atleast one fieldtype
58
59
*/
60
61
/**
62
 * Content object.
63
 */
64
class ContentObject extends EzPlatformObject
65
{
66
    /**
67
     * @var ContentMapper
68
     */
69
    private $mapper;
70
71
    /**
72
     * @return ContentMapper
73
     */
74 6
    public function getMapper()
75
    {
76 6
        if (!$this->mapper) {
77 6
            $this->mapper = new ContentMapper($this);
78 6
        }
79
80 6
        return $this->mapper;
81
    }
82
83
    /**
84
     * Constructs content object.
85
     *
86
     * @param array $data       Field data
87
     * @param array $properties Additional properties
88
     */
89 10
    public function __construct(array $data, array $properties = array())
90
    {
91 10
        parent::__construct($data, array_merge(
92
            array(
93 10
                'main_object' => true,
94 10
                'parent_locations' => [],
95 10
            ),
96
            $properties
97 10
        ));
98
99 10
        if (isset($properties['parent_locations'])) {
100
            $this->setParentLocations($properties['parent_locations']);
101
        }
102 10
    }
103
104
    /**
105
     * Values in array must be of type Location, LocationObject or int.
106
     *
107
     * @param array $parentLocations
108
     */
109 7
    public function setParentLocations(array $parentLocations)
110
    {
111 7
        $this->properties['parent_locations'] = [];
112 7
        foreach ($parentLocations as $location) {
113 6
            $this->addParentLocation($location);
114 7
        }
115 7
    }
116
117
    /**
118
     * Convert parameters to LocationCreateStruct and stores it on the ContentObject.
119
     *
120
     * @param Location|LocationObject|int $parentLocation
121
     *
122
     * @throws InvalidDataStructureException
123
     */
124 6
    public function addParentLocation($parentLocation)
125
    {
126 6
        $locationObject = $this->convertToLocationObject($parentLocation);
127
128 6
        if (!isset($locationObject->data['parent_location_id']) || (int) $locationObject->data['parent_location_id'] < 1) {
129
            throw new InvalidDataStructureException('Parent location id must be an integer of 2 or above.');
130
        }
131
132 6
        if (!isset($locationObject->data['content_id'])) {
133 3
            if ($this->getProperty('id')) {
134
                $locationObject->data['content_id'] = $this->getProperty('id');
135
            }
136 3
        }
137
138 6
        $this->properties['parent_locations'][$locationObject->data['parent_location_id']] = $locationObject;
139 6
    }
140
141
    /**
142
     * @param int|Location|LocationObject $parentLocation
143
     *
144
     * @return LocationObject
145
     */
146 6
    private function convertToLocationObject($parentLocation)
147
    {
148 6
        $locationObject = new LocationObject(array());
149
150 6
        switch (true) {
151 6
            case $parentLocation instanceof Location:
152 5
                $locationObject->getMapper()->locationToObject($parentLocation);
153 5
                break;
154 3
            case $parentLocation instanceof LocationObject:
155 2
                $locationObject = $parentLocation;
156 2
                break;
157 3
            case is_int($parentLocation):
158 3
                $locationObject->data['parent_location_id'] = $parentLocation;
159 3
                break;
160
        }
161
162 6
        return $locationObject;
163
    }
164
}
165