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