Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Mode
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 withChildren
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 hasCapability
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getByCapability
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/** @noinspection PhpUnhandledExceptionInspection */
4declare(strict_types=1);
5
6// TODO unit test
7
8class Mode {
9  const array relations = [
10    'RW' => ['RO'],
11    'RO' => [],
12    'monitor' => [
13      'monitor-group',
14      'monitor-study'
15    ],
16    'monitor-group' => [],
17    'monitor-study' => [],
18  ];
19
20  // capabilities are defined in /definitions/, this is a digest on what concerns the backend TODO use the /definitions/ maybe
21  const array capabilities = [
22    'run-hot-return' => [
23      'monitorable'
24    ],
25    'run-hot-restart' => [
26      'alwaysNewSession',
27      'monitorable'
28    ],
29    'run-demo' => [
30      'alwaysNewSession'
31    ],
32    'run-trial' => [
33      'monitorable'
34    ],
35    'run-review' => [],
36    'run-simulation' => [],
37    'monitor-group' => [
38      'protectedLogin'
39    ],
40    'monitor-study' => [
41      'protectedLogin'
42    ],
43    'sys-check-login' => [
44      'alwaysNewSession'
45    ],
46  ];
47
48  static function withChildren(string $role): array {
49    if (!isset(Mode::relations[$role])) {
50      return [];
51    }
52
53    $roles = [$role];
54
55    foreach (Mode::relations[$role] as $childRole) {
56      $roles = array_merge($roles, Mode::withChildren($childRole));
57    }
58
59    return $roles;
60  }
61
62  static function hasCapability(string $role, string $capability): bool {
63    return in_array($capability, Mode::capabilities[$role] ?? []);
64  }
65
66  static function getByCapability(string $capability): array {
67    $roles = [];
68    foreach (Mode::capabilities as $role => $capabilities) {
69      if (in_array($capability, $capabilities)) {
70        $roles[] = $role;
71      }
72    }
73    return $roles;
74  }
75}