Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| GroupMapper | |
0.00% |
0 / 14 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
| getPermissionForModule | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| countMembers | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package Modules\Admin\Models |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace Modules\Admin\Models; |
| 16 | |
| 17 | use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; |
| 18 | use phpOMS\DataStorage\Database\Query\Builder; |
| 19 | |
| 20 | /** |
| 21 | * Group mapper class. |
| 22 | * |
| 23 | * @package Modules\Admin\Models |
| 24 | * @license OMS License 2.0 |
| 25 | * @link https://jingga.app |
| 26 | * @since 1.0.0 |
| 27 | * |
| 28 | * @template T of Group |
| 29 | * @extends DataMapperFactory<T> |
| 30 | */ |
| 31 | final class GroupMapper extends DataMapperFactory |
| 32 | { |
| 33 | /** |
| 34 | * Columns. |
| 35 | * |
| 36 | * @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}> |
| 37 | * @since 1.0.0 |
| 38 | */ |
| 39 | public const COLUMNS = [ |
| 40 | 'group_id' => ['name' => 'group_id', 'type' => 'int', 'internal' => 'id'], |
| 41 | 'group_name' => ['name' => 'group_name', 'type' => 'string', 'internal' => 'name', 'autocomplete' => true], |
| 42 | 'group_status' => ['name' => 'group_status', 'type' => 'int', 'internal' => 'status'], |
| 43 | 'group_desc' => ['name' => 'group_desc', 'type' => 'string', 'internal' => 'description'], |
| 44 | 'group_desc_raw' => ['name' => 'group_desc_raw', 'type' => 'string', 'internal' => 'descriptionRaw'], |
| 45 | 'group_created' => ['name' => 'group_created', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true], |
| 46 | ]; |
| 47 | |
| 48 | /** |
| 49 | * Model to use by the mapper. |
| 50 | * |
| 51 | * @var class-string<T> |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | public const MODEL = Group::class; |
| 55 | |
| 56 | /** |
| 57 | * Primary table. |
| 58 | * |
| 59 | * @var string |
| 60 | * @since 1.0.0 |
| 61 | */ |
| 62 | public const TABLE = 'group'; |
| 63 | |
| 64 | /** |
| 65 | * Primary field name. |
| 66 | * |
| 67 | * @var string |
| 68 | * @since 1.0.0 |
| 69 | */ |
| 70 | public const PRIMARYFIELD = 'group_id'; |
| 71 | |
| 72 | /** |
| 73 | * Created at column |
| 74 | * |
| 75 | * @var string |
| 76 | * @since 1.0.0 |
| 77 | */ |
| 78 | public const CREATED_AT = 'group_created'; |
| 79 | |
| 80 | /** |
| 81 | * Has many relation. |
| 82 | * |
| 83 | * @var array<string, array{mapper:class-string, table:string, self?:?string, external?:?string, column?:string}> |
| 84 | * @since 1.0.0 |
| 85 | */ |
| 86 | public const HAS_MANY = [ |
| 87 | 'accounts' => [ |
| 88 | 'mapper' => AccountMapper::class, |
| 89 | 'table' => 'account_group', |
| 90 | 'external' => 'account_group_account', |
| 91 | 'self' => 'account_group_group', |
| 92 | ], |
| 93 | 'permissions' => [ |
| 94 | 'mapper' => GroupPermissionMapper::class, |
| 95 | 'table' => 'group_permission', |
| 96 | 'external' => null, |
| 97 | 'self' => 'group_permission_group', |
| 98 | ], |
| 99 | ]; |
| 100 | |
| 101 | /** |
| 102 | * Get groups which reference a certain module |
| 103 | * |
| 104 | * @param string $module Module |
| 105 | * |
| 106 | * @return array |
| 107 | * |
| 108 | * @since 1.0.0 |
| 109 | */ |
| 110 | public static function getPermissionForModule(string $module) : array |
| 111 | { |
| 112 | $query = self::getQuery(); |
| 113 | $query->innerJoin(GroupPermissionMapper::TABLE) |
| 114 | ->on(self::TABLE . '_d1.group_id', '=', GroupPermissionMapper::TABLE . '.group_permission_group') |
| 115 | ->where(GroupPermissionMapper::TABLE . '.group_permission_module', '=', $module); |
| 116 | |
| 117 | return self::getAll()->execute($query); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Count the number of group members |
| 122 | * |
| 123 | * @param int $group Group to inspect (0 = all groups) |
| 124 | * |
| 125 | * @return array<string, int> |
| 126 | * |
| 127 | * @since 1.0.0 |
| 128 | */ |
| 129 | public static function countMembers(int $group = 0) : array |
| 130 | { |
| 131 | $query = new Builder(self::$db); |
| 132 | $query->select(self::HAS_MANY['accounts']['self']) |
| 133 | ->select('COUNT(' . self::HAS_MANY['accounts']['external'] . ')') |
| 134 | ->from(self::HAS_MANY['accounts']['table']) |
| 135 | ->groupBy(self::HAS_MANY['accounts']['self']); |
| 136 | |
| 137 | if ($group !== 0) { |
| 138 | $query->where(self::HAS_MANY['accounts']['self'], '=', $group); |
| 139 | } |
| 140 | |
| 141 | $result = $query->execute()?->fetchAll(\PDO::FETCH_KEY_PAIR); |
| 142 | |
| 143 | return $result === null ? [] : $result; |
| 144 | } |
| 145 | } |