| Total Complexity | 3 |
| Total Lines | 39 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | class UserBelongsToRole implements Rule |
||
| 10 | { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var Role |
||
| 14 | */ |
||
| 15 | private $role; |
||
| 16 | /** |
||
| 17 | * @var User |
||
| 18 | */ |
||
| 19 | private $userRepository; |
||
| 20 | /** |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | private $roleId; |
||
| 24 | |||
| 25 | 6 | public function __construct(int $roleId, User $userRepository) |
|
| 26 | { |
||
| 27 | 6 | $this->roleId = $roleId; |
|
| 28 | 6 | $this->userRepository = $userRepository; |
|
| 29 | 6 | $this->roleId = $roleId; |
|
| 30 | 6 | } |
|
| 31 | |||
| 32 | /** |
||
| 33 | * @inheritDoc |
||
| 34 | */ |
||
| 35 | 6 | public function passes($attribute, $value) |
|
| 36 | { |
||
| 37 | return $this->userRepository->getById($value)->roles()->filter(function(Role $role) { |
||
| 38 | 5 | return $role->id() === $this->roleId; |
|
| 39 | 6 | })->count() > 0; |
|
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @inheritDoc |
||
| 44 | */ |
||
| 45 | 1 | public function message() |
|
| 48 | } |
||
| 49 | } |