Completed
Pull Request — 1.0 (#53)
by Harald
09:44 queued 04:24
created

UserGroupManager::createOrUpdate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 12
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3.0416
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\Repository;
15
use eZ\Publish\API\Repository\UserService;
16
use Psr\Log\LoggerAwareInterface;
17
use Psr\Log\LoggerInterface;
18
use Transfer\Data\ObjectInterface;
19
use Transfer\Data\ValueObject;
20
use Transfer\EzPlatform\Repository\Values\UserGroupObject;
21
use Transfer\EzPlatform\Repository\Manager\Type\CreatorInterface;
22
use Transfer\EzPlatform\Repository\Manager\Type\FinderInterface;
23
use Transfer\EzPlatform\Repository\Manager\Type\RemoverInterface;
24
use Transfer\EzPlatform\Repository\Manager\Type\UpdaterInterface;
25
use eZ\Publish\API\Repository\Values\User\UserGroup;
26
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
27
28
/**
29
 * User Group manager.
30
 *
31
 * @internal
32
 */
33
class UserGroupManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface, FinderInterface
34
{
35
    /**
36
     * @var Repository
37
     */
38
    private $repository;
39
40
    /**
41
     * @var LoggerInterface
42
     */
43
    private $logger;
44
45
    /**
46
     * @var UserService
47
     */
48
    private $userService;
49
50
    /**
51
     * @var ContentService
52
     */
53
    private $contentService;
54
55
    /**
56
     * @var ContentTypeService
57
     */
58
    private $contentTypeService;
59
60
    /**
61
     * @param Repository $repository
62
     */
63 2
    public function __construct(Repository $repository)
64
    {
65 2
        $this->repository = $repository;
66 2
        $this->userService = $repository->getUserService();
67 2
        $this->contentService = $repository->getContentService();
68 2
        $this->contentTypeService = $repository->getContentTypeService();
69 2
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 2
    public function setLogger(LoggerInterface $logger)
75
    {
76 2
        $this->logger = $logger;
77 2
    }
78
79
    /**
80
     * Load a UserGroup by remote_id or id.
81
     *
82
     * @param ValueObject $object
83
     * @param bool        $throwException
84
     *
85
     * @return UserGroup|false
86
     *
87
     * @throws NotFoundException
88
     */
89 2
    public function find(ValueObject $object, $throwException = false)
90
    {
91 1
        try {
92 2
            if (isset($object->data['remote_id'])) {
93
                $contentObject = $this->contentService->loadContentByRemoteId($object->data['remote_id']);
94
                $userGroup = $this->userService->loadUserGroup($contentObject->contentInfo->id);
95 2
            } elseif ($object->getProperty('id')) {
96 2
                $userGroup = $this->userService->loadUserGroup($object->getProperty('id'));
97 2
            }
98 2
        } catch (NotFoundException $notFoundException) {
99
            $exception = $notFoundException;
100
        }
101
102 2
        if (!isset($userGroup)) {
103 2
            if (isset($exception) && $throwException) {
104
                throw $exception;
105
            }
106
107 2
            return false;
108
        }
109
110 2
        return $userGroup;
111
    }
112
113
    /**
114
     * Shortcut to get UserGroup by id, mainly to get parent by Id.
115
     *
116
     * @param int  $id
117
     * @param bool $throwException
118
     *
119
     * @return UserGroup|false
120
     *
121
     * @throws NotFoundException
122
     */
123 2
    public function findById($id, $throwException = false)
124
    {
125 2
        return $this->find(new ValueObject([], ['id' => $id]), $throwException);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 2
    public function create(ObjectInterface $object)
132
    {
133 2
        if (!$object instanceof UserGroupObject) {
134
            return;
135
        }
136
137 2
        $parentUserGroup = $this->findById($object->data['parent_id'], true);
138
139
        // Instantiate usergroup
140 2
        $contentType = $this->contentTypeService->loadContentTypeByIdentifier($object->data['content_type_identifier']);
141 2
        $userGroupCreateStruct = $this->userService->newUserGroupCreateStruct(
142 2
            $object->data['main_language_code'],
143
            $contentType
144 2
        );
145
146
        // Populate usergroup fields
147 2
        $object->getMapper()->populateUserGroupCreateStruct($userGroupCreateStruct);
148
149
        // Create usergroup
150 2
        $userGroup = $this->userService->createUserGroup($userGroupCreateStruct, $parentUserGroup);
0 ignored issues
show
Security Bug introduced by
It seems like $parentUserGroup defined by $this->findById($object->data['parent_id'], true) on line 137 can also be of type false; however, eZ\Publish\API\Repositor...vice::createUserGroup() does only seem to accept object<eZ\Publish\API\Re...\Values\User\UserGroup>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
151
152 2
        $object->getMapper()->userGroupToObject($userGroup);
153
154 2
        return $object;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 1
    public function update(ObjectInterface $object)
161
    {
162 1
        if (!$object instanceof UserGroupObject) {
163
            return;
164
        }
165
166 1
        $userGroup = $this->find($object, true);
167
168 1
        $userGroupUpdateStruct = $this->userService->newUserGroupUpdateStruct();
169 1
        $userGroupUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct();
170
171 1
        $object->getMapper()->populateUserGroupUpdateStruct($userGroupUpdateStruct);
172
173 1
        $userGroup = $this->userService->updateUserGroup($userGroup, $userGroupUpdateStruct);
0 ignored issues
show
Security Bug introduced by
It seems like $userGroup can also be of type false; however, eZ\Publish\API\Repositor...vice::updateUserGroup() does only seem to accept object<eZ\Publish\API\Re...\Values\User\UserGroup>, did you maybe forget to handle an error condition?
Loading history...
174
175 1
        if ($userGroup->parentId !== $object->data['parent_id']) {
176 1
            $newParentGroup = $this->findById($object->data['parent_id'], true);
177 1
            $this->userService->moveUserGroup($userGroup, $newParentGroup);
0 ignored issues
show
Security Bug introduced by
It seems like $newParentGroup defined by $this->findById($object->data['parent_id'], true) on line 176 can also be of type false; however, eZ\Publish\API\Repositor...ervice::moveUserGroup() does only seem to accept object<eZ\Publish\API\Re...\Values\User\UserGroup>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
178 1
        }
179
180 1
        $object->getMapper()->userGroupToObject($userGroup);
181
182 1
        return $object;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188 2 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...
189
    {
190 2
        if (!$object instanceof UserGroupObject) {
191
            return;
192
        }
193
194 2
        if (!$this->find($object)) {
195 2
            return $this->create($object);
196
        } else {
197 1
            return $this->update($object);
198
        }
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function remove(ObjectInterface $object)
205
    {
206
        if (!$object instanceof UserGroupObject) {
207
            return;
208
        }
209
210
        $userGroup = $this->find($object, true);
211
212
        $this->userService->deleteUserGroup($userGroup);
0 ignored issues
show
Security Bug introduced by
It seems like $userGroup defined by $this->find($object, true) on line 210 can also be of type false; however, eZ\Publish\API\Repositor...vice::deleteUserGroup() does only seem to accept object<eZ\Publish\API\Re...\Values\User\UserGroup>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
213
214
        return true;
215
    }
216
}
217