1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\Module\AssignRoles\Support; |
4
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Contracts\Models\Group; |
6
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Position; |
7
|
|
|
use BristolSU\Support\Logic\Contracts\Audience\LogicAudience; |
8
|
|
|
use BristolSU\Support\Logic\Contracts\LogicRepository; |
9
|
|
|
use BristolSU\Support\Logic\Facade\LogicTester; |
10
|
|
|
use BristolSU\Support\ModuleInstance\ModuleInstance; |
11
|
|
|
use Illuminate\Support\Facades\Cache; |
12
|
|
|
|
13
|
|
|
class RequiredSettingRetrieval |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
|
|
|
|
17
|
|
|
* @var LogicRepository |
18
|
|
|
*/ |
19
|
|
|
private $logicRepository; |
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @var LogicAudience |
22
|
|
|
*/ |
23
|
|
|
private $logicAudience; |
|
|
|
|
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @var Position |
26
|
|
|
*/ |
27
|
|
|
private $positionRepository; |
|
|
|
|
28
|
|
|
|
29
|
|
|
public function __construct(LogicRepository $logicRepository, LogicAudience $logicAudience, Position $positionRepository) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$this->logicRepository = $logicRepository; |
32
|
|
|
$this->logicAudience = $logicAudience; |
33
|
|
|
$this->positionRepository = $positionRepository; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getSettings(Group $group, array $settings, ModuleInstance $moduleInstance) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
if(Cache::has($this->getCacheKey($group, $moduleInstance))) { |
|
|
|
|
39
|
|
|
return Cache::get($this->getCacheKey($group, $moduleInstance)); |
40
|
|
|
} |
41
|
|
|
foreach ((array_key_exists('required', $settings)?$settings['required']:[]) as $setting) { |
42
|
|
|
if ($this->groupIsForSetting($group, $setting)) { |
43
|
|
|
Cache::put($this->getCacheKey($group, $moduleInstance), $setting['required'], 7200); |
44
|
|
|
return $setting['required']; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
throw new SettingRetrievalException('Could not find a setting'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function getCacheKey(Group $group, ModuleInstance $moduleInstance) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
return RequiredSettingRetrieval::class . '.' . $moduleInstance->id() . '.' . $group->id(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function groupIsForSetting(Group $group, array $setting): bool |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
if (!array_key_exists('logic_id', $setting)) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
$logic = $this->logicRepository->getById($setting['logic_id']); |
61
|
|
|
return LogicTester::evaluate($logic, null, $group, null); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
} |