Repository URL to install this package:
|
Version:
2.2.0 ▾
|
<?php
namespace CrazyFactory\Jobs;
use CrazyFactory\Jobs\Utils\Format;
class JobResult
{
protected $code;
protected $output;
protected $start = 0;
protected $end = 0;
/**
* JobResult constructor.
*
* @param $code
* @param int $start
* @param int $end
* @param array $output
*/
public function __construct($code, $start = 0, $end = 0, array $output = [])
{
$this->code = $code;
$this->start = $start;
$this->end = $end;
$this->output = $output;
}
/**
* @param string $message
* @param int $code
*
* @return JobResult
*/
public static function fatal($message = "encountered an unknown error", $code = 255)
{
echo "FATAL: " . $message . "\n";
return new JobResult($code, 0, 0, ["FATAL: " . $message]);
}
/**
* @return mixed
*/
public function getCode()
{
return $this->code;
}
/**
* @param int $lineLimit
*
* @return array
*/
public function getLastParagraph($lineLimit = 20)
{
$result = [];
if (!$this->hasOutput()) {
return $result;
}
$lines = $this->getOutput();
// remove blank lines from the end of the array
while (!empty($lines) && trim(end($lines)) === '') {
array_pop($lines);
}
// take lines until the next blank line
$count = 0;
while (!empty($lines) && trim(end($lines)) !== '' && $count < $lineLimit) {
$result[] = array_pop($lines);
$count++;
}
return array_reverse($result);
}
/**
* @return bool
*/
public function hasOutput()
{
return !empty($this->output);
}
/**
* @return string[]
*/
public function getOutput()
{
return array_slice($this->output, 0);
}
/**
* @return string|null
*/
public function getLastLine()
{
return end($this->output);
}
/**
* @return int
*/
public function getStart()
{
return $this->start;
}
/**
* @return int
*/
public function getEnd()
{
return $this->end;
}
/**
* @return string
*/
public function getDurationFormatted()
{
return Format::secondsToTimeElapsedString($this->getDuration());
}
/**
* @return int
*/
public function getDuration()
{
return $this->end - $this->start;
}
}