Created
May 25, 2018 17:07
-
-
Save Medalink/2a2776960999bd46761d36d0b1356436 to your computer and use it in GitHub Desktop.
Code Challenge: What improvements could you make to this controller method to reduce code duplication? Are there any additional laravel functions/method that could replace raw/manual PHP code?
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
<?php | |
namespace App\Http\Controllers; | |
use App\Actblue; | |
use App\Client; | |
use App\Packages\ReportHelper; | |
use DB; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Carbon; | |
use Illuminate\Support\Facades\Cache; | |
class ClientController extends Controller | |
{ | |
/** | |
* @param Request $request | |
* @param string $id | |
* @param null|string $date | |
* | |
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View | |
*/ | |
public function show(Request $request, $id, $date = null) | |
{ | |
// Cache reports for an hour | |
$cacheFor = Carbon::now()->addHour(); | |
// Setup defaults and handle params into carbon objects | |
$firstDayOfThisQuarter = Carbon::now()->firstOfQuarter(); | |
$donationStartDate = Carbon::parse($request->get('donationStartDate', $firstDayOfThisQuarter)); | |
$donationEndDate = Carbon::parse($request->get('donationEndDate', Carbon::now()->toDateString())); | |
$startDate = $donationStartDate->toDateString(); | |
// Example: | |
// donationEndDate = 2017-03-31 | |
// corresponds to 2017-03-31 00:00:00 UTC, but we also want results from that day | |
// add 1 day gets us to 2017-04-01 00:00:00 UTC | |
$endDate = (clone $donationEndDate)->addDay()->toDateString(); | |
$client = Client::with([ | |
'petitions', | |
'sharedPetitions', | |
'petitions.client', | |
'sharedPetitions.client', | |
])->whereId($id)->first(); | |
if ($client === null) { | |
return redirect('client.index'); | |
} | |
// Holders | |
$clientId = $client->id; | |
$donations = Actblue::select(DB::raw('sum(amount) as revenue, count(distinct email) as donors, date(created_at) date_donated'))->whereClientId($id)->groupBy('date_donated')->orderBy('date_donated', | |
'desc')->limit(30)->get(); | |
$program_summary = ReportHelper::program($client); | |
$cohort = ReportHelper::cohort($client); | |
$monthly = ReportHelper::monthly($client, $date); | |
// Cache all the things per client per date range | |
$cacheKey = 'donation.sums.by.times.donated.' . $clientId . '.' . $startDate . '.' . $endDate; | |
$donation_sums_by_times_donated = Cache::remember($cacheKey, $cacheFor, | |
function () use ($clientId, $startDate, $endDate) { | |
return Actblue::donationSumsByTimesDonated($clientId, $startDate, $endDate); | |
}); | |
$cacheKey = 'donation.sums.by.refcode.' . $clientId . '.' . $startDate . '.' . $endDate; | |
$donation_sums_by_refcode = Cache::remember($cacheKey, $cacheFor, | |
function () use ($clientId, $startDate, $endDate) { | |
return Actblue::donationSumsByRefcode($clientId, $startDate, $endDate); | |
}); | |
$cacheKey = 'donation.sums.by.contribution.form.' . $clientId . '.' . $startDate . '.' . $endDate; | |
$donation_sums_by_contribution_form = Cache::remember($cacheKey, $cacheFor, | |
function () use ($clientId, $startDate, $endDate) { | |
return Actblue::donationSumsByContributionForm($clientId, $startDate, $endDate); | |
}); | |
$cacheKey = 'donation.amounts.' . $clientId . '.' . $startDate . '.' . $endDate; | |
$donation_amounts = Cache::remember($cacheKey, $cacheFor, function () use ($clientId, $startDate, $endDate) { | |
return Actblue::donationAmounts($clientId, $startDate, $endDate); | |
}); | |
$cacheKey = 'donation.totals.by.date.' . $clientId . '.' . $startDate . '.' . $endDate; | |
$donation_totals_by_date = Cache::remember($cacheKey, $cacheFor, | |
function () use ($clientId, $startDate, $endDate) { | |
return Actblue::donationTotalsByDate($clientId, $startDate, $endDate); | |
}); | |
$cacheKey = 'donations.totals.per.person.by.date.' . $clientId . '.' . $startDate . '.' . $endDate; | |
$donations_totals_per_person_by_date = Cache::remember($cacheKey, $cacheFor, | |
function () use ($clientId, $startDate, $endDate) { | |
return Actblue::donationsTotalsPerPersonByDate($clientId, $startDate, $endDate); | |
}); | |
$total_donations = 0; | |
foreach ($donation_sums_by_times_donated as $donation_sum) { | |
$total_donations += $donation_sum->total; | |
} | |
return view('client.show', [ | |
'client' => $client, | |
'programsummary' => $program_summary, | |
'cohort' => $cohort, | |
'monthly' => $monthly, | |
'date' => $date, | |
'donationStartDate' => $donationStartDate->toDateString(), | |
'donationEndDate' => $donationEndDate->toDateString(), | |
'donations' => $donations, | |
'donation_sums_by_times_donated' => $donation_sums_by_times_donated, | |
'donation_amounts' => $donation_amounts, | |
'donation_totals_by_date' => $donation_totals_by_date, | |
'donation_totals_per_person_by_date' => $donations_totals_per_person_by_date, | |
'donation_sums_by_refcode' => $donation_sums_by_refcode, | |
'donation_sums_by_contribution_form' => $donation_sums_by_contribution_form, | |
'total_donations' => $total_donations, | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment