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 +106 -27 trunk → 2.1.11 View file →
@@ -1,42 +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 - $value = (string) ($data[$name] ?? '');
82 + $isValid = isset($data[$name]['date']) === true && isset($data[$name]['time']) === true
83 + && (string)$data[$name]['date'] !== '' && (string)$data[$name]['time'] !== '';
24 84
25 - return ($value !== '') ? $value : null;
85 + return ($isValid === true) ? (string)$data[$name]['date'].'T'.$data[$name]['time'] : null;
26 86 }
27 87
28 88 /**
29 - * @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
30 96 */
31 97 private function setUserGroups(
32 98 array $filteredUserGroups,
33 - string $objectType,
34 - int|string|null $objectId,
99 + $objectType,
100 + $objectId,
35 101 array $addUserGroups,
36 102 array $removeUserGroups
37 - ): void {
38 - /** @var UserGroup $userGroup */
103 + ) {
39 104 foreach ($filteredUserGroups as $groupId => $userGroup) {
40 105 if (isset($removeUserGroups[$groupId]) === true) {
41 106 $userGroup->removeObject($objectType, $objectId);
42 107 }
@@ -41,9 +106,9 @@
41 106 $userGroup->removeObject($objectType, $objectId);
42 107 }
43 108
44 109 if (isset($addUserGroups[$groupId]['id']) === true
45 - && (int) $addUserGroups[$groupId]['id'] === (int) $groupId
110 + && (int)$addUserGroups[$groupId]['id'] === (int)$groupId
46 111 ) {
47 112 $userGroup->addObject(
48 113 $objectType,
49 114 $objectId,
@@ -54,12 +119,17 @@
54 119 }
55 120 }
56 121
57 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 + *
58 129 * @throws UserGroupAssignmentException
59 - * @throws UserGroupTypeException
60 130 */
61 - private function setDynamicGroups(string $objectType, int|string|null $objectId, array $addDynamicUserGroups): void
131 + private function setDynamicGroups($objectType, $objectId, array $addDynamicUserGroups)
62 132 {
63 133 foreach ($addDynamicUserGroups as $dynamicUserGroupKey => $addDynamicUserGroup) {
64 134 $dynamicUserGroupData = explode('|', $dynamicUserGroupKey);
65 135
@@ -81,12 +151,15 @@
81 151 }
82 152 }
83 153
84 154 /**
85 - * @throws UserGroupTypeException
86 - * @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
87 160 */
88 - private function setDefaultGroups(array $filteredUserGroups, string $objectType, int|string|null $objectId): void
161 + private function setDefaultGroups(array $filteredUserGroups, $objectType, $objectId)
89 162 {
90 163 /**
91 164 * @var UserGroup[] $userGroupsToCheck
92 165 */
@@ -104,19 +177,25 @@
104 177 }
105 178 }
106 179
107 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 + *
108 189 * @throws UserGroupAssignmentException
109 - * @throws UserGroupTypeException
110 - * @throws Exception
111 190 */
112 191 public function assignObjectToUserGroups(
113 - string $objectType,
114 - int|string|null $objectId,
192 + $objectType,
193 + $objectId,
115 194 array $addUserGroups,
116 195 array $removeUserGroups,
117 196 array $addDynamicUserGroups
118 - ): void {
197 + ) {
119 198 $filteredUserGroups = $this->userGroupHandler->getFilteredUserGroups();
120 199 $this->setUserGroups($filteredUserGroups, $objectType, $objectId, $addUserGroups, $removeUserGroups);
121 200
122 201 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true) {