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