ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
AbstractTask.php
Go to the documentation of this file.
1<?php
2
20
23use ILIAS\BackgroundTasks\Implementation\Values\ScalarValues\BasicScalarValueFactory;
29
35abstract class AbstractTask implements Task
36{
37 use BasicScalarValueFactory;
38
39 public const MAIN_REMOVE = 'bt_main_remove';
40 public const MAIN_ABORT = 'bt_main_abort';
44 protected array $input = [];
45 protected Value $output;
46
50 public function setInput(array $values): void
51 {
52 $this->input = $this->getValues($values);
53 $this->checkTypes($this->input);
54 }
55
56 protected function checkTypes(array $values)
57 {
58 $expectedTypes = $this->getInputTypes();
59
60 foreach ($expectedTypes as $i => $expectedType) {
61 $givenType = $this->extractType($values[$i]);
62 if (!$givenType->isExtensionOf($expectedType)) {
63 throw new InvalidArgumentException("Types did not match when setting input for "
64 . static::class
65 . ". Expected type $expectedType given type $givenType.");
66 }
67 }
68 }
69
74 protected function extractType($value): Type
75 {
76 if (is_a($value, Value::class)) {
77 return $value->getType();
78 }
79 if (is_a($value, Task::class)) {
80 return $value->getOutputType();
81 }
82
83 throw new InvalidArgumentException("Input values must be Tasks or Values (extend BT\\Task or BT\\Value).");
84 }
85
90 public function getOutput(): Value
91 {
92 $thunk = new ThunkValue($this->getOutputType());
93 $thunk->setParentTask($this);
94
95 return $thunk;
96 }
97
102 private function getValues(array $values): array
103 {
104 $inputs = [];
105
106 foreach ($values as $value) {
107 if ($value instanceof Task) {
108 $inputs[] = $value->getOutput();
109 } elseif ($value instanceof Value) {
110 $inputs[] = $value;
111 } else {
112 $inputs[] = $this->wrapScalar($value);
113 }
114 }
115
116 return $inputs;
117 }
118
122 public function getInput(): array
123 {
124 return $this->input;
125 }
126
127 public function getType(): string
128 {
129 return static::class;
130 }
131
137 public function unfoldTask(): array
138 {
139 $list = [$this];
140 foreach ($this->getInput() as $input) {
141 if ($input instanceof ThunkValue) {
142 $list = array_merge($list, $input->getParentTask()->unfoldTask());
143 }
144 }
145
146 return $list;
147 }
148
152 public function getRemoveOption(): Option
153 {
154 return new UserInteractionOption('remove', self::MAIN_REMOVE);
155 }
156
160 public function getAbortOption(): Option
161 {
162 return new UserInteractionOption('abort', self::MAIN_ABORT);
163 }
164}
Class Exception Thrown if some procedure did not get the arguments it expected.
getAbortOption()
Option In case a Job is failed or did not respond for some time, an Abort-Option is displayed....
getRemoveOption()
Option An Option to remove the current task and do some cleanup if possible. This Option is displayed...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: AbstractJob.php:19
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Job.php:19