Completed
Pull Request — 1.0 (#63)
by Harald
04:05
created

ContentManager::mapObjectToContentStruct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 6
nc 4
nop 2
crap 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\Manager;
11
12
use eZ\Publish\API\Repository\ContentService;
13
use eZ\Publish\API\Repository\ContentTypeService;
14
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
15
use eZ\Publish\API\Repository\LocationService;
16
use eZ\Publish\API\Repository\Repository;
17
use eZ\Publish\API\Repository\Values\Content\Content;
18
use eZ\Publish\API\Repository\Values\Content\Location;
19
use Psr\Log\LoggerAwareInterface;
20
use Psr\Log\LoggerInterface;
21
use Transfer\Data\ObjectInterface;
22
use Transfer\Data\ValueObject;
23
use Transfer\EzPlatform\Exception\ObjectNotFoundException;
24
use Transfer\EzPlatform\Repository\Values\ContentObject;
25
use Transfer\EzPlatform\Repository\Values\LocationObject;
26
use Transfer\EzPlatform\Exception\UnsupportedObjectOperationException;
27
use Transfer\EzPlatform\Repository\Manager\Type\CreatorInterface;
28
use Transfer\EzPlatform\Repository\Manager\Type\FinderInterface;
29
use Transfer\EzPlatform\Repository\Manager\Type\RemoverInterface;
30
use Transfer\EzPlatform\Repository\Manager\Type\UpdaterInterface;
31
32
/**
33
 * Content manager.
34
 *
35
 * @internal
36
 */
37
class ContentManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface, FinderInterface
38
{
39
    /**
40
     * @var LocationManager
41
     */
42
    private $locationManager;
43
44
    /**
45
     * @var ContentService
46
     */
47
    protected $contentService;
48
49
    /**
50
     * @var ContentTypeService
51
     */
52
    protected $contentTypeService;
53
54
    /**
55
     * @var LocationService
56
     */
57
    protected $locationService;
58
59
    /**
60
     * @var LoggerInterface
61
     */
62
    protected $logger;
63
64
    /**
65
     * @param Repository      $repository
66
     * @param LocationManager $locationManager
67
     */
68
    public function __construct(Repository $repository, LocationManager $locationManager)
69
    {
70 10
        $this->locationManager = $locationManager;
71
        $this->contentService = $repository->getContentService();
72 10
        $this->contentTypeService = $repository->getContentTypeService();
73 10
        $this->locationService = $repository->getLocationService();
74 10
    }
75 10
76 10
    /**
77
     * {@inheritdoc}
78
     */
79
    public function setLogger(LoggerInterface $logger)
80
    {
81 29
        $this->logger = $logger;
82
    }
83 29
84 29
    /**
85
     * {@inheritdoc}
86
     */
87
    public function find(ValueObject $object)
88
    {
89 21
        try {
90
            if ($object->getProperty('remote_id')) {
91 1
                $content = $this->contentService->loadContentByRemoteId($object->getProperty('remote_id'));
92 21
            }
93 21
        } catch (NotFoundException $notFoundException) {
94 16
            // We'll throw our own exception later instead.
95 21
        }
96
97
        if (!isset($content)) {
98
            throw new ObjectNotFoundException(Content::class, array('remote_id'));
99 21
        }
100 10
101
        return $content;
102
    }
103 16
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function create(ObjectInterface $object)
108
    {
109 9
        if (!$object instanceof ContentObject) {
110
            throw new UnsupportedObjectOperationException(ContentObject::class, get_class($object));
111 9
        }
112 1
113
        $createStruct = $this->contentService->newContentCreateStruct(
114
            $this->contentTypeService->loadContentTypeByIdentifier($object->getProperty('content_type_identifier')),
115 8
            $object->getProperty('language')
116 8
        );
117 8
118 8
        $object->getMapper()->mapObjectToCreateStruct($object, $createStruct);
0 ignored issues
show
Bug introduced by
The method mapObjectToCreateStruct() does not seem to exist on object<Transfer\EzPlatfo...s\Mapper\ContentMapper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
120 8
        /** @var LocationObject[] $locationObjects */
121
        $locationObjects = $object->getProperty('parent_locations');
122
        $locationCreateStructs = [];
123 8
        if (is_array($locationObjects) && count($locationObjects) > 0) {
124 8
            foreach ($locationObjects as $locationObject) {
125 8
                $locationCreateStruct = $this->locationService->newLocationCreateStruct($locationObject->data['parent_location_id']);
126 2
                $locationObject->getMapper()->getNewLocationCreateStruct($locationCreateStruct);
127 2
                $locationCreateStructs[] = $locationCreateStruct;
128 2
            }
129 2
        }
130 2
131 2
        $content = $this->contentService->createContent($createStruct, $locationCreateStructs);
132
        $this->contentService->publishVersion($content->versionInfo);
133 8
134 8
        if ($this->logger) {
135
            $this->logger->info(sprintf('Published new version of %s', $object->getProperty('name')), array('ContentManager::create'));
136 8
        }
137 8
138 8
        $object->setProperty('id', $content->contentInfo->id);
139
        $object->setProperty('version_info', $content->versionInfo);
140 8
        $object->setProperty('content_info', $content->contentInfo);
141 8
142 8
        return $object;
143
    }
144 8
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function update(ObjectInterface $object)
149
    {
150 16
        if (!$object instanceof ContentObject) {
151
            throw new UnsupportedObjectOperationException(ContentObject::class, get_class($object));
152 16
        }
153 1
154
        $existingContent = $this->find($object);
155
        if (null === $object->getProperty('content_info')) {
156 15
            $object->setProperty('content_info', $existingContent->contentInfo);
157 15
        }
158 13
159 13
        $contentDraft = $this->contentService->createContentDraft($object->getProperty('content_info'));
160
161 15
        $contentUpdateStruct = $this->contentService->newContentUpdateStruct();
162
        $object->getMapper()->mapObjectToUpdateStruct($object, $contentUpdateStruct);
163 15
164 16
        $contentDraft = $this->contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
165
        $content = $this->contentService->publishVersion($contentDraft->versionInfo);
166 15
167 15
        if ($this->logger) {
168
            $this->logger->info(sprintf('Published new version of %s', $object->getProperty('name')), array('ContentManager::update'));
169 15
        }
170 15
171 15
        $object->setProperty('id', $content->contentInfo->id);
172
        $object->setProperty('version_info', $content->versionInfo);
173 15
        $object->setProperty('content_info', $content->contentInfo);
174 15
175 15
        // Add/Update/Delete parent locations
176
        $this->locationManager->syncronizeLocationsFromContentObject($object);
177
178 15
        return $object;
179
    }
180 15
181
    /**
182
     * {@inheritdoc}
183
     */
184 View Code Duplication
    public function createOrUpdate(ObjectInterface $object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
    {
186 19
        if (!$object instanceof ContentObject) {
187
            throw new UnsupportedObjectOperationException(ContentObject::class, get_class($object));
188 19
        }
189 1
190
        try {
191
            $this->find($object);
192
193 18
            return $this->update($object);
194
        } catch (NotFoundException $notFound) {
195 15
            return $this->create($object);
196 8
        }
197 8
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202 View Code Duplication
    public function remove(ObjectInterface $object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
203
    {
204 3
        if (!$object instanceof ContentObject) {
205
            throw new UnsupportedObjectOperationException(ContentObject::class, get_class($object));
206 3
        }
207 1
208 2
        try {
209
            $content = $this->find($object);
210
            $this->contentService->deleteContent($content->contentInfo);
211 2
212 1
            return true;
213
        } catch (NotFoundException $notFound) {
214 1
            return false;
215 1
        }
216 1
    }
217
218
    /**
219
     * Assigns a main location ID for a content object.
220
     *
221
     * @param ContentObject $object   Content object
222
     * @param Location      $location Location
223
     *
224
     * @return Content
225
     */
226
    public function setMainLocation(ContentObject $object, Location $location)
227
    {
228 3
        $contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct();
229
230 3
        $contentMetadataUpdateStruct->mainLocationId = $location->id;
231
232 3
        $object->setProperty('main_location_id', $location->id);
233
234 3
        return $this->contentService->updateContentMetadata($object->getProperty('content_info'), $contentMetadataUpdateStruct);
235
    }
236
}
237