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