Skip to content

Commit

Permalink
Small refactoring for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
bayram-aloware committed Feb 24, 2022
1 parent 3837669 commit bdb7901
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/Middlewares/WorkloadResponseMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,21 @@ public function handle($request, Closure $next)

if ($request->path() == 'horizon/api/workload') {

$env = config('app.env');

$wildcards = collect(config('horizon.environments.' . $env))
->pluck('queue')
->flatten()
->filter(function ($item) {
return Str::contains($item, '*');
})
->values()
->all();
$wildcards = $this->getWildcards();

$groups = collect($response->getOriginalContent())
->map(function ($item) use ($wildcards) {

$queues = $this->normalizeQueueName($item['name']);

foreach ($wildcards as $wildcard) {

foreach ($queues as $queue) {

if ($this->wildcardMatches($wildcard, $queue)) {
if (fnmatch($wildcard, $queue)) {
$item['name'] = $wildcard;
}
}
}

return $item;
})
->groupBy('name')
Expand All @@ -59,9 +49,18 @@ public function handle($request, Closure $next)
return $response;
}

private function wildcardMatches($pattern, $haystack): bool
private function getWildcards(): array
{
return fnmatch($pattern, $haystack);
$env = config('app.env');

return collect(config('horizon.environments.' . $env))
->pluck('queue')
->flatten()
->filter(function ($item) {
return Str::contains($item, '*');
})
->values()
->all();
}

private function normalizeQueueName($queue): array
Expand Down

0 comments on commit bdb7901

Please sign in to comment.