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

UserGroupManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
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
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
        /* @todo update with mapper */
153 2
        $object->setProperty('id', $userGroup->id);
154
155 2
        return $object;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 1
    public function update(ObjectInterface $object)
162
    {
163 1
        if (!$object instanceof UserGroupObject) {
164
            return;
165
        }
166
167 1
        $userGroup = $this->find($object, true);
168
169 1
        $userGroupUpdateStruct = $this->userService->newUserGroupUpdateStruct();
170 1
        $userGroupUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct();
171
172 1
        $object->getMapper()->populateUserGroupUpdateStruct($userGroupUpdateStruct);
173
174 1
        $this->userService->updateUserGroup($userGroup, $userGroupUpdateStruct);
0 ignored issues
show
Security Bug introduced by
It seems like $userGroup defined by $this->find($object, true) on line 167 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?

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...
175
176 1
        if ($userGroup->parentId !== $object->data['parent_id']) {
177
            $newParentGroup = $this->findById($object->data['parent_id'], true);
178
            $this->userService->moveUserGroup($userGroup, $newParentGroup);
0 ignored issues
show
Security Bug introduced by
It seems like $userGroup defined by $this->find($object, true) on line 167 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...
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
        }
180
181
        /* @todo update object */
182
183 1
        return $object;
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189 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...
190
    {
191 2
        if (!$object instanceof UserGroupObject) {
192
            return;
193
        }
194
195 2
        if (!$this->find($object)) {
196 2
            return $this->create($object);
197
        } else {
198 1
            return $this->update($object);
199
        }
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function remove(ObjectInterface $object)
206
    {
207
        if (!$object instanceof UserGroupObject) {
208
            return;
209
        }
210
211
        $userGroup = $this->find($object, true);
212
213
        $this->userService->deleteUserGroup($userGroup);
0 ignored issues
show
Security Bug introduced by
It seems like $userGroup defined by $this->find($object, true) on line 211 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...
214
215
        return true;
216
    }
217
}
218