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

UserGroupManager::find()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 23
ccs 15
cts 15
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\Exception\UnsupportedObjectOperationException;
21
use Transfer\EzPlatform\Repository\Values\UserGroupObject;
22
use Transfer\EzPlatform\Repository\Manager\Type\CreatorInterface;
23
use Transfer\EzPlatform\Repository\Manager\Type\FinderInterface;
24
use Transfer\EzPlatform\Repository\Manager\Type\RemoverInterface;
25
use Transfer\EzPlatform\Repository\Manager\Type\UpdaterInterface;
26
use eZ\Publish\API\Repository\Values\User\UserGroup;
27
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
28
29
/**
30
 * User Group manager.
31
 *
32
 * @internal
33
 */
34
class UserGroupManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface, FinderInterface
35
{
36
    /**
37
     * @var Repository
38
     */
39
    private $repository;
40
41
    /**
42
     * @var LoggerInterface
43
     */
44
    private $logger;
45
46
    /**
47
     * @var UserService
48
     */
49
    private $userService;
50
51
    /**
52
     * @var ContentService
53
     */
54
    private $contentService;
55
56
    /**
57
     * @var ContentTypeService
58
     */
59
    private $contentTypeService;
60
61
    /**
62
     * @param Repository $repository
63
     */
64 6
    public function __construct(Repository $repository)
65
    {
66 6
        $this->repository = $repository;
67 6
        $this->userService = $repository->getUserService();
68 6
        $this->contentService = $repository->getContentService();
69 6
        $this->contentTypeService = $repository->getContentTypeService();
70 6
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 22
    public function setLogger(LoggerInterface $logger)
76
    {
77 22
        $this->logger = $logger;
78 22
    }
79
80
    /**
81
     * Load a UserGroup by remote_id or id.
82
     *
83
     * @param ValueObject $object
84
     * @param bool        $throwException
85
     *
86
     * @return UserGroup|false
87
     *
88
     * @throws NotFoundException
89
     */
90 15
    public function find(ValueObject $object, $throwException = false)
91 1
    {
92
        try {
93 15
            if (isset($object->data['remote_id'])) {
94 3
                $contentObject = $this->contentService->loadContentByRemoteId($object->data['remote_id']);
95 2
                $userGroup = $this->userService->loadUserGroup($contentObject->contentInfo->id);
96 15
            } elseif ($object->getProperty('id')) {
97 15
                $userGroup = $this->userService->loadUserGroup($object->getProperty('id'));
98 15
            }
99 15
        } catch (NotFoundException $notFoundException) {
100 3
            $exception = $notFoundException;
101
        }
102
103 15
        if (!isset($userGroup)) {
104 15
            if (isset($exception) && $throwException) {
105 1
                throw $exception;
106
            }
107
108 15
            return false;
109
        }
110
111 15
        return $userGroup;
112
    }
113
114
    /**
115
     * Shortcut to get UserGroup by id, mainly to get parent by Id.
116
     *
117
     * @param int  $id
118
     * @param bool $throwException
119
     *
120
     * @return UserGroup|false
121
     *
122
     * @throws NotFoundException
123
     */
124 15
    public function findById($id, $throwException = false)
125
    {
126 15
        return $this->find(new ValueObject([], ['id' => $id]), $throwException);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 15
    public function create(ObjectInterface $object)
133
    {
134 15
        if (!$object instanceof UserGroupObject) {
135 1
            throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object));
136
        }
137
138 15
        $parentUserGroup = $this->findById($object->data['parent_id'], true);
139
140
        // Instantiate usergroup
141 15
        $contentType = $this->contentTypeService->loadContentTypeByIdentifier($object->data['content_type_identifier']);
142 15
        $userGroupCreateStruct = $this->userService->newUserGroupCreateStruct(
143 15
            $object->data['main_language_code'],
144
            $contentType
145 15
        );
146
147
        // Populate usergroup fields
148 15
        $object->getMapper()->populateUserGroupCreateStruct($userGroupCreateStruct);
149
150
        // Create usergroup
151 15
        $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 138 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...
152
153 15
        $object->getMapper()->userGroupToObject($userGroup);
154
155 15
        return $object;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 4
    public function update(ObjectInterface $object)
162
    {
163 4
        if (!$object instanceof UserGroupObject) {
164 1
            throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object));
165
        }
166
167 3
        $userGroup = $this->find($object, true);
168
169 3
        $userGroupUpdateStruct = $this->userService->newUserGroupUpdateStruct();
170 3
        $userGroupUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct();
171
172 3
        $object->getMapper()->populateUserGroupUpdateStruct($userGroupUpdateStruct);
173
174 3
        $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...
175
176 3
        if ($userGroup->parentId !== $object->data['parent_id']) {
177 1
            $newParentGroup = $this->findById($object->data['parent_id'], true);
178 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 177 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...
179 2
            $userGroup = $this->find($object, true);
180 1
        }
181
182 3
        $object->getMapper()->userGroupToObject($userGroup);
0 ignored issues
show
Security Bug introduced by
It seems like $userGroup defined by $this->find($object, true) on line 179 can also be of type false; however, Transfer\EzPlatform\Repo...er::userGroupToObject() 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...
183
184 3
        return $object;
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190 15
    public function createOrUpdate(ObjectInterface $object)
191
    {
192 15
        if (!$object instanceof UserGroupObject) {
193 1
            throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object));
194
        }
195
196 15
        if (!$this->find($object)) {
197 15
            return $this->create($object);
198
        } else {
199 3
            return $this->update($object);
200
        }
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206 2 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...
207
    {
208 2
        if (!$object instanceof UserGroupObject) {
209 1
            throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object));
210
        }
211
212 1
        $userGroup = $this->find($object, true);
213
214 1
        $this->userService->deleteUserGroup($userGroup);
0 ignored issues
show
Security Bug introduced by
It seems like $userGroup defined by $this->find($object, true) on line 212 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...
215
216 1
        return true;
217
    }
218
}
219