ILIAS  trunk Revision v11.0_alpha-1846-g895b5f47236
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Wiring.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\LegalDocuments;
22 
23 use Closure;
24 use Exception;
38 use ilDashboardGUI;
42 use ilStartUpGUI;
43 
44 class Wiring implements UseSlot
45 {
46  private readonly Map $map;
47 
48  public function __construct(private readonly SlotConstructor $slot, ?Map $map = null)
49  {
50  $this->map = $map ?? new Map();
51  }
52 
53  public function afterLogin(callable $after_login): self
54  {
55  return $this->addTo('after-login', Closure::fromCallable($after_login));
56  }
57 
58  public function showInFooter(callable $show): self
59  {
60  return $this->addTo('footer', $this->slot->id(), Closure::fromCallable($show));
61  }
62 
63  public function canWithdraw(WithdrawProcess $withdraw_process): self
64  {
65  $withdraw = $this->protect($withdraw_process->showWithdraw(...), $withdraw_process->isOnGoing(...));
66 
67  return $this->addTo('withdraw', $this->slot->id(), $withdraw)
68  ->addTo('intercept', new ConditionalIntercept($withdraw_process->isOnGoing(...), $this->slot->id(), new Target($this->path(ilLegalDocumentsWithdrawalGUI::class))))
69  ->addTo('logout-text', $this->slot->id(), $withdraw_process->showValidatePasswordMessage(...))
70  ->addTo('show-on-login-page', $this->slot->withdrawalFinished($withdraw_process->withdrawalFinished(...)));
71  }
72 
73  public function showOnLoginPage(callable $show): self
74  {
75  return $this->addTo('show-on-login-page', $this->slot->id(), Closure::fromCallable($show));
76  }
77 
78  public function hasPublicPage(callable $public_page, ?string $goto_name = null): self
79  {
80  $wiring = $this->addTo('public-page', $this->slot->id(), fn(...$args) => new Ok($public_page(...$args)));
81  return null === $goto_name ?
82  $wiring :
83  $wiring->addTo('goto', new ConditionalGotoLink($goto_name, fn() => new Target(ilStartUpGUI::class, 'showLegalDocuments', ['id' => $this->slot->id()])));
84  }
85 
86  public function hasAgreement(Agreement $on_login, ?string $goto_name = null): self
87  {
88  $public_target = new Target(ilStartUpGUI::class, 'showLegalDocuments', ['id' => $this->slot->id()]);
89  $agreement_target = new Target($this->path(ilLegalDocumentsAgreementGUI::class), '', ['id' => $this->slot->id()]);
90 
91  $wiring = $this->addTo('public-page', $this->slot->id(), fn(...$args) => new Ok($on_login->showAgreement(...$args)))
92  ->addTo('agreement-form', $this->slot->id(), $this->protect($on_login->showAgreementForm(...), $on_login->needsToAgree(...)))
93  ->addTo('intercept', new ConditionalIntercept($on_login->needsToAgree(...), $this->slot->id(), $agreement_target));
94 
95  return null === $goto_name ?
96  $wiring :
97  $wiring->addTo('goto', new ConditionalGotoLink($goto_name, fn() => $on_login->needsToAgree() ? $agreement_target : $public_target));
98  }
99 
100  public function hasHistory(): self
101  {
102  $document = $this->map->value()['document'][$this->slot->id()] ?? $this->error('Cannot have a history without documents.');
103  return $this->addTo('history', $this->slot->id(), $this->slot->history($document));
104  }
105 
106  public function onSelfRegistration(SelfRegistration $self_registration): self
107  {
108  return $this->addTo('self-registration', $self_registration);
109  }
110 
111  public function hasOnlineStatusFilter(callable $only_visible_users): self
112  {
113  return $this->addTo('filter-online-users', $only_visible_users);
114  }
115 
116  public function canReadInternalMails(Constraint $constraint): self
117  {
118  return $this->addTo('constrain-internal-mail', $constraint);
119  }
120 
121  public function canUseSoapApi(Constraint $constraint): self
122  {
123  return $this->addTo('use-soap-api', $constraint);
124  }
125 
126  public function hasDocuments(array $content_as_component = [], ?SelectionMap $available_conditions = null): self
127  {
128  $available_conditions ??= new SelectionMap();
129  $repository = $this->slot->documentRepository();
130  $document = $this->slot->document($this->slot->readOnlyDocuments($repository), $available_conditions, $content_as_component);
131 
132  return $this->addTo('document', $this->slot->id(), $document)
133  ->addTo('writable-document', $this->slot->id(), $this->slot->document($repository, $available_conditions, $content_as_component));
134  }
135 
136  public function hasUserManagementFields(callable $field_value): self
137  {
138  return $this->addTo('user-management-fields', $this->slot->id(), $field_value);
139  }
140 
141  public function hasPublicApi(PublicApi $api): self
142  {
143  return $this->addTo('public-api', $this->slot->id(), $api);
144  }
145 
146  public function map(): Map
147  {
148  return $this->map;
149  }
150 
151  private function error(string $message): void
152  {
153  throw new Exception($message);
154  }
155 
156  private function addTo(string $name, $id_or_value, $value = null): self
157  {
158  $map = $this->map;
159  if ($value !== null) {
160  if ($this->map->has($name, $id_or_value)) {
161  throw new Exception('Duplicated entry. Key ' . $id_or_value . ' already exists for ' . $name);
162  }
163  $map = $map->set($name, $id_or_value, $value);
164  } else {
165  $map = $map->add($name, $id_or_value);
166  }
167 
168  return new self($this->slot, $map);
169  }
170 
179  private function protect(Closure $to_be_protected, Closure $protector): Closure
180  {
181  return static fn(...$args): Result => $protector() ? new Ok($to_be_protected(...$args)) : new Error('Not available.');
182  }
183 
187  private function path(string $class): array
188  {
189  return [ilDashboardGUI::class, ilPersonalProfileGUI::class, $class];
190  }
191 }
canWithdraw(WithdrawProcess $withdraw_process)
Definition: Wiring.php:63
__construct(private readonly SlotConstructor $slot, ?Map $map=null)
Definition: Wiring.php:48
onSelfRegistration(SelfRegistration $self_registration)
Definition: Wiring.php:106
canUseSoapApi(Constraint $constraint)
Definition: Wiring.php:121
addTo(string $name, $id_or_value, $value=null)
Definition: Wiring.php:156
protect(Closure $to_be_protected, Closure $protector)
A B
Definition: Wiring.php:179
hasOnlineStatusFilter(callable $only_visible_users)
Definition: Wiring.php:111
showAgreement(string $gui, string $cmd)
hasPublicApi(PublicApi $api)
Definition: Wiring.php:141
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
showInFooter(callable $show)
Definition: Wiring.php:58
afterLogin(callable $after_login)
Definition: Wiring.php:53
hasDocuments(array $content_as_component=[], ?SelectionMap $available_conditions=null)
Definition: Wiring.php:126
path(string $class)
Definition: Wiring.php:187
error(string $message)
Definition: Wiring.php:151
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:30
hasUserManagementFields(callable $field_value)
Definition: Wiring.php:136
canReadInternalMails(Constraint $constraint)
Definition: Wiring.php:116
showAgreementForm(string $gui, string $cmd)
showOnLoginPage(callable $show)
Definition: Wiring.php:73
hasAgreement(Agreement $on_login, ?string $goto_name=null)
Definition: Wiring.php:86
$message
Definition: xapiexit.php:31
hasPublicPage(callable $public_page, ?string $goto_name=null)
Definition: Wiring.php:78