chore: code cleanup.

This commit is contained in:
James Cole
2023-05-29 13:56:55 +02:00
parent 7f7644c92f
commit 1b52147a05
295 changed files with 12418 additions and 12324 deletions

View File

@@ -47,8 +47,8 @@ use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use JsonException;
use Illuminate\Support\Facades\Log;
use JsonException;
/**
* Class RecurringRepository
@@ -92,21 +92,6 @@ class RecurringRepository implements RecurringRepositoryInterface
return false;
}
/**
* Returns all of the user's recurring transactions.
*
* @return Collection
*/
public function get(): Collection
{
return $this->user->recurrences()
->with(['TransactionCurrency', 'TransactionType', 'RecurrenceRepetitions', 'RecurrenceTransactions'])
->orderBy('active', 'DESC')
->orderBy('transaction_type_id', 'ASC')
->orderBy('title', 'ASC')
->get();
}
/**
* Destroy a recurring transaction.
*
@@ -127,6 +112,21 @@ class RecurringRepository implements RecurringRepositoryInterface
$this->user->recurrences()->delete();
}
/**
* Returns all of the user's recurring transactions.
*
* @return Collection
*/
public function get(): Collection
{
return $this->user->recurrences()
->with(['TransactionCurrency', 'TransactionType', 'RecurrenceRepetitions', 'RecurrenceTransactions'])
->orderBy('active', 'DESC')
->orderBy('transaction_type_id', 'ASC')
->orderBy('title', 'ASC')
->get();
}
/**
* Get ALL recurring transactions.
*
@@ -276,6 +276,45 @@ class RecurringRepository implements RecurringRepositoryInterface
return '';
}
/**
* Generate events in the date range.
*
* @param RecurrenceRepetition $repetition
* @param Carbon $start
* @param Carbon $end
*
* @return array
*
*/
public function getOccurrencesInRange(RecurrenceRepetition $repetition, Carbon $start, Carbon $end): array
{
$occurrences = [];
$mutator = clone $start;
$mutator->startOfDay();
$skipMod = $repetition->repetition_skip + 1;
Log::debug(sprintf('Calculating occurrences for rep type "%s"', $repetition->repetition_type));
Log::debug(sprintf('Mutator is now: %s', $mutator->format('Y-m-d')));
if ('daily' === $repetition->repetition_type) {
$occurrences = $this->getDailyInRange($mutator, $end, $skipMod);
}
if ('weekly' === $repetition->repetition_type) {
$occurrences = $this->getWeeklyInRange($mutator, $end, $skipMod, $repetition->repetition_moment);
}
if ('monthly' === $repetition->repetition_type) {
$occurrences = $this->getMonthlyInRange($mutator, $end, $skipMod, $repetition->repetition_moment);
}
if ('ndom' === $repetition->repetition_type) {
$occurrences = $this->getNdomInRange($mutator, $end, $skipMod, $repetition->repetition_moment);
}
if ('yearly' === $repetition->repetition_type) {
$occurrences = $this->getYearlyInRange($mutator, $end, $skipMod, $repetition->repetition_moment);
}
// filter out all the weekend days:
return $this->filterWeekends($repetition, $occurrences);
}
/**
* @param RecurrenceTransaction $transaction
*
@@ -345,16 +384,6 @@ class RecurringRepository implements RecurringRepositoryInterface
return $collector->getPaginatedGroups();
}
/**
* @param User|Authenticatable|null $user
*/
public function setUser(User|Authenticatable|null $user): void
{
if (null !== $user) {
$this->user = $user;
}
}
/**
* @param Recurrence $recurrence
*
@@ -465,27 +494,6 @@ class RecurringRepository implements RecurringRepositoryInterface
return $this->filterMaxDate($repeatUntil, $occurrences);
}
/**
* @param Carbon|null $max
* @param array $occurrences
*
* @return array
*/
private function filterMaxDate(?Carbon $max, array $occurrences): array
{
if (null === $max) {
return $occurrences;
}
$filtered = [];
foreach ($occurrences as $date) {
if ($date->lte($max)) {
$filtered[] = $date;
}
}
return $filtered;
}
/**
* Parse the repetition in a string that is user readable.
*
@@ -562,6 +570,16 @@ class RecurringRepository implements RecurringRepositoryInterface
return $search->take($limit)->get(['id', 'title', 'description']);
}
/**
* @param User|Authenticatable|null $user
*/
public function setUser(User|Authenticatable|null $user): void
{
if (null !== $user) {
$this->user = $user;
}
}
/**
* @param array $data
*
@@ -605,45 +623,6 @@ class RecurringRepository implements RecurringRepositoryInterface
return 0;
}
/**
* Generate events in the date range.
*
* @param RecurrenceRepetition $repetition
* @param Carbon $start
* @param Carbon $end
*
* @return array
*
*/
public function getOccurrencesInRange(RecurrenceRepetition $repetition, Carbon $start, Carbon $end): array
{
$occurrences = [];
$mutator = clone $start;
$mutator->startOfDay();
$skipMod = $repetition->repetition_skip + 1;
Log::debug(sprintf('Calculating occurrences for rep type "%s"', $repetition->repetition_type));
Log::debug(sprintf('Mutator is now: %s', $mutator->format('Y-m-d')));
if ('daily' === $repetition->repetition_type) {
$occurrences = $this->getDailyInRange($mutator, $end, $skipMod);
}
if ('weekly' === $repetition->repetition_type) {
$occurrences = $this->getWeeklyInRange($mutator, $end, $skipMod, $repetition->repetition_moment);
}
if ('monthly' === $repetition->repetition_type) {
$occurrences = $this->getMonthlyInRange($mutator, $end, $skipMod, $repetition->repetition_moment);
}
if ('ndom' === $repetition->repetition_type) {
$occurrences = $this->getNdomInRange($mutator, $end, $skipMod, $repetition->repetition_moment);
}
if ('yearly' === $repetition->repetition_type) {
$occurrences = $this->getYearlyInRange($mutator, $end, $skipMod, $repetition->repetition_moment);
}
// filter out all the weekend days:
return $this->filterWeekends($repetition, $occurrences);
}
/**
* Update a recurring transaction.
*
@@ -660,4 +639,25 @@ class RecurringRepository implements RecurringRepositoryInterface
return $service->update($recurrence, $data);
}
/**
* @param Carbon|null $max
* @param array $occurrences
*
* @return array
*/
private function filterMaxDate(?Carbon $max, array $occurrences): array
{
if (null === $max) {
return $occurrences;
}
$filtered = [];
foreach ($occurrences as $date) {
if ($date->lte($max)) {
$filtered[] = $date;
}
}
return $filtered;
}
}