PluginProbe
User Access Manager / 2.1.11
User Access Manager v2.1.11
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/UserGroup/UserGroupAssignmentHandler.php +105 -27 2.3.16 → 2.1.11 View file →
@@ -1,43 +1,107 @@
1 1 <?php
2 -
3 -declare(strict_types=1);
4 -
2 +/**
3 + * UserGroupAssignmentHandler.php
4 + *
5 + * The UserGroupAssignmentHandler class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <[email protected]>
10 + * @copyright 2008-2017 Alexander Schneider
11 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 + * @version SVN: $id$
13 + * @link http://wordpress.org/extend/plugins/user-access-manager/
14 + */
5 15 namespace UserAccessManager\UserGroup;
6 16
7 -use Exception;
8 17 use UserAccessManager\User\UserHandler;
9 18 use UserAccessManager\Util\DateUtil;
10 19
20 +/**
21 + * Class UserGroupAssignmentHandler
22 + *
23 + * @package UserAccessManager\UserGroup
24 + */
11 25 class UserGroupAssignmentHandler
12 26 {
27 + /**
28 + * @var DateUtil
29 + */
30 + protected $dateUtil;
31 +
32 + /**
33 + * @var UserHandler
34 + */
35 + protected $userHandler;
36 +
37 + /**
38 + * @var UserGroupHandler
39 + */
40 + protected $userGroupHandler;
41 +
42 + /**
43 + * @var UserGroupFactory
44 + */
45 + protected $userGroupFactory;
46 +
47 + /**
48 + * @var null|string
49 + */
50 + protected $groupsFromName = null;
51 +
52 + /**
53 + * UserGroupAssignmentHandler constructor.
54 + *
55 + * @param DateUtil $dateUtil
56 + * @param UserHandler $userHandler
57 + * @param UserGroupHandler $userGroupHandler
58 + * @param UserGroupFactory $userGroupFactory
59 + */
13 60 public function __construct(
14 - protected DateUtil $dateUtil,
15 - protected UserHandler $userHandler,
16 - protected UserGroupHandler $userGroupHandler,
17 - protected UserGroupFactory $userGroupFactory
61 + DateUtil $dateUtil,
62 + UserHandler $userHandler,
63 + UserGroupHandler $userGroupHandler,
64 + UserGroupFactory $userGroupFactory
18 65 ) {
66 + $this->dateUtil = $dateUtil;
67 + $this->userHandler = $userHandler;
68 + $this->userGroupHandler = $userGroupHandler;
69 + $this->userGroupFactory = $userGroupFactory;
19 70 }
20 71
21 - private function getDateParameter(array $data, string $name): ?string
72 + /**
73 + * Processes the date parameter.
74 + *
75 + * @param array $data
76 + * @param string $name
77 + *
78 + * @return null|string
79 + */
80 + private function getDateParameter(array $data, $name)
22 81 {
23 82 $isValid = isset($data[$name]['date']) === true && isset($data[$name]['time']) === true
24 - && (string) $data[$name]['date'] !== '' && (string) $data[$name]['time'] !== '';
83 + && (string)$data[$name]['date'] !== '' && (string)$data[$name]['time'] !== '';
25 84
26 - return ($isValid === true) ? ((string) $data[$name]['date']) . 'T' . $data[$name]['time'] : null;
85 + return ($isValid === true) ? (string)$data[$name]['date'].'T'.$data[$name]['time'] : null;
27 86 }
28 87
29 88 /**
30 - * @throws Exception
89 + * Updates the user groups for the given object.
90 + *
91 + * @param AbstractUserGroup[] $filteredUserGroups
92 + * @param string $objectType
93 + * @param string $objectId
94 + * @param array $addUserGroups
95 + * @param array $removeUserGroups
31 96 */
32 97 private function setUserGroups(
33 98 array $filteredUserGroups,
34 - string $objectType,
35 - int|string|null $objectId,
99 + $objectType,
100 + $objectId,
36 101 array $addUserGroups,
37 102 array $removeUserGroups
38 - ): void {
39 - /** @var UserGroup $userGroup */
103 + ) {
40 104 foreach ($filteredUserGroups as $groupId => $userGroup) {
41 105 if (isset($removeUserGroups[$groupId]) === true) {
42 106 $userGroup->removeObject($objectType, $objectId);
43 107 }
@@ -42,9 +106,9 @@
42 106 $userGroup->removeObject($objectType, $objectId);
43 107 }
44 108
45 109 if (isset($addUserGroups[$groupId]['id']) === true
46 - && (int) $addUserGroups[$groupId]['id'] === (int) $groupId
110 + && (int)$addUserGroups[$groupId]['id'] === (int)$groupId
47 111 ) {
48 112 $userGroup->addObject(
49 113 $objectType,
50 114 $objectId,
@@ -55,12 +119,17 @@
55 119 }
56 120 }
57 121
58 122 /**
123 + * Sets the dynamic user groups for the given object.
124 + *
125 + * @param string $objectType
126 + * @param string $objectId
127 + * @param array $addDynamicUserGroups
128 + *
59 129 * @throws UserGroupAssignmentException
60 - * @throws UserGroupTypeException
61 130 */
62 - private function setDynamicGroups(string $objectType, int|string|null $objectId, array $addDynamicUserGroups): void
131 + private function setDynamicGroups($objectType, $objectId, array $addDynamicUserGroups)
63 132 {
64 133 foreach ($addDynamicUserGroups as $dynamicUserGroupKey => $addDynamicUserGroup) {
65 134 $dynamicUserGroupData = explode('|', $dynamicUserGroupKey);
66 135
@@ -82,12 +151,15 @@
82 151 }
83 152 }
84 153
85 154 /**
86 - * @throws UserGroupTypeException
87 - * @throws Exception
155 + * Sets the default user groups for the given object.
156 + *
157 + * @param AbstractUserGroup[] $filteredUserGroups
158 + * @param string $objectType
159 + * @param string $objectId
88 160 */
89 - private function setDefaultGroups(array $filteredUserGroups, string $objectType, int|string|null $objectId): void
161 + private function setDefaultGroups(array $filteredUserGroups, $objectType, $objectId)
90 162 {
91 163 /**
92 164 * @var UserGroup[] $userGroupsToCheck
93 165 */
@@ -105,19 +177,25 @@
105 177 }
106 178 }
107 179
108 180 /**
181 + * Saves the object data to the database.
182 + *
183 + * @param string $objectType
184 + * @param int|string $objectId
185 + * @param array $addUserGroups
186 + * @param array $removeUserGroups
187 + * @param array $addDynamicUserGroups
188 + *
109 189 * @throws UserGroupAssignmentException
110 - * @throws UserGroupTypeException
111 - * @throws Exception
112 190 */
113 191 public function assignObjectToUserGroups(
114 - string $objectType,
115 - int|string|null $objectId,
192 + $objectType,
193 + $objectId,
116 194 array $addUserGroups,
117 195 array $removeUserGroups,
118 196 array $addDynamicUserGroups
119 - ): void {
197 + ) {
120 198 $filteredUserGroups = $this->userGroupHandler->getFilteredUserGroups();
121 199 $this->setUserGroups($filteredUserGroups, $objectType, $objectId, $addUserGroups, $removeUserGroups);
122 200
123 201 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true) {