Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.59% |
25 / 27 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| DeployLogParser | |
92.59% |
25 / 27 |
|
50.00% |
2 / 4 |
12.06 | |
0.00% |
0 / 1 |
| parse | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| processLogLine | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| handleEntryStart | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| handleEntryEnd | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Service; |
| 6 | |
| 7 | use LogicException; |
| 8 | |
| 9 | class DeployLogParser |
| 10 | { |
| 11 | /** |
| 12 | * @return array<string, string> |
| 13 | */ |
| 14 | public function parse(string $contents): array |
| 15 | { |
| 16 | $entries = []; |
| 17 | $entry = null; |
| 18 | $dateTime = null; |
| 19 | |
| 20 | foreach (explode("\n", $contents) as $line) { |
| 21 | $line = trim($line); |
| 22 | if ($line === '' || $line === '0') { |
| 23 | continue; |
| 24 | } |
| 25 | |
| 26 | $this->processLogLine($line, $entry, $dateTime, $entries); |
| 27 | } |
| 28 | |
| 29 | return $entries; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param array<string, string> $entries |
| 34 | */ |
| 35 | private function processLogLine(string $line, ?string &$entry, ?string &$dateTime, array &$entries): void |
| 36 | { |
| 37 | if (str_starts_with($line, '>>>==============')) { |
| 38 | $this->handleEntryStart($entry); |
| 39 | |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if (str_starts_with($line, '<<<===========')) { |
| 44 | $this->handleEntryEnd($entry, $dateTime, $entries); |
| 45 | |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | if ('' === $entry) { |
| 50 | // The first line contains the dateTime string |
| 51 | $dateTime = $line; |
| 52 | $entry = $line."\n"; |
| 53 | |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $entry .= $line."\n"; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param-out string $entry |
| 62 | */ |
| 63 | private function handleEntryStart(?string &$entry): void |
| 64 | { |
| 65 | if (!is_null($entry)) { |
| 66 | throw new LogicException('Entry finished string not found'); |
| 67 | } |
| 68 | |
| 69 | $entry = ''; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param array<string, string> $entries |
| 74 | * @param-out null $entry |
| 75 | */ |
| 76 | private function handleEntryEnd(?string &$entry, ?string $dateTime, array &$entries): void |
| 77 | { |
| 78 | if (is_null($entry)) { |
| 79 | throw new LogicException('Entry not started.'); |
| 80 | } |
| 81 | |
| 82 | $entries[(string) $dateTime] = $entry; |
| 83 | $entry = null; |
| 84 | } |
| 85 | } |