-
Notifications
You must be signed in to change notification settings - Fork 0
/
PositionCanBeUsed.php
67 lines (58 loc) · 1.82 KB
/
PositionCanBeUsed.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace BristolSU\Module\AssignRoles\Rules;
use BristolSU\ControlDB\Contracts\Repositories\Role;
use BristolSU\Module\AssignRoles\Support\PositionSettingRetrieval;
use BristolSU\Support\Authentication\Contracts\Authentication;
use BristolSU\Support\Logic\Contracts\LogicRepository;
use BristolSU\Support\Logic\Facade\LogicTester;
use Illuminate\Contracts\Validation\Rule;
class PositionCanBeUsed implements Rule
{
/**
* @var PositionSettingRetrieval
*/
private $positionSettingRetrieval;
/**
* @var Role
*/
private $roleRepository;
/**
* @var Authentication
*/
private $authentication;
public function __construct(PositionSettingRetrieval $positionSettingRetrieval, Role $roleRepository, Authentication $authentication)
{
$this->positionSettingRetrieval = $positionSettingRetrieval;
$this->roleRepository = $roleRepository;
$this->authentication = $authentication;
}
/**
* @inheritDoc
*/
public function passes($attribute, $value)
{
try {
$settings = $this->positionSettingRetrieval->getSettings($this->group());
} catch (\Exception $e) {
return false;
}
if(in_array($value, $settings['only_one_role'])) {
$roles = $this->roleRepository->allThroughGroup($this->group());
return $roles->filter(function(\BristolSU\ControlDB\Contracts\Models\Role $role) use ($value) {
return $role->positionId() === (int) $value;
})->count() === 0;
}
return true;
}
protected function group()
{
return $this->authentication->getGroup();
}
/**
* @inheritDoc
*/
public function message()
{
return 'The position has already been used in another role';
}
}