Skip to content

Commit

Permalink
refactor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
orbeji committed Jan 19, 2024
1 parent 847f4c2 commit 9850e48
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
25 changes: 15 additions & 10 deletions src/Command/RouteUsageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,27 @@ private function printResult(array $routesUsage, InputInterface $input, OutputIn
$rows = [];
foreach ($routesUsage as $routeUsage) {
if ($routeUsage['count'] === 0) {
$rows[] = [
new TableCell($routeUsage['value'], ['style' => new TableCellStyle(['fg' => 'red',])]),
new TableCell((string)$routeUsage['count'], ['style' => new TableCellStyle(['fg' => 'red',])]),
new TableCell($routeUsage['date'], ['style' => new TableCellStyle(['fg' => 'red',])]),
];
$rows[] = $this->addRow($routeUsage, 'red');
} else {
$rows[] = [
new TableCell($routeUsage['value'], ['style' => new TableCellStyle(['fg' => 'green',])]),
new TableCell((string)$routeUsage['count'], ['style' => new TableCellStyle(['fg' => 'green',])]),
new TableCell($routeUsage['date'], ['style' => new TableCellStyle(['fg' => 'green',])]),
];
$rows[] = $this->addRow($routeUsage, 'green');
}
}
$symfonyStyle->table(
['Route', '#Uses', 'Last access'],
$rows
);
}

/**
* @param array{value: string, count: int, date: string} $routeUsage
* @return TableCell[]
*/
private function addRow(array $routeUsage, string $color): array
{
return [
new TableCell($routeUsage['value'], ['style' => new TableCellStyle(['fg' => $color,])]),
new TableCell((string)$routeUsage['count'], ['style' => new TableCellStyle(['fg' => $color,])]),
new TableCell($routeUsage['date'], ['style' => new TableCellStyle(['fg' => $color,])]),
];
}
}
4 changes: 3 additions & 1 deletion src/EventSubscriber/LogRoutesSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public function onController(ControllerEvent $controllerEvent): void
{
$request = $controllerEvent->getRequest();
$route = $request->get('_route', '');
Assert::string($route);
if (!is_string($route)) {
return;
}
if ($route === '' || str_starts_with($route, '_')) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Helper/RouteUsageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ private function getRoutesUsageResult(
$unusedRoutes = array();
foreach ($allRoutes as $route) {
$existRouteInArray = $this->existRouteInArray($route, $usedRoutes);
if ($showAll && $existRouteInArray instanceof UsedRoute) {
if (!$existRouteInArray) {
$unusedRoutes[] = ['value' => $route, 'count' => 0, 'date' => '-'];
} elseif ($showAll && $existRouteInArray instanceof UsedRoute) {
$unusedRoutes[] = [
'value' => $existRouteInArray->getRoute(),
'count' => $existRouteInArray->getVisits(),
'date' => date('d/m/Y', $existRouteInArray->getTimestamp()),
];
} elseif (!$existRouteInArray) {
$unusedRoutes[] = ['value' => $route, 'count' => 0, 'date' => '-'];
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Provider/UsageRouteProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@

interface UsageRouteProviderInterface
{
/**
* Everytime a user accesses a route this method is called to store this usage
*/
public function addRoute(UsedRoute $route): void;

/**
* This method aggregates all UsedRoutes by the used route and sums all visits, leaving the timestamp of the last
* visit
* @return UsedRoute[]
*/
public function getRoutesUsage(): array;
Expand Down

0 comments on commit 9850e48

Please sign in to comment.