-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from cash-track/feature/profile-statistics
Feature / Profile Statistics
- Loading branch information
Showing
13 changed files
with
739 additions
and
337 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Controller; | ||
|
||
use App\Database\User; | ||
use Spiral\Auth\AuthScope; | ||
|
||
abstract class AuthAwareController | ||
{ | ||
/** | ||
* @var \App\Database\User | ||
*/ | ||
protected User $user; | ||
|
||
/** | ||
* ProfileStatisticsController constructor. | ||
* | ||
* @param \Spiral\Auth\AuthScope $auth | ||
*/ | ||
public function __construct(AuthScope $auth) | ||
{ | ||
$user = $auth->getActor(); | ||
|
||
if (! $user instanceof User) { | ||
throw new \RuntimeException('Unable to get authenticated user'); | ||
} | ||
|
||
$this->user = $user; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
app/src/Controller/Profile/ProfileStatisticsController.php
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controller\Profile; | ||
|
||
use App\Controller\AuthAwareController; | ||
use App\Database\Currency; | ||
use App\Repository\ChargeRepository; | ||
use App\Repository\CurrencyRepository; | ||
use App\Repository\WalletRepository; | ||
use App\Service\Statistics\ProfileStatistics; | ||
use App\View\CurrencyView; | ||
use App\View\WalletsView; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Spiral\Http\ResponseWrapper; | ||
use Spiral\Router\Annotation\Route; | ||
|
||
class ProfileStatisticsController extends AuthAwareController | ||
{ | ||
/** | ||
* @Route(route="/profile/statistics/charges-flow", name="profile.statistics.charges-flow", methods="GET", group="auth") | ||
* | ||
* @param \Spiral\Http\ResponseWrapper $response | ||
* @param \App\Repository\CurrencyRepository $currencyRepository | ||
* @param \App\View\CurrencyView $currencyView | ||
* @param \App\Service\Statistics\ProfileStatistics $statistics | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function chargesFlow( | ||
ResponseWrapper $response, | ||
CurrencyRepository $currencyRepository, | ||
CurrencyView $currencyView, | ||
ProfileStatistics $statistics, | ||
): ResponseInterface { | ||
$currency = $currencyRepository->findByPK($this->user->defaultCurrencyCode ?? Currency::DEFAULT_CURRENCY_CODE); | ||
|
||
if (! $currency instanceof Currency) { | ||
return $response->json([ | ||
'message' => 'Unable to find currency.', | ||
], 400); | ||
} | ||
|
||
$data = $statistics->getChargeFlow($this->user, $currency); | ||
$data['currency'] = $currencyView->map($currency); | ||
|
||
return $response->json([ | ||
'data' => $data, | ||
]); | ||
} | ||
|
||
/** | ||
* @Route(route="/profile/statistics/counters", name="profile.statistics.counters", methods="GET", group="auth") | ||
* | ||
* @param \Spiral\Http\ResponseWrapper $response | ||
* @param \App\Service\Statistics\ProfileStatistics $statistics | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function counters(ResponseWrapper $response, ProfileStatistics $statistics): ResponseInterface | ||
{ | ||
return $response->json([ | ||
'data' => $statistics->getCounters($this->user), | ||
]); | ||
} | ||
|
||
/** | ||
* @Route(route="/profile/wallets/latest", name="profile.wallets.latest", methods="GET", group="auth") | ||
* | ||
* @param \App\Repository\WalletRepository $walletRepository | ||
* @param \App\Repository\ChargeRepository $chargeRepository | ||
* @param \App\View\WalletsView $view | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function walletsLatest( | ||
WalletRepository $walletRepository, | ||
ChargeRepository $chargeRepository, | ||
WalletsView $view | ||
): ResponseInterface { | ||
$wallets = $walletRepository->findAllUnArchivedByUserPKWithLimit($this->user->id); | ||
|
||
foreach ($wallets as $wallet) { | ||
$wallet->latestCharges = new ArrayCollection($chargeRepository->findByWalletIDLatest($wallet->id)); | ||
} | ||
|
||
return $view->json($wallets); | ||
} | ||
} |
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
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
Oops, something went wrong.