| @@ -1,8 +1,23 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * GroupCommand.php | |
| 4 | + * | |
| 5 | + * The GroupCommand class file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <alexanderschneider85@gmail.com> | |
| 10 | + * @author Nils Woetzel nils.woetzel@h-its.org | |
| 11 | + * @copyright 2008-2017 Alexander Schneider | |
| 12 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 | |
| 13 | + * @version SVN: $id$ | |
| 14 | + * @link http://wordpress.org/extend/plugins/user-access-manager/ | |
| 15 | + */ | |
| 2 | 16 | |
| 3 | 17 | declare(strict_types=1); |
| 4 | 18 | |
| 19 | + | |
| 5 | 20 | namespace UserAccessManager\Command; |
| 6 | 21 | |
| 7 | 22 | use Exception; |
| 8 | 23 | use UserAccessManager\Object\ObjectHandler; |
| @@ -14,19 +29,52 @@ | ||
| 14 | 29 | use WP_CLI\ExitException; |
| 15 | 30 | use WP_CLI\Formatter; |
| 16 | 31 | use WP_CLI_Command; |
| 17 | 32 | |
| 33 | +/** | |
| 34 | + * Class GroupCommand | |
| 35 | + * | |
| 36 | + * @package UserAccessManager\Command | |
| 37 | + */ | |
| 18 | 38 | class GroupCommand extends WP_CLI_Command |
| 19 | 39 | { |
| 20 | 40 | const FORMATTER_PREFIX = 'uam_user_groups'; |
| 21 | 41 | |
| 22 | - private static array $allowedAccessValues = ['group', 'all']; | |
| 42 | + /** | |
| 43 | + * @var array | |
| 44 | + */ | |
| 45 | + private static $allowedAccessValues = ['group', 'all']; | |
| 23 | 46 | |
| 47 | + /** | |
| 48 | + * @var WordpressCli | |
| 49 | + */ | |
| 50 | + private $wordpressCli; | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * @var UserGroupHandler | |
| 54 | + */ | |
| 55 | + private $userGroupHandler; | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * @var UserGroupFactory | |
| 59 | + */ | |
| 60 | + private $userGroupFactory; | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * ObjectCommand constructor. | |
| 64 | + * @param WordpressCli $wordpressCli | |
| 65 | + * @param UserGroupHandler $userGroupHandler | |
| 66 | + * @param UserGroupFactory $userGroupFactory | |
| 67 | + */ | |
| 24 | 68 | public function __construct( |
| 25 | - private WordpressCli $wordpressCli, | |
| 26 | - private UserGroupHandler $userGroupHandler, | |
| 27 | - private UserGroupFactory $userGroupFactory | |
| 69 | + WordpressCli $wordpressCli, | |
| 70 | + UserGroupHandler $userGroupHandler, | |
| 71 | + UserGroupFactory $userGroupFactory | |
| 28 | 72 | ) { |
| 73 | + $this->wordpressCli = $wordpressCli; | |
| 74 | + $this->userGroupHandler = $userGroupHandler; | |
| 75 | + $this->userGroupFactory = $userGroupFactory; | |
| 76 | + parent::__construct(); | |
| 29 | 77 | } |
| 30 | 78 | |
| 31 | 79 | /** |
| 32 | 80 | * Returns the formatter |
| @@ -50,19 +98,22 @@ | ||
| 50 | 98 | ); |
| 51 | 99 | } |
| 52 | 100 | |
| 53 | 101 | /** |
| 54 | - * List groups | |
| 102 | + * list groups | |
| 55 | 103 | * ## OPTIONS |
| 56 | - * [--format=<format>]: Accepted values: table, csv, JSON, count, ids. Default: table | |
| 104 | + * [--format=<format>] | |
| 105 | + * : Accepted values: table, csv, json, count, ids. Default: table | |
| 57 | 106 | * ## EXAMPLES |
| 58 | 107 | * wp uam groups list |
| 59 | 108 | * @subcommand ls |
| 109 | + * @param array $arguments | |
| 110 | + * @param array $assocArguments | |
| 60 | 111 | * @throws ExitException |
| 61 | 112 | * @throws UserGroupTypeException |
| 62 | 113 | * @throws Exception |
| 63 | 114 | */ |
| 64 | - public function ls(array $arguments, array $assocArguments): void | |
| 115 | + public function ls(array $arguments, array $assocArguments) | |
| 65 | 116 | { |
| 66 | 117 | if (count($arguments) > 0) { |
| 67 | 118 | $this->wordpressCli->error('No arguments excepted. Please use the format option.'); |
| 68 | 119 | return; |
| @@ -98,16 +149,18 @@ | ||
| 98 | 149 | |
| 99 | 150 | /** |
| 100 | 151 | * delete groups |
| 101 | 152 | * ## OPTIONS |
| 102 | - * <group_id>: id of the group(s) to delete; accepts unlimited ids | |
| 153 | + * <group_id> | |
| 154 | + * : id of the group(s) to delete; accepts unlimited ids | |
| 103 | 155 | * ## EXAMPLES |
| 104 | 156 | * wp uam groups del 3 5 |
| 105 | 157 | * @subcommand del |
| 158 | + * @param array $arguments | |
| 106 | 159 | * @throws ExitException |
| 107 | 160 | * @throws UserGroupTypeException |
| 108 | 161 | */ |
| 109 | - public function del(array $arguments): void | |
| 162 | + public function del(array $arguments) | |
| 110 | 163 | { |
| 111 | 164 | if (count($arguments) < 1) { |
| 112 | 165 | $this->wordpressCli->error('Expected: wp uam groups del \<id\> ...'); |
| 113 | 166 | return; |
| @@ -114,11 +167,11 @@ | ||
| 114 | 167 | } |
| 115 | 168 | |
| 116 | 169 | foreach ($arguments as $userGroupId) { |
| 117 | 170 | if ($this->userGroupHandler->deleteUserGroup($userGroupId) === true) { |
| 118 | - $this->wordpressCli->success("Successfully deleted group with id '$userGroupId'."); | |
| 171 | + $this->wordpressCli->success("Successfully deleted group with id '{$userGroupId}'."); | |
| 119 | 172 | } else { |
| 120 | - $this->wordpressCli->error("Group id '$userGroupId' doesn't exists."); | |
| 173 | + $this->wordpressCli->error("Group id '{$userGroupId}' doesn't exists."); | |
| 121 | 174 | } |
| 122 | 175 | } |
| 123 | 176 | } |
| 124 | 177 | |
| @@ -123,14 +176,14 @@ | ||
| 123 | 176 | } |
| 124 | 177 | |
| 125 | 178 | /** |
| 126 | 179 | * Checks if the user group already exists. |
| 127 | - * @param mixed $userGroupName | |
| 180 | + * @param $userGroupName | |
| 128 | 181 | * @return bool |
| 129 | 182 | * @throws ExitException |
| 130 | 183 | * @throws UserGroupTypeException |
| 131 | 184 | */ |
| 132 | - private function doesUserGroupExists(mixed $userGroupName): bool | |
| 185 | + private function doesUserGroupExists($userGroupName): bool | |
| 133 | 186 | { |
| 134 | 187 | $userGroups = $this->userGroupHandler->getUserGroups(); |
| 135 | 188 | |
| 136 | 189 | foreach ($userGroups as $userGroup) { |
| @@ -135,9 +188,9 @@ | ||
| 135 | 188 | |
| 136 | 189 | foreach ($userGroups as $userGroup) { |
| 137 | 190 | if ($userGroup->getName() === $userGroupName) { |
| 138 | 191 | $this->wordpressCli->error( |
| 139 | - "Group with the same name '$userGroupName' already exists: {$userGroup->getId()}" | |
| 192 | + "Group with the same name '{$userGroupName}' already exists: {$userGroup->getId()}" | |
| 140 | 193 | ); |
| 141 | 194 | |
| 142 | 195 | return false; |
| 143 | 196 | } |
| @@ -147,8 +200,11 @@ | ||
| 147 | 200 | } |
| 148 | 201 | |
| 149 | 202 | /** |
| 150 | 203 | * Returns the argument value. |
| 204 | + * @param array $arguments | |
| 205 | + * @param string $value | |
| 206 | + * @return string | |
| 151 | 207 | */ |
| 152 | 208 | private function getArgumentValue(array $arguments, string $value): string |
| 153 | 209 | { |
| 154 | 210 | return (isset($arguments[$value]) === true) ? (string) $arguments[$value] : ''; |
| @@ -155,8 +211,12 @@ | ||
| 155 | 211 | } |
| 156 | 212 | |
| 157 | 213 | /** |
| 158 | 214 | * Processes the access value. |
| 215 | + * @param array $arguments | |
| 216 | + * @param string $value | |
| 217 | + * @param bool $porcelain | |
| 218 | + * @return string | |
| 159 | 219 | */ |
| 160 | 220 | private function getAccessValue(array $arguments, string $value, bool $porcelain): string |
| 161 | 221 | { |
| 162 | 222 | $accessValue = $this->getArgumentValue($arguments, $value); |
| @@ -162,9 +222,9 @@ | ||
| 162 | 222 | $accessValue = $this->getArgumentValue($arguments, $value); |
| 163 | 223 | |
| 164 | 224 | if (in_array($accessValue, self::$allowedAccessValues) === false) { |
| 165 | 225 | if ($porcelain === true) { |
| 166 | - $this->wordpressCli->line("setting $value to " . self::$allowedAccessValues[0]); | |
| 226 | + $this->wordpressCli->line("setting {$value} to " . self::$allowedAccessValues[0]); | |
| 167 | 227 | } |
| 168 | 228 | |
| 169 | 229 | $accessValue = self::$allowedAccessValues[0]; |
| 170 | 230 | } |
| @@ -173,8 +233,11 @@ | ||
| 173 | 233 | } |
| 174 | 234 | |
| 175 | 235 | /** |
| 176 | 236 | * Creates the user group. |
| 237 | + * @param string $userGroupName | |
| 238 | + * @param array $assocArguments | |
| 239 | + * @return UserGroup | |
| 177 | 240 | * @throws UserGroupTypeException |
| 178 | 241 | * @throws Exception |
| 179 | 242 | */ |
| 180 | 243 | private function createUserGroup(string $userGroupName, array $assocArguments): UserGroup |
| @@ -210,12 +273,16 @@ | ||
| 210 | 273 | |
| 211 | 274 | /** |
| 212 | 275 | * add group |
| 213 | 276 | * ## OPTIONS |
| 214 | - * <group_name>: the name of the new group | |
| 215 | - * [--porcelain]: Output just the new post id. | |
| 216 | - * [--roles=<list>]: comma separated list of group associated roles | |
| 217 | - * [--<field>=<value>]: Associative args for a new UamUserGroup object | |
| 277 | + * <group_name> | |
| 278 | + * : the name of the new group | |
| 279 | + * [--porcelain] | |
| 280 | + * : Output just the new post id. | |
| 281 | + * [--roles=<list>] | |
| 282 | + * : comma separated list of group associated roles | |
| 283 | + * [--<field>=<value>] | |
| 284 | + * : Associative args for new UamUserGroup object | |
| 218 | 285 | * allowed fields and values are: |
| 219 | 286 | * desc="", |
| 220 | 287 | * read_access={group,all*}, |
| 221 | 288 | * write_access={group,all*}, |
| @@ -222,12 +289,14 @@ | ||
| 222 | 289 | * ip_range="192.168.0.1-192.168.0.10;192.168.0.20-192.168.0.30" |
| 223 | 290 | * *=default |
| 224 | 291 | * ## EXAMPLES |
| 225 | 292 | * wp uam groups add fighters --read_access=all |
| 293 | + * @param array $arguments | |
| 294 | + * @param array $assocArguments | |
| 226 | 295 | * @throws ExitException |
| 227 | 296 | * @throws UserGroupTypeException |
| 228 | 297 | */ |
| 229 | - public function add(array $arguments, array $assocArguments): void | |
| 298 | + public function add(array $arguments, array $assocArguments) | |
| 230 | 299 | { |
| 231 | 300 | if (isset($arguments[0]) === false) { |
| 232 | 301 | $this->wordpressCli->error("Please provide a group name."); |
| 233 | 302 | return; |
| @@ -244,8 +313,8 @@ | ||
| 244 | 313 | |
| 245 | 314 | if (isset($assocArguments['porcelain']) === true) { |
| 246 | 315 | $this->wordpressCli->line($userGroup->getId()); |
| 247 | 316 | } else { |
| 248 | - $this->wordpressCli->success("Added new group '$userGroupName' with id {$userGroup->getId()}."); | |
| 317 | + $this->wordpressCli->success("Added new group '{$userGroupName}' with id {$userGroup->getId()}."); | |
| 249 | 318 | } |
| 250 | 319 | } |
| 251 | 320 | } |