bdelespierre /
gitstats
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bdelespierre\GitStats; |
||
| 4 | |||
| 5 | use Bdelespierre\GitStats\Interfaces\ProcessServiceInterface; |
||
| 6 | |||
| 7 | class Task |
||
| 8 | { |
||
| 9 | protected ProcessServiceInterface $process; |
||
| 10 | protected string $name; |
||
| 11 | protected $command; |
||
| 12 | protected array $patterns = []; |
||
| 13 | protected string $output; |
||
| 14 | |||
| 15 | public function __construct( |
||
| 16 | ProcessServiceInterface $process, |
||
| 17 | string $name, |
||
| 18 | $command, |
||
| 19 | array $patterns |
||
| 20 | ) { |
||
| 21 | $this->process = $process; |
||
| 22 | |||
| 23 | $this->setName($name); |
||
| 24 | $this->setCommand($command); |
||
| 25 | $this->setPatterns($patterns); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function getName(): string |
||
| 29 | { |
||
| 30 | return $this->name; |
||
| 31 | } |
||
| 32 | |||
| 33 | public function getHeaders(): array |
||
| 34 | { |
||
| 35 | if (empty($this->patterns)) { |
||
| 36 | return [$this->getName()]; |
||
| 37 | } |
||
| 38 | |||
| 39 | $headers = []; |
||
| 40 | |||
| 41 | foreach ($this->patterns as $name => $regex) { |
||
| 42 | $headers[] = "{$this->getName()}:{$name}"; |
||
| 43 | } |
||
| 44 | |||
| 45 | return $headers; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function run(...$args): self |
||
| 49 | { |
||
| 50 | if (is_array($this->command)) { |
||
| 51 | $output = $this->process->run($this->command)->getOutput(); |
||
| 52 | } elseif (is_callable($this->command)) { |
||
| 53 | $command = $this->command; |
||
| 54 | $output = $command(...$args); |
||
| 55 | } elseif (is_string($this->command)) { |
||
| 56 | $output = $this->process->exec($this->command); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 57 | } |
||
| 58 | |||
| 59 | $this->output = trim($output); |
||
| 60 | |||
| 61 | return $this; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function getOutput(): ?string |
||
| 65 | { |
||
| 66 | return $this->output; |
||
| 67 | } |
||
| 68 | |||
| 69 | public function toArray() |
||
| 70 | { |
||
| 71 | |||
| 72 | if (empty($this->patterns)) { |
||
| 73 | return [ |
||
| 74 | $this->getName() => $this->getOutput() |
||
| 75 | ]; |
||
| 76 | } |
||
| 77 | |||
| 78 | $array = []; |
||
| 79 | |||
| 80 | foreach ($this->patterns as $name => $pattern) { |
||
| 81 | $array["{$this->getName()}:{$name}"] = $this->match($pattern); |
||
| 82 | } |
||
| 83 | |||
| 84 | return $array; |
||
| 85 | } |
||
| 86 | |||
| 87 | private function setName(string $name) |
||
| 88 | { |
||
| 89 | $this->name = $name; |
||
| 90 | } |
||
| 91 | |||
| 92 | private function setCommand($command) |
||
| 93 | { |
||
| 94 | if (! is_array($command) && ! is_callable($command) && ! is_string($command)) { |
||
| 95 | throw new \InvalidArgumentException("Invalid command: '{$command}'."); |
||
| 96 | } |
||
| 97 | |||
| 98 | $this->command = $command; |
||
| 99 | } |
||
| 100 | |||
| 101 | private function setPatterns(array $patterns) |
||
| 102 | { |
||
| 103 | foreach ($patterns as $regex) { |
||
| 104 | if (false === @preg_match($regex, null)) { |
||
| 105 | throw new \InvalidArgumentException("Invalid regex: '{$regex}'."); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | $this->patterns = $patterns; |
||
| 110 | } |
||
| 111 | |||
| 112 | private function match(string $regex) |
||
| 113 | { |
||
| 114 | $match = preg_match($regex, $this->getOutput(), $matches); |
||
| 115 | |||
| 116 | // if the pattern contains a capturing group |
||
| 117 | if (isset($matches[1])) { |
||
| 118 | return $matches[1]; |
||
| 119 | } |
||
| 120 | |||
| 121 | // did the regex match output? |
||
| 122 | return $match ? true : false; |
||
| 123 | } |
||
| 124 | } |
||
| 125 |