11<?php
22
3-
43namespace Flowpack \Prunner \Dto ;
54
5+ use Neos \Eel \ProtectedContextAwareInterface ;
66use Neos \Flow \Annotations as Flow ;
77
88/**
99 * @Flow\Proxy(false)
1010 */
11- class TaskResults implements \IteratorAggregate
11+ final class TaskResults implements \IteratorAggregate, \Countable, ProtectedContextAwareInterface
1212{
13+ /**
14+ * @var TaskResult[]
15+ */
1316 protected array $ taskResults ;
1417
18+ private function __construct (array $ taskResults )
19+ {
20+ $ this ->taskResults = $ taskResults ;
21+ }
22+
23+
1524 public function fromJsonArray (array $ in ): self
1625 {
1726 $ converted = [];
1827 foreach ($ in as $ el ) {
1928 $ converted [] = TaskResult::fromJsonArray ($ el );
2029 }
30+ return new self ($ converted );
31+ }
32+
33+ /**
34+ * return a new, immutable list containing only the tasks matching a certain prefix. Especially useful if you want to filter
35+ * the lists before rendering them in Fusion
36+ *
37+ * @param string $prefix
38+ * @return $this
39+ */
40+ public function filteredByPrefix (string $ prefix ): self
41+ {
42+ $ filtered = [];
43+ foreach ($ this ->taskResults as $ taskResult ) {
44+ if (strpos ($ taskResult ->getName (), $ prefix ) === 0 ) {
45+ $ filtered [] = $ taskResult ;
46+ }
47+ }
48+ return new self ($ filtered );
49+ }
50+
51+ /**
52+ * return a new, immutable list containing all the tasks EXCEPT $taskNamesToSkip. Especially useful if you want to filter
53+ * the lists before rendering them in Fusion
54+ *
55+ * @param string $prefix
56+ * @return $this
57+ */
58+ public function withoutTasks (string ...$ taskNamesToSkip ): self
59+ {
60+ $ filtered = [];
61+ foreach ($ this ->taskResults as $ taskResult ) {
62+ if (!in_array ($ taskResult ->getName (), $ taskNamesToSkip )) {
63+ $ filtered [] = $ taskResult ;
64+ }
65+ }
66+ return new self ($ filtered );
67+ }
68+
69+ /**
70+ * an aggregated status string - useful for rendering a status in a UI:
71+ * - If one of the elements is ERROR, it is failed.
72+ * - If one of the elements is RUNNING, it is running.
73+ * - If one element is CANCELED, it is cancelled.
74+ * - If all elements are DONE, it is done.
75+ * - If all elements are WAITING, it is waiting.
76+ * - If all elements are SKIPPED, it is skipped.
77+ * - otherwise, UNKNOWN
78+ *
79+ * @return string
80+ */
81+ public function getAggregatedStatus (): string
82+ {
83+ foreach ($ this ->taskResults as $ taskResult ) {
84+ if ($ taskResult ->getStatus () === TaskResult::STATUS_ERROR ) {
85+ return TaskResult::STATUS_ERROR ;
86+ }
87+ }
88+
89+ foreach ($ this ->taskResults as $ taskResult ) {
90+ if ($ taskResult ->getStatus () === TaskResult::STATUS_RUNNING ) {
91+ return TaskResult::STATUS_RUNNING ;
92+ }
93+ }
94+
95+ foreach ($ this ->taskResults as $ taskResult ) {
96+ if ($ taskResult ->getStatus () === TaskResult::STATUS_CANCELED ) {
97+ return TaskResult::STATUS_CANCELED ;
98+ }
99+ }
100+
101+ $ allDone = true ;
102+ foreach ($ this ->taskResults as $ taskResult ) {
103+ if ($ taskResult ->getStatus () !== TaskResult::STATUS_DONE ) {
104+ $ allDone = false ;
105+ }
106+ }
107+ if ($ allDone ) {
108+ return TaskResult::STATUS_DONE ;
109+ }
110+
111+ foreach ($ this ->taskResults as $ taskResult ) {
112+ if ($ taskResult ->getStatus () === TaskResult::STATUS_WAITING ) {
113+ return TaskResult::STATUS_WAITING ;
114+ }
115+ }
116+
117+ $ allSkipped = true ;
118+ foreach ($ this ->taskResults as $ taskResult ) {
119+ if ($ taskResult ->getStatus () !== TaskResult::STATUS_SKIPPED ) {
120+ $ allSkipped = false ;
121+ }
122+ }
123+ if ($ allSkipped ) {
124+ return TaskResult::STATUS_SKIPPED ;
125+ }
126+
127+ return 'unknown ' ;
128+ }
21129
130+ public function get (string $ taskName ): ?TaskResult
131+ {
132+ foreach ($ this ->taskResults as $ taskResult ) {
133+ if ($ taskResult ->getName () === $ taskName ) {
134+ return $ taskResult ;
135+ }
136+ }
137+ return null ;
22138
23- $ taskResults = new self ();
24- $ taskResults ->taskResults = $ converted ;
25- return $ taskResults ;
26139 }
27140
28141 /**
@@ -32,4 +145,15 @@ public function getIterator()
32145 {
33146 return new \ArrayIterator ($ this ->taskResults );
34147 }
148+
149+ public function count ()
150+ {
151+ return count ($ this ->taskResults );
152+ }
153+
154+ public function allowsCallOfMethod ($ methodName )
155+ {
156+ return true ;
157+ }
158+
35159}
0 commit comments