Completed
Push — 1.0 ( 2156a0...733517 )
by Valentin
07:09
created

ContentObject::addParentLocation()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 8.8571
cc 5
eloc 8
nc 4
nop 1
crap 5
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 3
    public function getMapper()
76
    {
77 3
        if (!$this->mapper) {
78 3
            $this->mapper = new ContentMapper($this);
79 3
        }
80
81 3
        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 22
    public function __construct($data, array $properties = array())
91
    {
92 22
        if ($data instanceof Content) {
93 1
            $this->getMapper()->contentToObject($data);
94 1
        } else {
95 22
            parent::__construct($data, array_merge(
96
                array(
97 22
                    'main_object' => true,
98 22
                    'parent_locations' => [],
99 22
                ),
100
                $properties
101 22
            ));
102
        }
103
104 22
        if (isset($properties['parent_locations'])) {
105 1
            $this->setParentLocations($properties['parent_locations']);
106 1
        }
107 22
    }
108
109
    /**
110
     * Values in array must be of type Location, LocationObject or int.
111
     *
112
     * @param array $parentLocations
113
     */
114 4
    public function setParentLocations(array $parentLocations)
115
    {
116 4
        $this->properties['parent_locations'] = [];
117 4
        foreach ($parentLocations as $location) {
118 4
            $this->addParentLocation($location);
119 4
        }
120 4
    }
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 5
    public function addParentLocation($parentLocation)
130
    {
131 5
        $locationObject = $this->convertToLocationObject($parentLocation);
132
133 5
        if (!isset($locationObject->data['parent_location_id']) || (int) $locationObject->data['parent_location_id'] < 1) {
134 1
            throw new InvalidDataStructureException('Parent location id must be an integer of 2 or above.');
135
        }
136
137 4
        if (!isset($locationObject->data['content_id'])) {
138 4
            if ($this->getProperty('id')) {
139 1
                $locationObject->data['content_id'] = $this->getProperty('id');
140 1
            }
141 4
        }
142
143 4
        $this->properties['parent_locations'][$locationObject->data['parent_location_id']] = $locationObject;
144 4
    }
145
146
    /**
147
     * @param int|Location|LocationObject $parentLocation
148
     *
149
     * @return LocationObject
150
     */
151 5
    private function convertToLocationObject($parentLocation)
152
    {
153 5
        $locationObject = new LocationObject(array());
154
155 5
        switch (true) {
156 5
            case $parentLocation instanceof Location:
157 1
                $locationObject->getMapper()->locationToObject($parentLocation);
158 1
                break;
159 5
            case is_int($parentLocation):
160 4
                $locationObject->data['parent_location_id'] = $parentLocation;
161 4
                break;
162 4
            case $parentLocation instanceof LocationObject:
163 4
            default:
164 4
                $locationObject = $parentLocation;
165 4
        }
166
167 5
        return $locationObject;
168
    }
169
}
170