Add PHP_CodeSniffer, lint code

This commit is contained in:
Ben Goldsworthy 2022-11-30 12:47:09 -06:00
parent 172131e1e6
commit ddf235a61b
8 changed files with 188 additions and 68 deletions

View file

@ -18,7 +18,8 @@ class TrackerController extends Controller
*
* @return \Illuminate\View\View
*/
public function show_past_trips_list() {
public function show_past_trips_list()
{
return view('past-trips');
}
@ -29,9 +30,12 @@ class TrackerController extends Controller
* @param string? $tripId
* @return \Illuminate\View\View
*/
public function show_trip(Request $request, string $tripId = null) {
public function show_trip(Request $request, string $tripId = null)
{
$tripId = $tripId ?? config('app.current_trip_id');
if (!$tripId) return view('no-trip');
if (!$tripId) {
return view('no-trip');
}
$viewMode = $request->input('show', null);
$fromCheckin = $request->input('from', null);
@ -42,28 +46,28 @@ class TrackerController extends Controller
if ($fromCheckin) {
$tripData->checkins = array_filter(
$tripData->checkins,
function($key) use ($fromCheckin) {
return $key >= $fromCheckin;
},
$tripData->checkins,
function ($key) use ($fromCheckin) {
return $key >= $fromCheckin;
},
ARRAY_FILTER_USE_KEY
);
}
if ($toCheckin) {
$tripData->checkins = array_filter(
$tripData->checkins,
function($key) use ($toCheckin) {
return $key <= $toCheckin;
},
$tripData->checkins,
function ($key) use ($toCheckin) {
return $key <= $toCheckin;
},
ARRAY_FILTER_USE_KEY
);
}
return view(
'tracker',
'tracker',
[
'trip' => $tripData,
'trip' => $tripData,
'showAllCheckins' => ($viewMode === 'all'),
'fromCheckin' => $fromCheckin,
'toCheckin' => $toCheckin
@ -80,8 +84,8 @@ class TrackerController extends Controller
* 1. If the trip is inactive (i.e., finished), use the cache
* 2. If the trip is active:
* 1. If the trip was actively tracking at last check, use the
* cache if it's less than an hour old. Otherwise, download
* the new trip data.
* cache if it's less than an hour old. Otherwise, download
* the new trip data.
* 2. If the trip was not actively tracking at lest check, use
* the cache if it's less than three hours old. Otherwise,
* download the new trip data.
@ -91,7 +95,8 @@ class TrackerController extends Controller
* @param bool $forceDownload
* @return string
*/
public function get_trip_data(string $tripId, bool $forceDownload = false) {
public function get_trip_data(string $tripId, bool $forceDownload = false)
{
$tripFileName = ( $tripId ?? config('app.current_trip_id') ) . '.json';
// Returns the cached trip data if the trip is inactive (i.e., finished)
@ -110,11 +115,21 @@ class TrackerController extends Controller
$cachedDataAge = intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600);
if ($cachedDataAge <= $cachingTimeout) {
Log::debug("Cached trip file '{$tripFileName}' is younger than {$cachingTimeout} hours, showing from cache...");
Log::debug(
"Cached trip file '" .
$tripFileName .
"' is younger than " .
$cachingTimeout .
" hours, showing from cache..."
);
return $cachedData;
}
} else {
Log::debug("Cached trip file '{$tripFileName}' is for an old trip, showing from cache...");
Log::debug(
"Cached trip file '" .
$tripFileName .
"' is for an old trip, showing from cache..."
);
return $cachedData;
}
}
@ -133,29 +148,37 @@ class TrackerController extends Controller
$response = $client->get('trip/'.($tripId ?? config('app.current_trip_id')).'/user/zmld8ko6qy7d9j3xvq10/json');
switch ($response->getStatusCode()) {
case 200:
$data = json_decode($response->getBody());
case 200:
$data = json_decode($response->getBody());
// Cache the downloaded file if it does not exist locally.
if (Storage::disk('local')->missing($tripFileName)) {
Log::debug("Caching new trip file '{$tripFileName}'.");
Storage::disk('local')->put($tripFileName, json_encode($data));
} else {
$cachedData = json_decode(Storage::disk('local')->get($tripFileName));
if ($data->trip->updated_at !== $cachedData->trip->updated_at) {
Log::debug("Cached trip file '{$tripFileName}' has different 'updated_at' time, updating cache...");
Log::debug("Caching new trip file '{$tripFileName}'.");
Storage::disk('local')->put($tripFileName, json_encode($data));
// TODO: Cache photos locally
} else {
Log::debug("Cached trip file '{$tripFileName}' has same 'updated_at' time, showing from cache...");
$data = $cachedData;
$cachedData = json_decode(Storage::disk('local')->get($tripFileName));
if ($data->trip->updated_at !== $cachedData->trip->updated_at) {
Log::debug(
"Cached trip file '" .
$tripFileName .
"' has different 'updated_at' time, updating cache..."
);
Storage::disk('local')->put($tripFileName, json_encode($data));
// TODO: Cache photos locally
} else {
Log::debug(
"Cached trip file '" .
$tripFileName .
"' has same 'updated_at' time, showing from cache..."
);
$data = $cachedData;
}
}
}
return $data->trip;
default:
// TODO: Add proper error handling.
return "Something went wrong";
return $data->trip;
default:
// TODO: Add proper error handling.
return "Something went wrong";
}
}
}