-
Notifications
You must be signed in to change notification settings - Fork 15
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
Remove the ability to mix free and paid pages #658
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ class Printer | |
private $number_of_copies; | ||
private $use_free_pages; | ||
private $print_account; | ||
private $free_page_pool = []; | ||
private $free_page_update = []; | ||
private $cost = 0; | ||
|
||
public function __construct($filename, $path, $use_free_pages = false, $is_two_sided = true, $number_of_copies = 1) | ||
|
@@ -41,49 +41,52 @@ public function print() | |
|
||
// If using free pages, check the amount that can be used | ||
if ($this->use_free_pages) { | ||
$this->calculateFreePagePool(); | ||
} | ||
|
||
// Calculate cost | ||
$this->cost = PrintAccount::getCost($this->pages, $this->is_two_sided, $this->number_of_copies); | ||
if (!$this->planFreePageUsage()) { | ||
return back()->withInput()->with('error', __('print.no_balance')); | ||
} | ||
|
||
// Check balance | ||
if (!$this->print_account->hasEnoughMoney($this->cost)) { | ||
return back()->withInput()->with('error', __('print.no_balance')); | ||
} | ||
// Print document | ||
return $this->printDocument(); | ||
} else { | ||
|
||
// Calculate cost | ||
$this->cost = PrintAccount::getCost($this->pages, $this->is_two_sided, $this->number_of_copies); | ||
|
||
// Check balance | ||
if (!$this->print_account->hasEnoughMoney($this->cost)) { | ||
return back()->withInput()->with('error', __('print.no_balance')); | ||
} | ||
|
||
// Print document | ||
return $this->printDocument(); | ||
// Print document | ||
return $this->printDocument(); | ||
} | ||
Comment on lines
+45
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve error messaging and transaction safety Whilst the logic flow is clearer, two concerns need addressing:
Consider this implementation: if ($this->use_free_pages) {
- if (!$this->planFreePageUsage()) {
- return back()->withInput()->with('error', __('print.no_balance'));
+ if (!$this->planFreePageUsage()) {
+ return back()->withInput()->with('error', __('print.no_mixed_payment_allowed'));
}
} else {
// Calculate cost
$this->cost = PrintAccount::getCost($this->pages, $this->is_two_sided, $this->number_of_copies);
- // Check balance
- if (!$this->print_account->hasEnoughMoney($this->cost)) {
- return back()->withInput()->with('error', __('print.no_balance'));
- }
+ // Wrap in transaction to prevent race conditions
+ return DB::transaction(function () {
+ $account = PrintAccount::lockForUpdate()->find($this->print_account->id);
+ if (!$account->hasEnoughMoney($this->cost)) {
+ return back()->withInput()->with('error', __('print.no_balance'));
+ }
+ return $this->printDocument();
+ });
}
|
||
} | ||
|
||
/** | ||
* Only calculating the values here to see how many pages can be covered free of charge. | ||
*/ | ||
private function calculateFreePagePool() | ||
private function planFreePageUsage() | ||
{ | ||
$this->free_page_pool = []; | ||
$available_pages = 0; | ||
$requested_pages = $this->number_of_copies * $this->pages; | ||
$this->free_page_update = []; | ||
$deducted_pages = 0; | ||
$all_pages = user()->freePages | ||
->where('deadline', '>', Carbon::now()) | ||
->sortBy('deadline'); | ||
|
||
foreach ($all_pages as $key => $free_page) { | ||
if ($available_pages + $free_page->amount >= $this->pages) { | ||
$this->free_page_pool[] = [ | ||
'page' => $free_page, | ||
'new_amount' => $free_page->amount - ($this->pages - $available_pages) | ||
]; | ||
$available_pages = $this->pages; | ||
break; | ||
} | ||
$this->free_page_pool[] = [ | ||
$deduce_from_current = min($requested_pages - $deducted_pages, $free_page->amount); | ||
$this->free_page_update[] = [ | ||
'page' => $free_page, | ||
'new_amount' => 0 | ||
'new_amount' => $free_page->amount - $deduce_from_current | ||
]; | ||
$available_pages += $free_page->amount; | ||
$deducted_pages += $deduce_from_current; | ||
if($deducted_pages == $requested_pages) { | ||
break; | ||
} | ||
} | ||
|
||
$this->pages -= $available_pages; | ||
return $deducted_pages == $requested_pages; | ||
} | ||
|
||
private function printDocument() | ||
|
@@ -95,7 +98,7 @@ private function printDocument() | |
|
||
// Update print account history | ||
$this->print_account->update(['last_modified_by' => user()->id]); | ||
foreach ($this->free_page_pool as $fp) { | ||
foreach ($this->free_page_update as $fp) { | ||
$fp['page']->update([ | ||
'amount' => $fp['new_amount'], | ||
'last_modified_by' => user()->id | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard against potential race conditions in balance checks.
There's a risk of race conditions between checking the balance and actually deducting the cost. If a user initiates multiple print jobs simultaneously, they could potentially exceed their balance.
Consider implementing a database transaction or optimistic locking: