| @@ -1,5 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * UserGroup.php | |
| 4 | + * | |
| 5 | + * The UserGroup class file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <alexanderschneider85@gmail.com> | |
| 10 | + * @copyright 2008-2017 Alexander Schneider | |
| 11 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 | |
| 12 | + * @version SVN: $id$ | |
| 13 | + * @link http://wordpress.org/extend/plugins/user-access-manager/ | |
| 14 | + */ | |
| 2 | 15 | |
| 3 | 16 | declare(strict_types=1); |
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\UserGroup; |
| @@ -7,29 +20,62 @@ | ||
| 7 | 20 | use Exception; |
| 8 | 21 | use UserAccessManager\Config\MainConfig; |
| 9 | 22 | use UserAccessManager\Database\Database; |
| 10 | 23 | use UserAccessManager\Object\ObjectHandler; |
| 24 | +use UserAccessManager\Util\Util; | |
| 25 | +use UserAccessManager\Wrapper\Php; | |
| 11 | 26 | use UserAccessManager\Wrapper\Wordpress; |
| 12 | 27 | |
| 28 | +/** | |
| 29 | + * Class UserGroup | |
| 30 | + * | |
| 31 | + * @package UserAccessManager\UserGroup | |
| 32 | + */ | |
| 13 | 33 | class UserGroup extends AbstractUserGroup |
| 14 | 34 | { |
| 15 | 35 | const USER_GROUP_TYPE = 'UserGroup'; |
| 16 | 36 | |
| 17 | - protected ?string $type = self::USER_GROUP_TYPE; | |
| 18 | - protected ?string $ipRange = null; | |
| 37 | + /** | |
| 38 | + * @var string | |
| 39 | + */ | |
| 40 | + protected $type = self::USER_GROUP_TYPE; | |
| 19 | 41 | |
| 20 | 42 | /** |
| 43 | + * @var string | |
| 44 | + */ | |
| 45 | + protected $ipRange = null; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * UserGroup constructor. | |
| 49 | + * @param Php $php | |
| 50 | + * @param Wordpress $wordpress | |
| 51 | + * @param Database $database | |
| 52 | + * @param MainConfig $config | |
| 53 | + * @param Util $util | |
| 54 | + * @param ObjectHandler $objectHandler | |
| 55 | + * @param AssignmentInformationFactory $assignmentInformationFactory | |
| 56 | + * @param null|string $id | |
| 21 | 57 | * @throws UserGroupTypeException |
| 22 | 58 | */ |
| 23 | 59 | public function __construct( |
| 60 | + Php $php, | |
| 24 | 61 | Wordpress $wordpress, |
| 25 | 62 | Database $database, |
| 26 | 63 | MainConfig $config, |
| 64 | + Util $util, | |
| 27 | 65 | ObjectHandler $objectHandler, |
| 28 | - AssignedObjectsLoader $assignedObjectsLoader, | |
| 29 | - int|string|null $id = null | |
| 66 | + AssignmentInformationFactory $assignmentInformationFactory, | |
| 67 | + $id = null | |
| 30 | 68 | ) { |
| 31 | - parent::__construct($wordpress, $database, $config, $objectHandler, $assignedObjectsLoader); | |
| 69 | + parent::__construct( | |
| 70 | + $php, | |
| 71 | + $wordpress, | |
| 72 | + $database, | |
| 73 | + $config, | |
| 74 | + $util, | |
| 75 | + $objectHandler, | |
| 76 | + $assignmentInformationFactory | |
| 77 | + ); | |
| 32 | 78 | |
| 33 | 79 | if ($id !== null) { |
| 34 | 80 | $this->load($id); |
| 35 | 81 | } |
| @@ -34,84 +80,107 @@ | ||
| 34 | 80 | $this->load($id); |
| 35 | 81 | } |
| 36 | 82 | } |
| 37 | 83 | |
| 38 | - public function getIpRange(): ?string | |
| 84 | + /** | |
| 85 | + * Returns the ip range. | |
| 86 | + * @return array|string | |
| 87 | + */ | |
| 88 | + public function getIpRange() | |
| 39 | 89 | { |
| 40 | 90 | return $this->ipRange; |
| 41 | 91 | } |
| 42 | 92 | |
| 93 | + /** | |
| 94 | + * Returns the ip range as array | |
| 95 | + * @return array | |
| 96 | + */ | |
| 43 | 97 | public function getIpRangeArray(): array |
| 44 | 98 | { |
| 45 | - return explode(';', (string) $this->ipRange); | |
| 99 | + return explode(';', $this->ipRange); | |
| 46 | 100 | } |
| 47 | 101 | |
| 48 | - public function setIpRange(array|string $ipRange): void | |
| 102 | + /** | |
| 103 | + * Sets the ip range. | |
| 104 | + * @param string|array $ipRange The new ip range. | |
| 105 | + */ | |
| 106 | + public function setIpRange($ipRange) | |
| 49 | 107 | { |
| 50 | 108 | $this->ipRange = (is_array($ipRange) === true) ? implode(';', $ipRange) : $ipRange; |
| 51 | 109 | } |
| 52 | 110 | |
| 53 | - public function load(int|string $id): bool | |
| 111 | + /** | |
| 112 | + * Loads the user group. | |
| 113 | + * @param int|string $id | |
| 114 | + * @return bool | |
| 115 | + */ | |
| 116 | + public function load($id): bool | |
| 54 | 117 | { |
| 55 | 118 | $query = $this->database->prepare( |
| 56 | 119 | "SELECT * |
| 57 | - FROM `{$this->database->getUserGroupTable()}` | |
| 58 | - WHERE `ID` = %d | |
| 120 | + FROM {$this->database->getUserGroupTable()} | |
| 121 | + WHERE ID = %d | |
| 59 | 122 | LIMIT 1", |
| 60 | 123 | $id |
| 61 | 124 | ); |
| 62 | 125 | |
| 63 | - $databaseUserGroup = $this->database->getRow($query); | |
| 126 | + $dbUserGroup = $this->database->getRow($query); | |
| 64 | 127 | |
| 65 | - if ($databaseUserGroup === null) { | |
| 66 | - return false; | |
| 128 | + if ($dbUserGroup !== null) { | |
| 129 | + $this->id = $id; | |
| 130 | + $this->name = $dbUserGroup->groupname; | |
| 131 | + $this->description = $dbUserGroup->groupdesc; | |
| 132 | + $this->readAccess = $dbUserGroup->read_access; | |
| 133 | + $this->writeAccess = $dbUserGroup->write_access; | |
| 134 | + $this->ipRange = $dbUserGroup->ip_range; | |
| 135 | + | |
| 136 | + return true; | |
| 67 | 137 | } |
| 68 | 138 | |
| 69 | - $this->assignDatabaseValues($databaseUserGroup); | |
| 70 | - | |
| 71 | - return true; | |
| 139 | + return false; | |
| 72 | 140 | } |
| 73 | 141 | |
| 74 | - public function assignDatabaseValues(object $databaseUserGroup): void | |
| 75 | - { | |
| 76 | - $this->id = $databaseUserGroup->ID; | |
| 77 | - $this->name = $databaseUserGroup->groupname; | |
| 78 | - $this->description = $databaseUserGroup->groupdesc; | |
| 79 | - $this->readAccess = $databaseUserGroup->read_access; | |
| 80 | - $this->writeAccess = $databaseUserGroup->write_access; | |
| 81 | - $this->ipRange = $databaseUserGroup->ip_range; | |
| 82 | - } | |
| 83 | - | |
| 142 | + /** | |
| 143 | + * Saves the user group. | |
| 144 | + * @return bool | |
| 145 | + */ | |
| 84 | 146 | public function save(): bool |
| 85 | 147 | { |
| 86 | - $columns = [ | |
| 87 | - 'groupname' => $this->name, | |
| 88 | - 'groupdesc' => $this->description, | |
| 89 | - 'read_access' => $this->readAccess, | |
| 90 | - 'write_access' => $this->writeAccess, | |
| 91 | - 'ip_range' => $this->ipRange | |
| 92 | - ]; | |
| 148 | + if ($this->id === null) { | |
| 149 | + $return = $this->database->insert( | |
| 150 | + $this->database->getUserGroupTable(), | |
| 151 | + [ | |
| 152 | + 'groupname' => $this->name, | |
| 153 | + 'groupdesc' => $this->description, | |
| 154 | + 'read_access' => $this->readAccess, | |
| 155 | + 'write_access' => $this->writeAccess, | |
| 156 | + 'ip_range' => $this->ipRange | |
| 157 | + ] | |
| 158 | + ); | |
| 93 | 159 | |
| 94 | - if ($this->id !== null) { | |
| 95 | - return $this->database->update( | |
| 160 | + if ($return !== false) { | |
| 161 | + $this->id = (string) $this->database->getLastInsertId(); | |
| 162 | + } | |
| 163 | + } else { | |
| 164 | + $return = $this->database->update( | |
| 96 | 165 | $this->database->getUserGroupTable(), |
| 97 | - $columns, | |
| 166 | + [ | |
| 167 | + 'groupname' => $this->name, | |
| 168 | + 'groupdesc' => $this->description, | |
| 169 | + 'read_access' => $this->readAccess, | |
| 170 | + 'write_access' => $this->writeAccess, | |
| 171 | + 'ip_range' => $this->ipRange | |
| 172 | + ], | |
| 98 | 173 | ['ID' => $this->id] |
| 99 | - ) !== false; | |
| 174 | + ); | |
| 100 | 175 | } |
| 101 | 176 | |
| 102 | - $return = $this->database->insert($this->database->getUserGroupTable(), $columns); | |
| 103 | - | |
| 104 | - if ($return === false) { | |
| 105 | - return false; | |
| 106 | - } | |
| 107 | - | |
| 108 | - $this->id = (string) $this->database->getLastInsertId(); | |
| 109 | - | |
| 110 | - return true; | |
| 177 | + return ($return !== false); | |
| 111 | 178 | } |
| 112 | 179 | |
| 113 | 180 | /** |
| 181 | + * Deletes the user group. | |
| 182 | + * @return bool | |
| 114 | 183 | * @throws Exception |
| 115 | 184 | */ |
| 116 | 185 | public function delete(): bool |
| 117 | 186 | { |
| @@ -118,12 +187,16 @@ | ||
| 118 | 187 | if ($this->id === null) { |
| 119 | 188 | return false; |
| 120 | 189 | } |
| 121 | 190 | |
| 122 | - $deleted = $this->database->delete( | |
| 191 | + $success = $this->database->delete( | |
| 123 | 192 | $this->database->getUserGroupTable(), |
| 124 | 193 | ['ID' => $this->id] |
| 125 | 194 | ); |
| 126 | 195 | |
| 127 | - return ($deleted !== false) && parent::delete(); | |
| 196 | + if ($success !== false) { | |
| 197 | + $success = parent::delete(); | |
| 198 | + } | |
| 199 | + | |
| 200 | + return $success; | |
| 128 | 201 | } |
| 129 | 202 | } |