Passed
Branch 1.0 (df18cb)
by Valentin
06:50
created

ContentObject::setStructCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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
namespace Transfer\EzPlatform\Repository\Values;
10
11
use eZ\Publish\API\Repository\Values\Content\Content;
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
 * Content object.
18
 *
19
 * @see http://transfer-framework.com/docs/1.0/sources_and_targets/ezplatform/the_objects/contentobject.html
20
 */
21
class ContentObject extends EzPlatformObject
22
{
23
    /**
24
     * @var ContentMapper
25
     */
26
    private $mapper;
27
28
    /**
29
     * @return ContentMapper
30
     */
31 22
    public function getMapper()
32
    {
33 22
        if (!$this->mapper) {
34 22
            $this->mapper = new ContentMapper($this);
35 22
        }
36
37 22
        return $this->mapper;
38 1
    }
39
40
    /**
41
     * Allows direct control in ContentCreateStruct and ContentUpdateStruct.
42
     *
43
     * @param \Closure $callback
44
     */
45 2
    public function setStructCallback(\Closure $callback)
46 1
    {
47 2
        $this->setProperty('struct_callback', $callback);
48 2
    }
49
50
    /**
51
     * Constructs content object.
52
     *
53
     * @param array|Content $data       Field data
54
     * @param array         $properties Additional properties
55
     */
56 24
    public function __construct($data, array $properties = array())
57
    {
58 24
        if ($data instanceof Content) {
59 1
            $this->getMapper()->contentToObject($data);
60 1
        } else {
61 24
            parent::__construct($data, array_merge(
62
                array(
63 24
                    'main_object' => true,
64 24
                    'parent_locations' => [],
65 24
                ),
66
                $properties
67 24
            ));
68
        }
69
70 24
        if (isset($properties['parent_locations'])) {
71 2
            $this->setParentLocations($properties['parent_locations']);
72 2
        }
73 24
    }
74
75
    /**
76
     * Values in array must be of type Location, LocationObject or int.
77
     *
78
     * @param array $parentLocations
79
     */
80 5
    public function setParentLocations(array $parentLocations)
81
    {
82 5
        $this->properties['parent_locations'] = [];
83 5
        foreach ($parentLocations as $location) {
84 5
            $this->addParentLocation($location);
85 5
        }
86 5
    }
87
88
    /**
89
     * Convert parameters to LocationCreateStruct and stores it on the ContentObject.
90
     *
91
     * @param Location|LocationObject|int $parentLocation
92
     *
93
     * @throws InvalidDataStructureException
94
     */
95 6
    public function addParentLocation($parentLocation)
96
    {
97 6
        $locationObject = $this->convertToLocationObject($parentLocation);
98
99 6
        if (!isset($locationObject->data['parent_location_id']) || (int) $locationObject->data['parent_location_id'] < 1) {
100 1
            throw new InvalidDataStructureException('Parent location id must be an integer of 2 or above.');
101
        }
102
103 5
        if (!isset($locationObject->data['content_id'])) {
104 5
            if ($this->getProperty('id')) {
105 1
                $locationObject->data['content_id'] = $this->getProperty('id');
106 1
            }
107 5
        }
108
109 5
        $this->properties['parent_locations'][$locationObject->data['parent_location_id']] = $locationObject;
110 5
    }
111
112
    /**
113
     * @param int|Location|LocationObject $parentLocation
114
     *
115
     * @return LocationObject
116
     */
117 6
    private function convertToLocationObject($parentLocation)
118
    {
119 6
        $locationObject = new LocationObject(array());
120
121 6
        switch (true) {
122 6
            case $parentLocation instanceof Location:
123 1
                $locationObject->getMapper()->locationToObject($parentLocation);
124 1
                break;
125 6
            case is_int($parentLocation):
126 4
                $locationObject->data['parent_location_id'] = $parentLocation;
127 4
                break;
128 5
            case $parentLocation instanceof LocationObject:
129 5
            default:
130 5
                $locationObject = $parentLocation;
131 5
        }
132
133 6
        return $locationObject;
134
    }
135
}
136