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