Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| IngressHelper | |
100.00% |
22 / 22 |
|
100.00% |
3 / 3 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parseKeysString | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
4 | |||
| getExistingKeysForMaxfield | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Service; |
| 6 | |
| 7 | use InvalidArgumentException; |
| 8 | use App\Type\AgentKeyInfo; |
| 9 | use App\Type\WaypointMap; |
| 10 | |
| 11 | readonly class IngressHelper |
| 12 | { |
| 13 | public function __construct( |
| 14 | private WayPointHelper $wayPointHelper, |
| 15 | ) {} |
| 16 | |
| 17 | /** |
| 18 | * @return array<int, AgentKeyInfo> |
| 19 | */ |
| 20 | public function parseKeysString(string $string): array |
| 21 | { |
| 22 | $keys = []; |
| 23 | |
| 24 | $lines = strpos($string, "\r\n") ? explode("\r\n", $string) : explode("\n", $string); |
| 25 | |
| 26 | for ($i = 1, $iMax = count($lines); $i < $iMax; ++$i) { |
| 27 | $k = new AgentKeyInfo; |
| 28 | $data = explode("\t", $lines[$i]); |
| 29 | if (count($data) !== 5) { |
| 30 | throw new InvalidArgumentException('Invalid keys string!'); |
| 31 | } |
| 32 | |
| 33 | $k->name = $this->wayPointHelper->cleanName($data[0]); |
| 34 | $k->link = $data[1]; |
| 35 | $k->guid = $data[2]; |
| 36 | $k->count = (int)$data[3]; |
| 37 | $k->capsules = $data[4]; |
| 38 | |
| 39 | $keys[] = $k; |
| 40 | } |
| 41 | |
| 42 | return $keys; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param WaypointMap[] $waypoints |
| 47 | * @return AgentKeyInfo[] |
| 48 | */ |
| 49 | public function getExistingKeysForMaxfield(array $waypoints, string $keys): array |
| 50 | { |
| 51 | $existingKeys = []; |
| 52 | $parsedKeys = $this->parseKeysString($keys); |
| 53 | |
| 54 | foreach ($waypoints as $waypoint) { |
| 55 | foreach ($parsedKeys as $parsedKey) { |
| 56 | if ($parsedKey->guid === $waypoint->guid) { |
| 57 | $existingKeys[] = $parsedKey; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return $existingKeys; |
| 63 | } |
| 64 | } |