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