2017-11-05 01:26:52 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\ViewComposers;
|
|
|
|
|
|
|
|
use App\Ejaculation;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\View\View;
|
|
|
|
|
2019-02-07 00:52:03 +09:00
|
|
|
class ProfileStatsComposer
|
2017-11-05 01:26:52 +09:00
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function compose(View $view)
|
|
|
|
{
|
|
|
|
// user変数に値が設定されてない場合は落とす
|
|
|
|
if (!$view->offsetExists('user')) {
|
|
|
|
throw new \LogicException('View data "user" was not exist.');
|
|
|
|
}
|
2020-11-08 23:41:51 +09:00
|
|
|
/** @var \App\User $user */
|
2017-11-05 01:26:52 +09:00
|
|
|
$user = $view->offsetGet('user');
|
|
|
|
|
|
|
|
// 現在のオナ禁セッションの経過時間
|
|
|
|
$latestEjaculation = Ejaculation::select('ejaculated_date')
|
|
|
|
->where('user_id', $user->id)
|
|
|
|
->orderByDesc('ejaculated_date')
|
|
|
|
->first();
|
|
|
|
if (!empty($latestEjaculation)) {
|
|
|
|
$currentSession = $latestEjaculation->ejaculated_date
|
|
|
|
->diff(Carbon::now())
|
|
|
|
->format('%a日 %h時間 %i分');
|
|
|
|
} else {
|
|
|
|
$currentSession = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 概況欄のデータ取得
|
2020-11-08 23:41:51 +09:00
|
|
|
$average = 0;
|
|
|
|
$divisor = 0;
|
|
|
|
$averageSources = DB::select(<<<'SQL'
|
2019-06-12 00:03:18 +09:00
|
|
|
SELECT
|
2020-11-08 23:41:51 +09:00
|
|
|
extract(epoch from ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)) AS span,
|
|
|
|
discard_elapsed_time
|
2019-06-12 00:03:18 +09:00
|
|
|
FROM
|
2020-11-08 23:41:51 +09:00
|
|
|
ejaculations
|
|
|
|
WHERE
|
|
|
|
user_id = :user_id
|
|
|
|
ORDER BY
|
|
|
|
ejaculated_date DESC
|
|
|
|
LIMIT
|
|
|
|
30
|
2019-06-12 00:03:18 +09:00
|
|
|
SQL
|
|
|
|
, ['user_id' => $user->id]);
|
2020-11-08 23:41:51 +09:00
|
|
|
foreach ($averageSources as $item) {
|
|
|
|
// 経過時間記録対象外のレコードがあったら、それより古いデータは平均の計算に加えない
|
|
|
|
if ($item->discard_elapsed_time) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$average += $item->span;
|
|
|
|
$divisor++;
|
|
|
|
}
|
|
|
|
if ($divisor > 0) {
|
|
|
|
$average /= $divisor;
|
|
|
|
}
|
2019-06-12 00:03:18 +09:00
|
|
|
|
2017-11-05 01:26:52 +09:00
|
|
|
$summary = DB::select(<<<'SQL'
|
|
|
|
SELECT
|
|
|
|
max(span) AS longest,
|
|
|
|
min(span) AS shortest,
|
2020-11-08 23:41:51 +09:00
|
|
|
sum(span) AS total_times
|
2017-11-05 01:26:52 +09:00
|
|
|
FROM
|
|
|
|
(
|
|
|
|
SELECT
|
2020-11-08 23:41:51 +09:00
|
|
|
extract(epoch from ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)) AS span,
|
|
|
|
discard_elapsed_time
|
2017-11-05 01:26:52 +09:00
|
|
|
FROM
|
|
|
|
ejaculations
|
|
|
|
WHERE
|
|
|
|
user_id = :user_id
|
|
|
|
ORDER BY
|
|
|
|
ejaculated_date DESC
|
|
|
|
) AS temp
|
2020-11-08 23:41:51 +09:00
|
|
|
WHERE
|
|
|
|
discard_elapsed_time = FALSE
|
2017-11-05 01:26:52 +09:00
|
|
|
SQL
|
|
|
|
, ['user_id' => $user->id]);
|
|
|
|
|
2020-11-08 23:41:51 +09:00
|
|
|
$total = $user->ejaculations()->count();
|
|
|
|
|
|
|
|
$view->with(compact('latestEjaculation', 'currentSession', 'average', 'summary', 'total'));
|
2017-11-05 01:26:52 +09:00
|
|
|
}
|
2019-01-15 00:05:01 +09:00
|
|
|
}
|