Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print module refactor #673

Merged
merged 65 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from 64 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
676ac18
refactor
sszajbely Jan 3, 2024
d330b8b
remove printer role
sszajbely Jan 3, 2024
056fb9e
style: format code with PHP CS Fixer
deepsource-autofix[bot] Jan 3, 2024
e038e09
Merge remote-tracking branch 'origin/development' into print-module-r…
sszajbely Jan 3, 2024
3689f71
remove printer role
sszajbely Jan 3, 2024
74b27a2
Merge branch 'print-module-refactor' of github.com:EotvosCollegium/ma…
sszajbely Jan 3, 2024
9d74515
style: format code with PHP CS Fixer
deepsource-autofix[bot] Jan 3, 2024
1f3dbfb
solving todos
sszajbely Jan 3, 2024
c8f89bd
replace exec with process->run
sszajbely Jan 3, 2024
781ac18
style: format code with PHP CS Fixer
deepsource-autofix[bot] Jan 3, 2024
913a4a6
added missing doc strings
sszajbely Jan 3, 2024
6612ad8
Merge branch 'print-module-refactor' of github.com:EotvosCollegium/ma…
sszajbely Jan 3, 2024
7ea9be0
style: format code with PHP CS Fixer
deepsource-autofix[bot] Jan 3, 2024
1930767
add missing doc comments
sszajbely Jan 3, 2024
26c746f
Merge branch 'print-module-refactor' of github.com:EotvosCollegium/ma…
sszajbely Jan 3, 2024
ead482c
style: format code with PHP CS Fixer
deepsource-autofix[bot] Jan 3, 2024
31527fe
fix
sszajbely Jan 3, 2024
e6547cb
Merge branch 'print-module-refactor' of github.com:EotvosCollegium/ma…
sszajbely Jan 3, 2024
62252a9
add missing doc comment
sszajbely Jan 3, 2024
d8fe598
style: format code with PHP CS Fixer
deepsource-autofix[bot] Jan 3, 2024
80db86f
fixes and #344
sszajbely Jan 3, 2024
e669bc1
Merge branch 'print-module-refactor' of github.com:EotvosCollegium/ma…
sszajbely Jan 3, 2024
d6fbe58
Created Process class, created PrinterHelper class, code improvements…
sszajbely May 9, 2024
1b1685c
style: format code with PHP CS Fixer
deepsource-autofix[bot] May 9, 2024
9499653
Resolving comments
sszajbely Dec 23, 2024
d5da58b
style: format code with PHP CS Fixer
deepsource-autofix[bot] Dec 23, 2024
45a370c
Merge remote-tracking branch 'origin/development' into print-module-r…
sszajbely Dec 23, 2024
34946ae
Merge branch 'print-module-refactor' of github.com:EotvosCollegium/ma…
sszajbely Dec 23, 2024
67e6413
Minor fixes
sszajbely Dec 23, 2024
1455bb9
Added doc comments
sszajbely Dec 23, 2024
0fd4a0c
Fixed piping in commands.
sszajbely Dec 23, 2024
4acf0a8
Use commands config file
sszajbely Dec 23, 2024
f85af3d
Minor fixes.
sszajbely Dec 24, 2024
a42a282
style: format code with PHP CS Fixer
deepsource-autofix[bot] Dec 24, 2024
548981f
Fix for migration.
sszajbely Dec 24, 2024
f7d2837
Merge branch 'print-module-refactor' of github.com:EotvosCollegium/ma…
sszajbely Dec 24, 2024
d656944
Minor fixes
sszajbely Dec 24, 2024
840f339
style: format code with PHP CS Fixer
deepsource-autofix[bot] Dec 24, 2024
83c86fa
Fixed PrinterHelper namespace
sszajbely Dec 24, 2024
d93c271
Merge branch 'print-module-refactor' of github.com:EotvosCollegium/ma…
sszajbely Dec 24, 2024
7e5196c
Remove references for Models in migrations
horcsinbalint Dec 25, 2024
c261baa
Fix seeders
horcsinbalint Dec 25, 2024
344a4d6
Remove printer role using migration
horcsinbalint Dec 25, 2024
b5fa014
Fixes
horcsinbalint Dec 25, 2024
d79cd34
Added better check for pinging command
horcsinbalint Dec 25, 2024
b5dbb57
Added logging for the results of commands
horcsinbalint Dec 25, 2024
afd2f1f
Added prefix for printing
horcsinbalint Dec 25, 2024
7961a11
style: format code with PHP CS Fixer
deepsource-autofix[bot] Dec 29, 2024
02daade
Make .env.example more understandable
horcsinbalint Dec 29, 2024
c519378
Using proper filtering instead of regex for ip
horcsinbalint Dec 29, 2024
7238e64
Added more strict rate limiting
horcsinbalint Dec 29, 2024
9fac107
Fixing up routes
horcsinbalint Dec 29, 2024
a4f888c
Better emailing for e-mail balances
horcsinbalint Dec 29, 2024
bd6c4cb
Emailing all involved
horcsinbalint Dec 29, 2024
517157b
Better commenting
horcsinbalint Dec 29, 2024
ee9b175
Restore composer.lock
horcsinbalint Dec 29, 2024
78f252c
Bugfixes and better suit our needs
horcsinbalint Dec 29, 2024
fe6e80b
Command fix
horcsinbalint Dec 29, 2024
ea2f243
Command fix
horcsinbalint Dec 29, 2024
d1fb982
Fix statuses
horcsinbalint Dec 29, 2024
6d45feb
Adding missing translation
horcsinbalint Dec 29, 2024
40ee670
Fix tests
horcsinbalint Dec 29, 2024
12f5a28
Fix tests
horcsinbalint Dec 29, 2024
eecb8cb
Fix tests
horcsinbalint Dec 29, 2024
e83748b
Fix commands as the order of parameters matter
horcsinbalint Dec 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.ci
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=password
PRINTER_NAME=printer
7 changes: 5 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ PREVENT_ACCESSING_MISSING_ATTRIBUTES=false

PRINT_COST_ONESIDED=8
PRINT_COST_TWOSIDED=12

#Data used during migration in 2023_12_25_182310_create_printers_table.php to create a new printer
PRINTER_NAME=NemUjBela
PRINTER_ADDITIONAL_ARGS=
PRINTER_STAT_ADDITIONAL_ARGS=
PRINTER_IP=
PRINTER_PORT=

NETREG=1000
KKT=2000
Expand All @@ -88,6 +90,7 @@ CANCEL_COMMAND=cancel
PDFINFO_COMMAND=pdfinfo
PING_COMMAND=ping
PDFLATEX_COMMAND=/usr/bin/pdflatex
RUN_COMMANDS_IN_DEBUG_MODE=false

WORKSHOP_BALANCE_EXTERN=0.45
WORKSHOP_BALANCE_RESIDENT=0.6
Expand Down
80 changes: 10 additions & 70 deletions app/Console/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console;

use App\Utils\Process;
use Illuminate\Support\Facades\Log;

/**
Expand All @@ -10,84 +11,23 @@
*/
class Commands
{
private static function isDebugMode()
{
return config('app.debug');
}

public static function getCompletedPrintingJobs()
{
$command = config('commands.lpstat') . " " . config('print.stat_additional_args') . " -W completed -o " . config('print.printer_name') . " | awk '{print $1}'";
if (self::isDebugMode()) {
$result = [0];
} else {
$result = [];
exec($command, $result);
}
Log::info([$command, $result]);
return $result;
}

public static function print($command)
{
if (self::isDebugMode()) {
$job_id = 0;
$result = "request id is " . config('print.printer_name') . "-" . $job_id . " (1 file(s))";
} else {
$result = exec($command);
}
Log::info([$command, $result]);
return $result;
}

public static function cancelPrintJob(string $jobID)
{
$command = config('commands.cancel') . " " . $jobID;
if (self::isDebugMode()) {
// cancel(1) exits with status code 0 if it succeeds
$result = ['output' => '', 'exit_code' => 0];
} else {
$output = exec($command, $result, $exit_code);
$result = ['output' => $output, 'exit_code' => $exit_code];
}
Log::info([$command, $result]);
return $result;
}

public static function getPages($path)
{
$command = config('commands.pdfinfo') . " " . $path . " | grep '^Pages' | awk '{print $2}' 2>&1";
if (self::isDebugMode()) {
$result = rand(1, 10);
} else {
$result = exec($command);
}
Log::info([$command, $result]);
return $result;
}

public static function pingRouter($router)
{
if (self::isDebugMode()) {
$result = rand(1, 10) > 9 ? "error" : '';
} else {
// This happens too often to log.
$command = config('commands.ping') . " " . $router->ip . " -c 1 | grep 'error\|unreachable'";
$result = exec($command);
if (!filter_var($router->ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
throw new \InvalidArgumentException("Invalid IP address: " . $router->ip);
}

$process = Process::fromShellCommandline(config('commands.ping') . " $router->ip -c 1 | grep 'error\|unreachable'");
$process->run($log = false);

Check failure on line 21 in app/Console/Commands.php

View workflow job for this annotation

GitHub Actions / phpunit

Parameter #1 $callback of method App\Utils\Process::run() expects (callable(): mixed)|null, false given.

Check failure on line 21 in app/Console/Commands.php

View workflow job for this annotation

GitHub Actions / phpunit

Parameter #1 $callback of method App\Utils\Process::run() expects (callable(): mixed)|null, false given.

Check failure on line 21 in app/Console/Commands.php

View workflow job for this annotation

GitHub Actions / phpunit

Parameter #1 $callback of method App\Utils\Process::run() expects (callable(): mixed)|null, false given.

Check failure on line 21 in app/Console/Commands.php

View workflow job for this annotation

GitHub Actions / phpunit

Parameter #1 $callback of method App\Utils\Process::run() expects (callable(): mixed)|null, false given.
$result = $process->getOutput(debugOutput: rand(1, 10) > 9 ? "error" : '');
Comment on lines +20 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Mismatch in parameter type for $process->run()
Kindly adjust your usage of $process->run($log = false);. The method expects the first argument to be a callable or null, rather than a boolean.

Please consider the following adjustment:

- $process->run($log = false);
+ $process->run(callback: null, env: [], log: false);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$process = Process::fromShellCommandline(config('commands.ping') . " $router->ip -c 1 | grep 'error\|unreachable'");
$process->run($log = false);
$result = $process->getOutput(debugOutput: rand(1, 10) > 9 ? "error" : '');
$process = Process::fromShellCommandline(config('commands.ping') . " $router->ip -c 1 | grep 'error\|unreachable'");
$process->run(callback: null, env: [], log: false);
$result = $process->getOutput(debugOutput: rand(1, 10) > 9 ? "error" : '');
🧰 Tools
🪛 phpstan (2.0.3)

21-21: Parameter #1 $callback of method App\Utils\Process::run() expects (callable(): mixed)|null, false given.

(argument.type)

🪛 GitHub Check: phpunit

[failure] 21-21:
Parameter #1 $callback of method App\Utils\Process::run() expects (callable(): mixed)|null, false given.

return $result;
}

public static function latexToPdf($path, $outputDir)
{
if (self::isDebugMode()) {
$result = "ok";
} else {
$command = config('commands.pdflatex') . " " . "-interaction=nonstopmode -output-dir " . $outputDir . " " . $path . " 2>&1";
Log::info($command);
$result = exec($command);
Log::info($result);
}
$process = new Process([config('commands.pdflatex'), '-interaction=nonstopmode', '-output-dir', $outputDir, $path]);
$process->run();
$result = $process->getOutput(debugOutput: 'ok');
return $result;
}
}
11 changes: 11 additions & 0 deletions app/Enums/PrintJobStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Enums;

enum PrintJobStatus: string {
case QUEUED = 'QUEUED';
//case ERROR = 'ERROR'; //There are some older print jobs that have this status, but it is not used anymore
case CANCELLED = 'CANCELLED';
case SUCCESS = 'SUCCESS';
// case REIMBURSED = 'REIMBURSED';
}
10 changes: 10 additions & 0 deletions app/Enums/PrinterCancelResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Enums;

enum PrinterCancelResult: string {
case AlreadyCancelled = "already-cancelled";
case AlreadyCompleted = "already-completed";
case CannotCancel = "cannot-cancel";
case Success = "successfully-cancelled";
}
2 changes: 0 additions & 2 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ public function create(array $data)
'password' => Hash::make($data['password']),
]);

$user->roles()->attach(Role::get(Role::PRINTER)->id);

if ($data['user_type'] == Role::TENANT) {
$user->roles()->attach(Role::get(Role::TENANT));
$user->personalInformation()->create([
Expand Down
Loading
Loading