Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.82% |
27 / 33 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Role | |
81.82% |
27 / 33 |
|
66.67% |
4 / 6 |
10.60 | |
0.00% |
0 / 1 |
| getEntityMapping | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
| postProcess | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| addConditionName | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| addConditionNames | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| addConditionRoleId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addOrderBy | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsdb\Query; |
| 4 | |
| 5 | class Role extends Base implements MappingInterface |
| 6 | { |
| 7 | /** |
| 8 | * @var string TABLE mysql table reference |
| 9 | */ |
| 10 | const TABLE = 'role'; |
| 11 | |
| 12 | #[\Override] |
| 13 | public function getEntityMapping() |
| 14 | { |
| 15 | return [ |
| 16 | 'id' => 'role.id', |
| 17 | 'name' => 'role.name', |
| 18 | 'description' => 'role.description', |
| 19 | 'permissions' => self::expression( |
| 20 | '(SELECT GROUP_CONCAT(DISTINCT p.name ORDER BY p.name SEPARATOR \',\') ' |
| 21 | . 'FROM role_permission rp ' |
| 22 | . 'JOIN permission p ON p.id = rp.permission_id ' |
| 23 | . 'WHERE rp.role_id = role.id)' |
| 24 | ), |
| 25 | 'assignedUserCount' => self::expression( |
| 26 | '(SELECT COUNT(DISTINCT ur.user_id) FROM user_role ur WHERE ur.role_id = role.id)' |
| 27 | ), |
| 28 | ]; |
| 29 | } |
| 30 | |
| 31 | #[\Override] |
| 32 | public function postProcess($data) |
| 33 | { |
| 34 | $permissionsKey = $this->getPrefixed('permissions'); |
| 35 | $rawPermissions = $data[$permissionsKey] ?? null; |
| 36 | $data[$permissionsKey] = ($rawPermissions === null || $rawPermissions === '') |
| 37 | ? [] |
| 38 | : explode(',', (string) $rawPermissions); |
| 39 | $countKey = $this->getPrefixed('assignedUserCount'); |
| 40 | if (array_key_exists($countKey, $data)) { |
| 41 | $data[$countKey] = (int) ($data[$countKey] ?? 0); |
| 42 | } |
| 43 | return $data; |
| 44 | } |
| 45 | |
| 46 | public function addConditionName(string $name): self |
| 47 | { |
| 48 | $this->query->where('role.name', '=', $name); |
| 49 | return $this; |
| 50 | } |
| 51 | |
| 52 | public function addConditionNames(array $names): self |
| 53 | { |
| 54 | if ($names === []) { |
| 55 | throw new \InvalidArgumentException('Argument $names must not be empty.'); |
| 56 | } |
| 57 | $this->query->whereIn('role.name', $names); |
| 58 | return $this; |
| 59 | } |
| 60 | |
| 61 | public function addConditionRoleId(int $roleId): self |
| 62 | { |
| 63 | $this->query->where('role.id', '=', $roleId); |
| 64 | return $this; |
| 65 | } |
| 66 | |
| 67 | public function addOrderBy(string $parameter, string $order = 'ASC'): self |
| 68 | { |
| 69 | $this->query->orderBy('role.' . $parameter, $order); |
| 70 | return $this; |
| 71 | } |
| 72 | } |