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/ObjectCommand.php +28 -18 trunk2.3.13 View file →
@@ -1,12 +1,11 @@
1 -<?php
1 +<?php declare(strict_types=1);
2 2
3 -declare(strict_types=1);
4 -
5 3 namespace UserAccessManager\Command;
6 4
7 5 use Exception;
8 6 use UserAccessManager\UserGroup\AbstractUserGroup;
7 +use UserAccessManager\UserGroup\UserGroup;
9 8 use UserAccessManager\UserGroup\UserGroupHandler;
10 9 use UserAccessManager\UserGroup\UserGroupTypeException;
11 10 use UserAccessManager\Wrapper\WordpressCli;
12 11 use WP_CLI\ExitException;
@@ -13,18 +12,17 @@
13 12 use WP_CLI_Command;
14 13
15 14 class ObjectCommand extends WP_CLI_Command
16 15 {
17 - public const ACTION_ADD = 'add';
18 - public const ACTION_UPDATE = 'update';
19 - public const ACTION_REMOVE = 'remove';
16 + const ACTION_ADD = 'add';
17 + const ACTION_UPDATE = 'update';
18 + const ACTION_REMOVE = 'remove';
20 19
21 - private const SUCCESS_MESSAGES = [
22 - self::ACTION_ADD => 'Groups %1$s successfully added to %2$s %3$s',
23 - self::ACTION_UPDATE => 'Successfully updated %2$s %3$s with groups %1$s',
24 - self::ACTION_REMOVE => 'Successfully removed groups: %1$s from %2$s %3$s'
25 - ];
26 -
20 + /**
21 + * ObjectCommand constructor.
22 + * @param WordpressCli $wordpressCli
23 + * @param UserGroupHandler $userGroupHandler
24 + */
27 25 public function __construct(
28 26 private WordpressCli $wordpressCli,
29 27 private UserGroupHandler $userGroupHandler
30 28 ) {
@@ -30,17 +28,22 @@
30 28 ) {
31 29 }
32 30
33 31 /**
32 + * Converts the string to and associative an array of index and group
34 33 * @param AbstractUserGroup[] $userGroups
35 - * @return array<string, int|string> group name to group id
34 + * @return array
36 35 */
37 36 private function getUserGroupNameMap(array $userGroups): array
38 37 {
39 - return array_flip(array_map(
40 - fn(AbstractUserGroup $userGroup) => $userGroup->getName(),
38 + $userGroupNames = array_map(
39 + function (UserGroup $userGroup) {
40 + return $userGroup->getName();
41 + },
41 42 $userGroups
42 - ));
43 + );
44 +
45 + return array_flip($userGroupNames);
43 46 }
44 47
45 48 private function getUserGroupIdAndType(array $namesMap, string $identifier, ?string &$type = ''): int|string
46 49 {
@@ -63,8 +66,9 @@
63 66 ): bool {
64 67 $addUserGroups = [];
65 68 $namesMap = $this->getUserGroupNameMap($userGroups);
66 69
70 + // find the UserGroup object for the ids or strings given on the commandline
67 71 foreach ($userGroupIds as $identifier) {
68 72 $userGroupId = $this->getUserGroupIdAndType($namesMap, $identifier, $type);
69 73
70 74 if (isset($userGroups[$userGroupId]) !== true) {
@@ -110,10 +114,16 @@
110 114 return;
111 115 }
112 116
113 117 $operation = $arguments[0];
118 + $messages = [
119 + self::ACTION_ADD => 'Groups %1$s successfully added to %2$s %3$s',
120 + self::ACTION_UPDATE => 'Successfully updated %2$s %3$s with groups %1$s',
121 + self::ACTION_REMOVE => 'Successfully removed groups: %1$s from %2$s %3$s'
122 + ];
114 123
115 - if (isset(self::SUCCESS_MESSAGES[$operation]) === false) {
124 + // check that an operation is valid
125 + if (isset($messages[$operation]) === false) {
116 126 $this->wordpressCli->error("Operation is not valid: $operation");
117 127 return;
118 128 }
119 129
@@ -148,8 +158,8 @@
148 158 $userGroup->save();
149 159 }
150 160
151 161 $this->wordpressCli->success(
152 - sprintf(self::SUCCESS_MESSAGES[$operation], implode(', ', $userGroupIds), $objectType, $objectId)
162 + sprintf($messages[$operation], implode(', ', $userGroupIds), $objectType, $objectId)
153 163 );
154 164 }
155 165 }