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

UserGroupManager::find()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 23
ccs 10
cts 10
cp 1
rs 6.7272
cc 7
eloc 14
nc 18
nop 2
crap 7
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 5
     */
63
    public function __construct(Repository $repository)
64 5
    {
65 5
        $this->repository = $repository;
66 5
        $this->userService = $repository->getUserService();
67 5
        $this->contentService = $repository->getContentService();
68 5
        $this->contentTypeService = $repository->getContentTypeService();
69
    }
70
71
    /**
72
     * {@inheritdoc}
73 4
     */
74
    public function setLogger(LoggerInterface $logger)
75 4
    {
76 4
        $this->logger = $logger;
77
    }
78
79
    /**
80
     * Load a UserGroup by remote_id or id.
81
     *
82
     * @param ValueObject $object
83
     * @param bool        $throwException
84
     *
85 17
     * @return UserGroup|false
86
     *
87
     * @throws NotFoundException
88 17
     */
89 4
    public function find(ValueObject $object, $throwException = false)
90 4
    {
91
        try {
92
            if (isset($object->data['remote_id'])) {
93
                $contentObject = $this->contentService->loadContentByRemoteId($object->data['remote_id']);
94
                $userGroup = $this->userService->loadUserGroup($contentObject->contentInfo->id);
95
            } elseif ($object->getProperty('id')) {
96
                $userGroup = $this->userService->loadUserGroup($object->getProperty('id'));
97 16
            }
98
        } catch (NotFoundException $notFoundException) {
99 16
            $exception = $notFoundException;
100 1
        }
101
102
        if (!isset($userGroup)) {
103 15
            if (isset($exception) && $throwException) {
104
                throw $exception;
105 15
            }
106 1
107
            return false;
108
        }
109
110 14
        return $userGroup;
111 14
    }
112 14
113
    /**
114 14
     * Shortcut to get UserGroup by id, mainly to get parent by Id.
115
     *
116
     * @param int  $id
117 14
     * @param bool $throwException
118
     *
119
     * @return UserGroup|false
120 14
     *
121 14
     * @throws NotFoundException
122
     */
123 14
    public function findById($id, $throwException = false)
124
    {
125
        return $this->find(new ValueObject([], ['id' => $id]), $throwException);
126
    }
127
128
    /**
129 7
     * {@inheritdoc}
130
     */
131 7
    public function create(ObjectInterface $object)
132 1
    {
133
        if (!$object instanceof UserGroupObject) {
134
            return;
135 6
        }
136 1
137
        $parentUserGroup = $this->findById($object->data['parent_id'], true);
138
139 5
        // Instantiate usergroup
140
        $contentType = $this->contentTypeService->loadContentTypeByIdentifier($object->data['content_type_identifier']);
141 5
        $userGroupCreateStruct = $this->userService->newUserGroupCreateStruct(
142 1
            $object->data['main_language_code'],
143
            $contentType
144
        );
145 4
146 4
        // Populate usergroup fields
147
        $object->getMapper()->populateUserGroupCreateStruct($userGroupCreateStruct);
148 4
149
        // Create usergroup
150 4
        $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 4
        $object->getMapper()->userGroupToObject($userGroup);
153 1
154 1
        return $object;
155 1
    }
156
157 4
    /**
158
     * {@inheritdoc}
159
     */
160
    public function update(ObjectInterface $object)
161
    {
162
        if (!$object instanceof UserGroupObject) {
163 12
            return;
164
        }
165 12
166 1
        $userGroup = $this->find($object, true);
167
168
        $userGroupUpdateStruct = $this->userService->newUserGroupUpdateStruct();
169 11
        $userGroupUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct();
170 2
171 2
        $object->getMapper()->populateUserGroupUpdateStruct($userGroupUpdateStruct);
172 11
173 11
        $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 2
        if ($userGroup->parentId !== $object->data['parent_id']) {
176
            $newParentGroup = $this->findById($object->data['parent_id'], true);
177
            $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
        }
179
180
        $object->getMapper()->userGroupToObject($userGroup);
181
182 4
        return $object;
183
    }
184 4
185 1
    /**
186
     * {@inheritdoc}
187
     */
188 3 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 2
    {
190 2
        if (!$object instanceof UserGroupObject) {
191 1
            return;
192
        }
193
194 2
        if (!$this->find($object)) {
195 1
            return $this->create($object);
196
        } else {
197
            return $this->update($object);
198 1
        }
199
    }
200 1
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