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