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/ObjectCommand.php +29 -18 2.3.182.3.4 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,34 +12,39 @@
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 ) {
29 + parent::__construct();
31 30 }
32 31
33 32 /**
33 + * Converts the string to and associative an array of index and group
34 34 * @param AbstractUserGroup[] $userGroups
35 - * @return array<string, int|string> group name to group id
35 + * @return array
36 36 */
37 37 private function getUserGroupNameMap(array $userGroups): array
38 38 {
39 - return array_flip(array_map(
40 - fn(AbstractUserGroup $userGroup) => $userGroup->getName(),
39 + $userGroupNames = array_map(
40 + function (UserGroup $userGroup) {
41 + return $userGroup->getName();
42 + },
41 43 $userGroups
42 - ));
44 + );
45 +
46 + return array_flip($userGroupNames);
43 47 }
44 48
45 49 private function getUserGroupIdAndType(array $namesMap, string $identifier, ?string &$type = ''): int|string
46 50 {
@@ -63,8 +67,9 @@
63 67 ): bool {
64 68 $addUserGroups = [];
65 69 $namesMap = $this->getUserGroupNameMap($userGroups);
66 70
71 + // find the UserGroup object for the ids or strings given on the commandline
67 72 foreach ($userGroupIds as $identifier) {
68 73 $userGroupId = $this->getUserGroupIdAndType($namesMap, $identifier, $type);
69 74
70 75 if (isset($userGroups[$userGroupId]) !== true) {
@@ -110,10 +115,16 @@
110 115 return;
111 116 }
112 117
113 118 $operation = $arguments[0];
119 + $messages = [
120 + self::ACTION_ADD => 'Groups %1$s successfully added to %2$s %3$s',
121 + self::ACTION_UPDATE => 'Successfully updated %2$s %3$s with groups %1$s',
122 + self::ACTION_REMOVE => 'Successfully removed groups: %1$s from %2$s %3$s'
123 + ];
114 124
115 - if (isset(self::SUCCESS_MESSAGES[$operation]) === false) {
125 + // check that an operation is valid
126 + if (isset($messages[$operation]) === false) {
116 127 $this->wordpressCli->error("Operation is not valid: $operation");
117 128 return;
118 129 }
119 130
@@ -148,8 +159,8 @@
148 159 $userGroup->save();
149 160 }
150 161
151 162 $this->wordpressCli->success(
152 - sprintf(self::SUCCESS_MESSAGES[$operation], implode(', ', $userGroupIds), $objectType, $objectId)
163 + sprintf($messages[$operation], implode(', ', $userGroupIds), $objectType, $objectId)
153 164 );
154 165 }
155 166 }