PluginProbe
User Access Manager / 2.2.3
User Access Manager v2.2.3
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 +86 -12 2.3.142.2.3 View file →
@@ -1,5 +1,18 @@
1 1 <?php
2 +/**
3 + * UserGroupAssignmentHandler.php
4 + *
5 + * The UserGroupAssignmentHandler class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <alexanderschneider85@gmail.com>
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 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\UserGroup;
@@ -7,37 +20,84 @@
7 20 use Exception;
8 21 use UserAccessManager\User\UserHandler;
9 22 use UserAccessManager\Util\DateUtil;
10 23
24 +/**
25 + * Class UserGroupAssignmentHandler
26 + *
27 + * @package UserAccessManager\UserGroup
28 + */
11 29 class UserGroupAssignmentHandler
12 30 {
31 + /**
32 + * @var DateUtil
33 + */
34 + protected $dateUtil;
35 +
36 + /**
37 + * @var UserHandler
38 + */
39 + protected $userHandler;
40 +
41 + /**
42 + * @var UserGroupHandler
43 + */
44 + protected $userGroupHandler;
45 +
46 + /**
47 + * @var UserGroupFactory
48 + */
49 + protected $userGroupFactory;
50 +
51 + /**
52 + * UserGroupAssignmentHandler constructor.
53 + * @param DateUtil $dateUtil
54 + * @param UserHandler $userHandler
55 + * @param UserGroupHandler $userGroupHandler
56 + * @param UserGroupFactory $userGroupFactory
57 + */
13 58 public function __construct(
14 - protected DateUtil $dateUtil,
15 - protected UserHandler $userHandler,
16 - protected UserGroupHandler $userGroupHandler,
17 - protected UserGroupFactory $userGroupFactory
59 + DateUtil $dateUtil,
60 + UserHandler $userHandler,
61 + UserGroupHandler $userGroupHandler,
62 + UserGroupFactory $userGroupFactory
18 63 ) {
64 + $this->dateUtil = $dateUtil;
65 + $this->userHandler = $userHandler;
66 + $this->userGroupHandler = $userGroupHandler;
67 + $this->userGroupFactory = $userGroupFactory;
19 68 }
20 69
70 + /**
71 + * Processes the date parameter.
72 + * @param array $data
73 + * @param string $name
74 + * @return null|string
75 + */
21 76 private function getDateParameter(array $data, string $name): ?string
22 77 {
23 78 $isValid = isset($data[$name]['date']) === true && isset($data[$name]['time']) === true
24 79 && (string) $data[$name]['date'] !== '' && (string) $data[$name]['time'] !== '';
25 80
26 - return ($isValid === true) ? ((string) $data[$name]['date']) . 'T' . $data[$name]['time'] : null;
81 + return ($isValid === true) ? (string) $data[$name]['date'] . 'T' . $data[$name]['time'] : null;
27 82 }
28 83
29 84 /**
85 + * Updates the user groups for the given object.
86 + * @param AbstractUserGroup[] $filteredUserGroups
87 + * @param string $objectType
88 + * @param int|string $objectId
89 + * @param array $addUserGroups
90 + * @param array $removeUserGroups
30 91 * @throws Exception
31 92 */
32 93 private function setUserGroups(
33 94 array $filteredUserGroups,
34 95 string $objectType,
35 - int|string|null $objectId,
96 + $objectId,
36 97 array $addUserGroups,
37 98 array $removeUserGroups
38 - ): void {
39 - /** @var UserGroup $userGroup */
99 + ) {
40 100 foreach ($filteredUserGroups as $groupId => $userGroup) {
41 101 if (isset($removeUserGroups[$groupId]) === true) {
42 102 $userGroup->removeObject($objectType, $objectId);
43 103 }
@@ -55,12 +115,16 @@
55 115 }
56 116 }
57 117
58 118 /**
119 + * Sets the dynamic user groups for the given object.
120 + * @param string $objectType
121 + * @param int|string $objectId
122 + * @param array $addDynamicUserGroups
59 123 * @throws UserGroupAssignmentException
60 124 * @throws UserGroupTypeException
61 125 */
62 - private function setDynamicGroups(string $objectType, int|string|null $objectId, array $addDynamicUserGroups): void
126 + private function setDynamicGroups(string $objectType, $objectId, array $addDynamicUserGroups)
63 127 {
64 128 foreach ($addDynamicUserGroups as $dynamicUserGroupKey => $addDynamicUserGroup) {
65 129 $dynamicUserGroupData = explode('|', $dynamicUserGroupKey);
66 130
@@ -82,12 +146,16 @@
82 146 }
83 147 }
84 148
85 149 /**
150 + * Sets the default user groups for the given object.
151 + * @param AbstractUserGroup[] $filteredUserGroups
152 + * @param string $objectType
153 + * @param int|string $objectId
86 154 * @throws UserGroupTypeException
87 155 * @throws Exception
88 156 */
89 - private function setDefaultGroups(array $filteredUserGroups, string $objectType, int|string|null $objectId): void
157 + private function setDefaultGroups(array $filteredUserGroups, string $objectType, $objectId)
90 158 {
91 159 /**
92 160 * @var UserGroup[] $userGroupsToCheck
93 161 */
@@ -105,8 +173,14 @@
105 173 }
106 174 }
107 175
108 176 /**
177 + * Saves the object data to the database.
178 + * @param string $objectType
179 + * @param int|string $objectId
180 + * @param array $addUserGroups
181 + * @param array $removeUserGroups
182 + * @param array $addDynamicUserGroups
109 183 * @throws UserGroupAssignmentException
110 184 * @throws UserGroupTypeException
111 185 * @throws Exception
112 186 */
@@ -111,13 +185,13 @@
111 185 * @throws Exception
112 186 */
113 187 public function assignObjectToUserGroups(
114 188 string $objectType,
115 - int|string|null $objectId,
189 + $objectId,
116 190 array $addUserGroups,
117 191 array $removeUserGroups,
118 192 array $addDynamicUserGroups
119 - ): void {
193 + ) {
120 194 $filteredUserGroups = $this->userGroupHandler->getFilteredUserGroups();
121 195 $this->setUserGroups($filteredUserGroups, $objectType, $objectId, $addUserGroups, $removeUserGroups);
122 196
123 197 if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true) {