Completed
Pull Request — 1.0 (#53)
by Harald
06:08
created

ContentObject   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 83.67%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 16
c 3
b 0
f 1
lcom 2
cbo 5
dl 0
loc 105
ccs 41
cts 49
cp 0.8367
rs 10

5 Methods

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