Skip to content

Commit 4d526de

Browse files
authored
Merge pull request #476 from opcodesio/refactor/file-and-folder-sorting
Replace magic strings with enums in sorting logic
2 parents 30a281e + 5825a58 commit 4d526de

13 files changed

+286
-75
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ build
66
composer.lock
77
coverage
88
docs
9+
logs
910
phpunit.xml
1011
phpstan.neon
1112
testbench.yaml

config/log-viewer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Opcodes\LogViewer\Enums\FolderSortingMethod;
3+
use Opcodes\LogViewer\Enums\SortingMethod;
44
use Opcodes\LogViewer\Enums\SortingOrder;
55
use Opcodes\LogViewer\Enums\Theme;
66

@@ -283,13 +283,13 @@
283283
'use_local_storage' => true,
284284

285285
// Method to sort the folders. Other options: `Alphabetical`, `ModifiedTime`
286-
'folder_sorting_method' => FolderSortingMethod::ModifiedTime,
286+
'folder_sorting_method' => SortingMethod::ModifiedTime,
287287

288288
// Order to sort the folders. Other options: `Ascending`, `Descending`
289289
'folder_sorting_order' => SortingOrder::Descending,
290290

291291
// Method for sorting log-files into directories. Other options: `Alphabetical`, `ModifiedTime`
292-
'file_sorting_method' => FolderSortingMethod::ModifiedTime,
292+
'file_sorting_method' => SortingMethod::ModifiedTime,
293293

294294
// Order to sort the logs. Other options: `Ascending`, `Descending`
295295
'log_sorting_order' => SortingOrder::Descending,

src/Enums/FolderSortingMethod.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Opcodes\LogViewer\Enums;
44

5+
/**
6+
* @deprecated Use SortingMethod instead
7+
*/
58
class FolderSortingMethod
69
{
710
public const Alphabetical = 'Alphabetical';

src/Enums/SortingMethod.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Opcodes\LogViewer\Enums;
4+
5+
class SortingMethod
6+
{
7+
public const Alphabetical = 'Alphabetical';
8+
public const ModifiedTime = 'ModifiedTime';
9+
}

src/Http/Controllers/FilesController.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use Illuminate\Http\Request;
66
use Illuminate\Support\Facades\Gate;
77
use Illuminate\Support\Facades\URL;
8-
use Opcodes\LogViewer\Enums\FolderSortingMethod;
8+
use Opcodes\LogViewer\Enums\SortingMethod;
9+
use Opcodes\LogViewer\Enums\SortingOrder;
910
use Opcodes\LogViewer\Facades\LogViewer;
1011
use Opcodes\LogViewer\Http\Resources\LogFileResource;
1112

@@ -14,17 +15,18 @@ class FilesController
1415
public function index(Request $request)
1516
{
1617
$files = LogViewer::getFiles();
17-
$sortingMethod = config('log-viewer.defaults.file_sorting_method', FolderSortingMethod::ModifiedTime);
18+
$sortingMethod = config('log-viewer.defaults.file_sorting_method', SortingMethod::ModifiedTime);
19+
$direction = $this->validateDirection($request->query('direction'));
1820

19-
if ($sortingMethod === FolderSortingMethod::ModifiedTime) {
20-
if ($request->query('direction', 'desc') === 'asc') {
21+
if ($sortingMethod === SortingMethod::ModifiedTime) {
22+
if ($direction === SortingOrder::Ascending) {
2123
$files = $files->sortByEarliestFirst();
2224
} else {
2325
$files = $files->sortByLatestFirst();
2426
}
2527

2628
} else {
27-
if ($request->query('direction', 'desc') === 'asc') {
29+
if ($direction === SortingOrder::Ascending) {
2830
$files = $files->sortAlphabeticallyAsc();
2931
} else {
3032
$files = $files->sortAlphabeticallyDesc();
@@ -34,6 +36,15 @@ public function index(Request $request)
3436
return LogFileResource::collection($files);
3537
}
3638

39+
private function validateDirection(?string $direction): string
40+
{
41+
if ($direction === SortingOrder::Ascending) {
42+
return SortingOrder::Ascending;
43+
}
44+
45+
return SortingOrder::Descending;
46+
}
47+
3748
public function requestDownload(Request $request, string $fileIdentifier)
3849
{
3950
$file = LogViewer::getFile($fileIdentifier);

src/Http/Controllers/FoldersController.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Http\Request;
66
use Illuminate\Support\Facades\Gate;
77
use Illuminate\Support\Facades\URL;
8-
use Opcodes\LogViewer\Enums\FolderSortingMethod;
8+
use Opcodes\LogViewer\Enums\SortingMethod;
99
use Opcodes\LogViewer\Enums\SortingOrder;
1010
use Opcodes\LogViewer\Facades\LogViewer;
1111
use Opcodes\LogViewer\Http\Resources\LogFolderResource;
@@ -17,20 +17,20 @@ public function index(Request $request)
1717
{
1818
$folders = LogViewer::getFilesGroupedByFolder();
1919

20-
$sortingMethod = config('log-viewer.defaults.folder_sorting_method', FolderSortingMethod::ModifiedTime);
20+
$sortingMethod = config('log-viewer.defaults.folder_sorting_method', SortingMethod::ModifiedTime);
2121
$sortingOrder = config('log-viewer.defaults.folder_sorting_order', SortingOrder::Descending);
2222

23-
$fileSortingMethod = config('log-viewer.defaults.file_sorting_method', FolderSortingMethod::ModifiedTime);
24-
$fileSortingOrder = $request->query('direction', 'desc');
23+
$fileSortingMethod = config('log-viewer.defaults.file_sorting_method', SortingMethod::ModifiedTime);
24+
$fileSortingOrder = $this->validateDirection($request->query('direction'));
2525

26-
if ($sortingMethod === FolderSortingMethod::Alphabetical) {
26+
if ($sortingMethod === SortingMethod::Alphabetical) {
2727
if ($sortingOrder === SortingOrder::Ascending) {
2828
$folders = $folders->sortAlphabeticallyAsc();
2929
} else {
3030
$folders = $folders->sortAlphabeticallyDesc();
3131
}
3232
} else { // ModifiedTime
33-
if ($fileSortingOrder === 'asc') {
33+
if ($fileSortingOrder === SortingOrder::Ascending) {
3434
$folders = $folders->sortByEarliestFirstIncludingFiles();
3535
} else {
3636
$folders = $folders->sortByLatestFirstIncludingFiles();
@@ -39,15 +39,15 @@ public function index(Request $request)
3939

4040
// Sort files within folders after sorting folders
4141
$folders->each(function ($folder) use ($fileSortingMethod, $fileSortingOrder) {
42-
if ($fileSortingMethod === FolderSortingMethod::ModifiedTime) {
43-
if ($fileSortingOrder === 'asc') {
42+
if ($fileSortingMethod === SortingMethod::ModifiedTime) {
43+
if ($fileSortingOrder === SortingOrder::Ascending) {
4444
$folder->files()->sortByEarliestFirst();
4545
} else {
4646
$folder->files()->sortByLatestFirst();
4747
}
4848

4949
} else {
50-
if ($fileSortingOrder === 'asc') {
50+
if ($fileSortingOrder === SortingOrder::Ascending) {
5151
$folder->files()->sortAlphabeticallyAsc();
5252
} else {
5353
$folder->files()->sortAlphabeticallyDesc();
@@ -58,6 +58,15 @@ public function index(Request $request)
5858
return LogFolderResource::collection($folders->values());
5959
}
6060

61+
private function validateDirection(?string $direction): string
62+
{
63+
if ($direction === SortingOrder::Ascending) {
64+
return SortingOrder::Ascending;
65+
}
66+
67+
return SortingOrder::Descending;
68+
}
69+
6170
public function requestDownload(Request $request, string $folderIdentifier)
6271
{
6372
$folder = LogViewer::getFolder($folderIdentifier);

src/Http/Controllers/IndexController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Opcodes\LogViewer\Http\Controllers;
44

5-
use Opcodes\LogViewer\Enums\FolderSortingMethod;
5+
use Opcodes\LogViewer\Enums\SortingMethod;
66
use Opcodes\LogViewer\Facades\LogViewer;
77
use Opcodes\LogViewer\LogFolder;
88
use Opcodes\LogViewer\Utils\Utils;
@@ -15,7 +15,7 @@ public function __invoke()
1515
abort(404);
1616
}
1717

18-
$files_sort_by_time = config('log-viewer.defaults.file_sorting_method') === FolderSortingMethod::ModifiedTime;
18+
$files_sort_by_time = config('log-viewer.defaults.file_sorting_method') === SortingMethod::ModifiedTime;
1919

2020
return view(LogViewer::getViewLayout(), [
2121
'logViewerScriptVariables' => [
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
3+
use Opcodes\LogViewer\Enums\SortingMethod;
4+
use Opcodes\LogViewer\Enums\SortingOrder;
5+
6+
use function Pest\Laravel\getJson;
7+
8+
beforeEach(function () {
9+
config(['log-viewer.include_files' => ['*.log', '*/**.log']]);
10+
});
11+
12+
// FilesController with invalid sorting values
13+
14+
it('files controller returns default desc order when invalid direction is provided', function () {
15+
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]);
16+
$names = ['one.log', 'two.log', 'three.log'];
17+
generateLogFiles($names, randomContent: true);
18+
19+
array_map(function (string $name) {
20+
$this->travelTo(now()->addSecond());
21+
touch(storage_path('logs/'.$name), now()->timestamp);
22+
}, $names);
23+
24+
$response = getJson(route('log-viewer.files', ['direction' => 'invalid']));
25+
26+
expect(array_column($response->json(), 'name'))->toBe([
27+
'three.log',
28+
'two.log',
29+
'one.log',
30+
]);
31+
});
32+
33+
it('files controller returns default desc order when empty direction is provided', function () {
34+
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]);
35+
$names = ['one.log', 'two.log', 'three.log'];
36+
generateLogFiles($names, randomContent: true);
37+
38+
array_map(function (string $name) {
39+
$this->travelTo(now()->addSecond());
40+
touch(storage_path('logs/'.$name), now()->timestamp);
41+
}, $names);
42+
43+
$response = getJson(route('log-viewer.files', ['direction' => '']));
44+
45+
expect(array_column($response->json(), 'name'))->toBe([
46+
'three.log',
47+
'two.log',
48+
'one.log',
49+
]);
50+
});
51+
52+
it('files controller returns default desc order when direction is null', function () {
53+
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]);
54+
$names = ['one.log', 'two.log', 'three.log'];
55+
generateLogFiles($names, randomContent: true);
56+
57+
array_map(function (string $name) {
58+
$this->travelTo(now()->addSecond());
59+
touch(storage_path('logs/'.$name), now()->timestamp);
60+
}, $names);
61+
62+
$response = getJson(route('log-viewer.files'));
63+
64+
expect(array_column($response->json(), 'name'))->toBe([
65+
'three.log',
66+
'two.log',
67+
'one.log',
68+
]);
69+
});
70+
71+
it('files controller returns alphabetical sorting with default desc when invalid direction is provided', function () {
72+
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::Alphabetical]);
73+
generateLogFiles(['one.log', 'two.log', 'three.log', 'four.log'], randomContent: true);
74+
75+
$response = getJson(route('log-viewer.files', ['direction' => 'invalid']));
76+
77+
expect(array_column($response->json(), 'name'))->toBe([
78+
'two.log',
79+
'three.log',
80+
'one.log',
81+
'four.log',
82+
]);
83+
});
84+
85+
it('files controller returns correct order with valid asc direction and alphabetical sorting', function () {
86+
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::Alphabetical]);
87+
generateLogFiles(['one.log', 'two.log', 'three.log', 'four.log'], randomContent: true);
88+
89+
$response = getJson(route('log-viewer.files', ['direction' => SortingOrder::Ascending]));
90+
91+
expect(array_column($response->json(), 'name'))->toBe([
92+
'four.log',
93+
'one.log',
94+
'three.log',
95+
'two.log',
96+
]);
97+
});
98+
99+
// FoldersController with invalid sorting values
100+
101+
it('folders controller returns default desc order when invalid direction is provided', function () {
102+
config(['log-viewer.include_files' => ['*/**.log']]);
103+
config(['log-viewer.defaults.folder_sorting_method' => SortingMethod::Alphabetical]);
104+
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]);
105+
$names = ['sub/one.log', 'sub/two.log', 'sub/three.log'];
106+
generateLogFiles($names, randomContent: true);
107+
108+
array_map(function (string $name) {
109+
$this->travelTo(now()->addSecond());
110+
touch(storage_path('logs/'.$name), now()->timestamp);
111+
}, $names);
112+
113+
$response = getJson(route('log-viewer.folders', ['direction' => 'invalid']));
114+
115+
expect(array_column($response->json()[0]['files'], 'name'))->toBe([
116+
'three.log',
117+
'two.log',
118+
'one.log',
119+
]);
120+
});
121+
122+
it('folders controller returns default desc order when empty direction is provided', function () {
123+
config(['log-viewer.include_files' => ['*/**.log']]);
124+
config(['log-viewer.defaults.folder_sorting_method' => SortingMethod::Alphabetical]);
125+
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]);
126+
$names = ['sub/one.log', 'sub/two.log', 'sub/three.log'];
127+
generateLogFiles($names, randomContent: true);
128+
129+
array_map(function (string $name) {
130+
$this->travelTo(now()->addSecond());
131+
touch(storage_path('logs/'.$name), now()->timestamp);
132+
}, $names);
133+
134+
$response = getJson(route('log-viewer.folders', ['direction' => '']));
135+
136+
expect(array_column($response->json()[0]['files'], 'name'))->toBe([
137+
'three.log',
138+
'two.log',
139+
'one.log',
140+
]);
141+
});
142+
143+
it('folders controller returns default desc order when direction is null', function () {
144+
config(['log-viewer.include_files' => ['*/**.log']]);
145+
config(['log-viewer.defaults.folder_sorting_method' => SortingMethod::Alphabetical]);
146+
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]);
147+
$names = ['sub/one.log', 'sub/two.log', 'sub/three.log'];
148+
generateLogFiles($names, randomContent: true);
149+
150+
array_map(function (string $name) {
151+
$this->travelTo(now()->addSecond());
152+
touch(storage_path('logs/'.$name), now()->timestamp);
153+
}, $names);
154+
155+
$response = getJson(route('log-viewer.folders'));
156+
157+
expect(array_column($response->json()[0]['files'], 'name'))->toBe([
158+
'three.log',
159+
'two.log',
160+
'one.log',
161+
]);
162+
});
163+
164+
it('folders controller returns correct order with valid asc direction and alphabetical sorting', function () {
165+
config(['log-viewer.include_files' => ['*/**.log']]);
166+
config(['log-viewer.defaults.folder_sorting_method' => SortingMethod::Alphabetical]);
167+
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::Alphabetical]);
168+
generateLogFiles(['sub/one.log', 'sub/two.log', 'sub/three.log', 'sub/four.log'], randomContent: true);
169+
170+
$response = getJson(route('log-viewer.folders', ['direction' => SortingOrder::Ascending]));
171+
172+
expect(array_column($response->json()[0]['files'], 'name'))->toBe([
173+
'four.log',
174+
'one.log',
175+
'three.log',
176+
'two.log',
177+
]);
178+
});

0 commit comments

Comments
 (0)