PluginProbe
User Access Manager / 2.3.13
User Access Manager v2.3.13
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/Command/GroupCommand.php +42 -34 trunk2.3.13 View file →
@@ -16,12 +16,11 @@
16 16 use WP_CLI_Command;
17 17
18 18 class GroupCommand extends WP_CLI_Command
19 19 {
20 - public const FORMATTER_PREFIX = 'uam_user_groups';
20 + const FORMATTER_PREFIX = 'uam_user_groups';
21 21
22 - private const ALLOWED_ACCESS_VALUES = ['group', 'all'];
23 - private const DEFAULT_ACCESS_VALUE = self::ALLOWED_ACCESS_VALUES[0];
22 + private static array $allowedAccessValues = ['group', 'all'];
24 23
25 24 public function __construct(
26 25 private WordpressCli $wordpressCli,
27 26 private UserGroupHandler $userGroupHandler,
@@ -28,9 +27,14 @@
28 27 private UserGroupFactory $userGroupFactory
29 28 ) {
30 29 }
31 30
32 - private function getFormatter(array &$assocArguments): Formatter
31 + /**
32 + * Returns the formatter
33 + * @param $assocArguments
34 + * @return Formatter
35 + */
36 + private function getFormatter(&$assocArguments): Formatter
33 37 {
34 38 return $this->wordpressCli->createFormatter(
35 39 $assocArguments,
36 40 [
@@ -83,13 +87,14 @@
83 87 'roles' => implode(
84 88 ',',
85 89 array_keys($userGroup->getAssignedObjectsByType(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE))
86 90 ),
87 - 'ip_range' => $userGroup->getIpRange() ?? ''
91 + 'ip_range' => $userGroup->getIpRange() !== null ? $userGroup->getIpRange() : ''
88 92 ];
89 93 }
90 94
91 - $this->getFormatter($assocArguments)->display_items($groups);
95 + $formatter = $this->getFormatter($assocArguments);
96 + $formatter->display_items($groups);
92 97 }
93 98
94 99 /**
95 100 * delete groups
@@ -117,15 +122,19 @@
117 122 }
118 123 }
119 124
120 125 /**
121 - * Reports the conflicting group and returns false when the name is already taken.
126 + * Checks if the user group already exists.
127 + * @param mixed $userGroupName
128 + * @return bool
122 129 * @throws ExitException
123 130 * @throws UserGroupTypeException
124 131 */
125 - private function isUserGroupNameAvailable(mixed $userGroupName): bool
132 + private function doesUserGroupExists(mixed $userGroupName): bool
126 133 {
127 - foreach ($this->userGroupHandler->getUserGroups() as $userGroup) {
134 + $userGroups = $this->userGroupHandler->getUserGroups();
135 +
136 + foreach ($userGroups as $userGroup) {
128 137 if ($userGroup->getName() === $userGroupName) {
129 138 $this->wordpressCli->error(
130 139 "Group with the same name '$userGroupName' already exists: {$userGroup->getId()}"
131 140 );
@@ -136,45 +145,37 @@
136 145
137 146 return true;
138 147 }
139 148
140 - private function getArgumentValue(array $arguments, string $name): string
149 + /**
150 + * Returns the argument value.
151 + */
152 + private function getArgumentValue(array $arguments, string $value): string
141 153 {
142 - return isset($arguments[$name]) ? (string) $arguments[$name] : '';
154 + return (isset($arguments[$value]) === true) ? (string) $arguments[$value] : '';
143 155 }
144 156
145 157 /**
146 - * @throws ExitException
158 + * Processes the access value.
147 159 */
148 - private function getAccessValue(array $arguments, string $name, bool $porcelain): string
160 + private function getAccessValue(array $arguments, string $value, bool $porcelain): string
149 161 {
150 - $accessValue = $this->getArgumentValue($arguments, $name);
162 + $accessValue = $this->getArgumentValue($arguments, $value);
151 163
152 - if (in_array($accessValue, self::ALLOWED_ACCESS_VALUES) === true) {
153 - return $accessValue;
154 - }
164 + if (in_array($accessValue, self::$allowedAccessValues) === false) {
165 + if ($porcelain === true) {
166 + $this->wordpressCli->line("setting $value to " . self::$allowedAccessValues[0]);
167 + }
155 168
156 - if ($porcelain === true) {
157 - $this->wordpressCli->line("setting $name to " . self::DEFAULT_ACCESS_VALUE);
169 + $accessValue = self::$allowedAccessValues[0];
158 170 }
159 171
160 - return self::DEFAULT_ACCESS_VALUE;
172 + return $accessValue;
161 173 }
162 174
163 175 /**
176 + * Creates the user group.
164 177 * @throws UserGroupTypeException
165 - */
166 - private function assignRoles(UserGroup $userGroup, string $commaSeparatedRoles): void
167 - {
168 - $userGroup->removeObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE);
169 -
170 - foreach (explode(',', $commaSeparatedRoles) as $role) {
171 - $userGroup->addObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, trim($role));
172 - }
173 - }
174 -
175 - /**
176 - * @throws UserGroupTypeException
177 178 * @throws Exception
178 179 */
179 180 private function createUserGroup(string $userGroupName, array $assocArguments): UserGroup
180 181 {
@@ -190,10 +191,17 @@
190 191 $userGroup->setIpRange($ipRange);
191 192 $userGroup->setReadAccess($readAccess);
192 193 $userGroup->setWriteAccess($writeAccess);
193 194
195 + // add roles
194 196 if (isset($assocArguments['roles']) === true) {
195 - $this->assignRoles($userGroup, $assocArguments['roles']);
197 + $roles = explode(',', $assocArguments['roles']);
198 +
199 + $userGroup->removeObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE);
200 +
201 + foreach ($roles as $role) {
202 + $userGroup->addObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, trim($role));
203 + }
196 204 }
197 205
198 206 $userGroup->save();
199 207
@@ -226,9 +234,9 @@
226 234 }
227 235
228 236 $userGroupName = $arguments[0];
229 237
230 - if ($this->isUserGroupNameAvailable($userGroupName) === false) {
238 + if ($this->doesUserGroupExists($userGroupName) === false) {
231 239 return;
232 240 }
233 241
234 242 $userGroup = $this->createUserGroup($userGroupName, $assocArguments);