Skip to content

Commit

Permalink
FEATURE: convenience getter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-daehne committed Nov 14, 2023
1 parent aec5f1d commit 9f8dbb7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
82 changes: 75 additions & 7 deletions Classes/Dto/Jobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
class Jobs implements \IteratorAggregate
{

/**
* @var Job[]
*/
Expand All @@ -20,7 +21,6 @@ private function __construct(array $jobs)
$this->jobs = $jobs;
}


public static function fromJsonArray(array $in): self
{
$converted = [];
Expand All @@ -42,22 +42,90 @@ public function forPipeline(PipelineName $pipeline): Jobs
return new self($filteredJobs);
}

/**
* @return Jobs all scheduled jobs not yet started
*/
public function waiting(): Jobs
{
return $this->filter(function (Job $job) {
return !$job->getStart();
});
}

/**
* Filter running jobs
*
* @return Jobs
*/
public function running(): Jobs
{
$filteredJobs = [];
foreach ($this->jobs as $job) {
return $this->filter(function (Job $job) {
// running = started jobs which have not finished.
if ($job->getStart() !== null && !$job->getEnd()) {
$filteredJobs[] = $job;
return $job->getStart() !== null && !$job->getEnd();
});
}

/**
* @return Jobs successful, canceled and failed jobs
*/
public function completed(): Jobs
{
return $this->filter(function (Job $job) {
return $job->isCompleted();
});
}

/**
* @return Jobs successfully completed jobs
*/
public function successful(): Jobs
{
return $this->filter(function (Job $job) {
return $job->isCompleted() && !$job->isErrored() && !$job->isCanceled();
});
}

/**
* @return Jobs canceled jobs
*/
public function canceled(): Jobs
{
return $this->filter(function (Job $job) {
return $job->isCanceled();
});
}

/**
* @return Jobs failed jobs
*/
public function errored(): Jobs
{
return $this->filter(function (Job $job) {
return $job->isErrored();
});
}

/**
* @param callable $predicate function(Job $job): bool
* @return Jobs all jobs where $predicate($job)
*/
public function filter(callable $predicate): Jobs
{
$result = [];
foreach ($this->jobs as $job) {
if ($predicate($job)) {
$result[] = $job;
}
}
return new self($result);
}

return new self($filteredJobs);
/**
* @return Job[]
*/
public function getArray(): array
{
return $this->jobs;
}

/**
Expand All @@ -67,4 +135,4 @@ public function getIterator()
{
return new \ArrayIterator($this->jobs);
}
}
}
9 changes: 8 additions & 1 deletion Classes/Dto/Pipelines.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public static function fromJsonArray(array $in): self
return $pipelines;
}

/**
* @return Pipeline[]
*/
public function getArray(): array
{
return $this->pipelines;
}

/**
* @return \Iterator<Pipeline>
Expand All @@ -32,4 +39,4 @@ public function getIterator()
{
return new \ArrayIterator($this->pipelines);
}
}
}
9 changes: 8 additions & 1 deletion Classes/Dto/TaskResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ public function get(string $taskName): ?TaskResult

}

/**
* @return TaskResult[]
*/
public function getArray(): array {
return $this->taskResults;
}

/**
* @return \Iterator<TaskResult>
*/
Expand All @@ -156,4 +163,4 @@ public function allowsCallOfMethod($methodName)
return true;
}

}
}

0 comments on commit 9f8dbb7

Please sign in to comment.