Cleanup Utils #467 (#547)

* Remove Utils Facade
This commit is contained in:
Nabeel S
2020-02-11 12:32:41 -05:00
committed by GitHub
parent be6c164f03
commit b36a3009dd
27 changed files with 281 additions and 289 deletions

View File

@@ -3,7 +3,7 @@
namespace App\Console\Commands;
use App\Contracts\Command;
use App\Facades\Utils;
use App\Support\Units\Time;
use GuzzleHttp\Client;
use Illuminate\Database\Eloquent\Collection;
@@ -64,7 +64,7 @@ class AcarsReplay extends Command
protected function startPirep($flight): string
{
// convert the planned flight time to be completely in minutes
$pft = Utils::hoursToMinutes(
$pft = Time::hoursToMinutes(
$flight->planned_hrsenroute,
$flight->planned_minenroute
);

View File

@@ -2,10 +2,10 @@
namespace App\Contracts;
use App\Facades\Utils;
use App\Models\Award as AwardModel;
use App\Models\User;
use App\Models\UserAward;
use App\Support\Utils;
use Exception;
use Illuminate\Support\Facades\Log;

View File

@@ -1,222 +0,0 @@
<?php
namespace App\Facades;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Facade;
class Utils extends Facade
{
/**
* @return string
*/
protected static function getFacadeAccessor(): string
{
return 'utils';
}
/**
* Simple check on the first character if it's an object or not
*
* @param $obj
*
* @return bool
*/
public static function isObject($obj): bool
{
if (!$obj) {
return false;
}
if ($obj[0] === '{' || $obj[0] === '[') {
return true;
}
return false;
}
/**
* Download a URI. If a file is given, it will save the downloaded
* content into that file
*
* @param string $uri
* @param null $file
*
* @throws \RuntimeException
* @throws \GuzzleHttp\Exception\GuzzleException
*
* @return string
*/
public static function downloadUrl($uri, $file = null): string
{
$opts = [];
if ($file !== null) {
$opts['sink'] = $file;
}
$client = new Client();
$response = $client->request('GET', $uri, $opts);
$body = $response->getBody()->getContents();
if ($response->getHeader('content-type') === 'application/json') {
$body = \GuzzleHttp\json_decode($body);
}
return $body;
}
/**
* Returns a 40 character API key that a user can use
*
* @return string
*/
public static function generateApiKey(): string
{
$key = substr(sha1(time().mt_rand()), 0, 20);
return $key;
}
/**
* @param string $minutes
*
* @return array
*/
public static function minutesToTimeParts($minutes): array
{
$hours = floor($minutes / 60);
$minutes %= 60;
return ['h' => $hours, 'm' => $minutes];
}
public static function minutesToTimeString($minutes): string
{
$hm = self::minutesToTimeParts($minutes);
return $hm['h'].'h '.$hm['m'].'m';
}
/**
* Convert seconds to an array of hours, minutes, seconds
*
* @param int $seconds
*
* @throws \Exception
*
* @return array['h', 'm', 's']
*/
public static function secondsToTimeParts($seconds): array
{
$dtF = new \DateTimeImmutable('@0', new \DateTimeZone('UTC'));
$dtT = new \DateTimeImmutable("@$seconds", new \DateTimeZone('UTC'));
$t = $dtF->diff($dtT);
$retval = [];
$retval['h'] = (int) $t->format('%h');
$retval['m'] = (int) $t->format('%i');
$retval['s'] = (int) $t->format('%s');
return $retval;
}
/**
* Convert seconds to HH MM format
*
* @param int $seconds
* @param bool $incl_sec
*
* @throws \Exception
*
* @return string
*/
public static function secondsToTimeString($seconds, $incl_sec = false): string
{
$hms = self::secondsToTimeParts($seconds);
$format = $hms['h'].'h '.$hms['m'].'m';
if ($incl_sec) {
$format .= ' '.$hms['s'].'s';
}
return $format;
}
/**
* @param $minutes
*
* @return float|int
*/
public static function minutesToSeconds($minutes)
{
return $minutes * 60;
}
/**
* Convert the seconds to minutes and then round it up
*
* @param $seconds
*
* @return float|int
*/
public static function secondsToMinutes($seconds)
{
return ceil($seconds / 60);
}
/**
* Convert hours to minutes. Pretty complex
*
* @param $minutes
*
* @return float|int
*/
public static function minutesToHours($minutes)
{
return $minutes / 60;
}
/**
* @param $hours
* @param null $minutes
*
* @return float|int
*/
public static function hoursToMinutes($hours, $minutes = null)
{
$total = (int) $hours * 60;
if ($minutes) {
$total += (int) $minutes;
}
return $total;
}
/**
* Bitwise operator for setting days of week to integer field
*
* @param int $datefield initial datefield
* @param array $day_enums Array of values from config("enum.days")
*
* @return int
*/
public static function setDays(int $datefield, array $day_enums): int
{
foreach ($day_enums as $day) {
$datefield |= $day;
}
return $datefield;
}
/**
* Bit check if a day exists within a integer bitfield
*
* @param int $datefield datefield from database
* @param int $day_enum Value from config("enum.days")
*
* @return bool
*/
public static function hasDay(int $datefield, int $day_enum): bool
{
return ($datefield & $day_enum) === $datefield;
}
}

View File

@@ -3,7 +3,6 @@
namespace App\Http\Controllers\Admin;
use App\Contracts\Controller;
use App\Facades\Utils;
use App\Http\Requests\CreatePirepRequest;
use App\Http\Requests\UpdatePirepRequest;
use App\Models\Enums\PirepSource;
@@ -260,7 +259,7 @@ class PirepController extends Controller
$hours = (int) $attrs['hours'];
$minutes = (int) $attrs['minutes'];
$pirep->flight_time = Utils::hoursToMinutes($hours) + $minutes;
$pirep->flight_time = Time::hoursToMinutes($hours) + $minutes;
$this->saveCustomFields($pirep, $request);
$this->saveFares($pirep, $request);

View File

@@ -3,7 +3,6 @@
namespace App\Http\Controllers\Admin;
use App\Contracts\Controller;
use App\Facades\Utils;
use App\Http\Requests\CreateUserRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Models\Rank;
@@ -15,6 +14,7 @@ use App\Repositories\PirepRepository;
use App\Repositories\UserRepository;
use App\Services\UserService;
use App\Support\Timezonelist;
use App\Support\Utils;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

View File

@@ -3,7 +3,6 @@
namespace App\Http\Controllers\Auth;
use App\Contracts\Controller;
use App\Facades\Utils;
use App\Models\Enums\UserState;
use App\Models\User;
use App\Repositories\AirlineRepository;
@@ -11,6 +10,7 @@ use App\Repositories\AirportRepository;
use App\Services\UserService;
use App\Support\Countries;
use App\Support\Timezonelist;
use App\Support\Utils;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;

View File

@@ -3,7 +3,6 @@
namespace App\Http\Controllers\Frontend;
use App\Contracts\Controller;
use App\Facades\Utils;
use App\Http\Requests\CreatePirepRequest;
use App\Http\Requests\UpdatePirepRequest;
use App\Models\Enums\PirepSource;
@@ -324,7 +323,7 @@ class PirepController extends Controller
// Any special fields
$hours = (int) $request->input('hours', 0);
$minutes = (int) $request->input('minutes', 0);
$pirep->flight_time = Utils::hoursToMinutes($hours) + $minutes;
$pirep->flight_time = Time::hoursToMinutes($hours) + $minutes;
// Set the correct fuel units
$pirep->block_fuel = new Fuel((float) $request->input('block_fuel'), setting('units.fuel'));

View File

@@ -3,13 +3,13 @@
namespace App\Http\Controllers\Frontend;
use App\Contracts\Controller;
use App\Facades\Utils;
use App\Models\User;
use App\Repositories\AirlineRepository;
use App\Repositories\AirportRepository;
use App\Repositories\UserRepository;
use App\Support\Countries;
use App\Support\Timezonelist;
use App\Support\Utils;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
/**
* Keep custom directives that can be used in templates
*/
class DirectiveServiceProvider extends ServiceProvider
{
public function boot(): void
{
Blade::directive('minutestotime', function ($expr) {
return "<?php echo \App\Support\Units\Time::minutesToTimeString($expr); ?>";
});
Blade::directive('minutestohours', function ($expr) {
return "<?php echo \App\Support\Units\Time::minutesToHours($expr); ?>";
});
}
}

View File

@@ -6,6 +6,36 @@ use Carbon\Carbon;
class Dates
{
/**
* Bitwise operator for setting days of week to integer field
*
* @param int $datefield initial datefield
* @param array $day_enums Array of values from config("enum.days")
*
* @return int
*/
public static function setDays(int $datefield, array $day_enums): int
{
foreach ($day_enums as $day) {
$datefield |= $day;
}
return $datefield;
}
/**
* Bit check if a day exists within a integer bitfield
*
* @param int $datefield datefield from database
* @param int $day_enum Value from config("enum.days")
*
* @return bool
*/
public static function hasDay(int $datefield, int $day_enum): bool
{
return ($datefield & $day_enum) === $datefield;
}
/**
* Get the list of months, given a start date
*

View File

@@ -55,10 +55,18 @@ class HttpClient
*/
public function download($uri, $local_path)
{
$response = $this->httpClient->request('GET', $uri, [
'sink' => $local_path,
]);
$opts = [];
if ($local_path !== null) {
$opts['sink'] = $local_path;
}
return $response;
$response = $this->httpClient->request('GET', $uri, $opts);
$body = $response->getBody()->getContents();
if ($response->getHeader('content-type') === 'application/json') {
$body = \GuzzleHttp\json_decode($body);
}
return $body;
}
}

View File

@@ -4,9 +4,6 @@ namespace App\Support\Units;
use Illuminate\Contracts\Support\Arrayable;
/**
* Class Time
*/
class Time implements Arrayable
{
public $hours;
@@ -90,4 +87,118 @@ class Time implements Arrayable
{
return $this->getMinutes();
}
/**
* @param string $minutes
*
* @return array
*/
public static function minutesToTimeParts($minutes): array
{
$hours = floor($minutes / 60);
$minutes %= 60;
return ['h' => $hours, 'm' => $minutes];
}
public static function minutesToTimeString($minutes): string
{
$hm = self::minutesToTimeParts($minutes);
return $hm['h'].'h '.$hm['m'].'m';
}
/**
* Convert seconds to an array of hours, minutes, seconds
*
* @param int $seconds
*
* @throws \Exception
*
* @return array['h', 'm', 's']
*/
public static function secondsToTimeParts($seconds): array
{
$dtF = new \DateTimeImmutable('@0', new \DateTimeZone('UTC'));
$dtT = new \DateTimeImmutable("@$seconds", new \DateTimeZone('UTC'));
$t = $dtF->diff($dtT);
$retval = [];
$retval['h'] = (int) $t->format('%h');
$retval['m'] = (int) $t->format('%i');
$retval['s'] = (int) $t->format('%s');
return $retval;
}
/**
* Convert seconds to HH MM format
*
* @param int $seconds
* @param bool $incl_sec
*
* @throws \Exception
*
* @return string
*/
public static function secondsToTimeString($seconds, $incl_sec = false): string
{
$hms = self::secondsToTimeParts($seconds);
$format = $hms['h'].'h '.$hms['m'].'m';
if ($incl_sec) {
$format .= ' '.$hms['s'].'s';
}
return $format;
}
/**
* @param $minutes
*
* @return float|int
*/
public static function minutesToSeconds($minutes)
{
return $minutes * 60;
}
/**
* Convert the seconds to minutes and then round it up
*
* @param $seconds
*
* @return float|int
*/
public static function secondsToMinutes($seconds)
{
return ceil($seconds / 60);
}
/**
* Convert hours to minutes. Pretty complex
*
* @param $minutes
*
* @return float|int
*/
public static function minutesToHours($minutes)
{
return $minutes / 60;
}
/**
* @param $hours
* @param null $minutes
*
* @return float|int
*/
public static function hoursToMinutes($hours, $minutes = null)
{
$total = (int) $hours * 60;
if ($minutes) {
$total += (int) $minutes;
}
return $total;
}
}

View File

@@ -11,6 +11,37 @@ use Nwidart\Modules\Facades\Module;
*/
class Utils
{
/**
* Returns a 40 character API key that a user can use
*
* @return string
*/
public static function generateApiKey(): string
{
$key = substr(sha1(time().mt_rand()), 0, 20);
return $key;
}
/**
* Simple check on the first character if it's an object or not
*
* @param $obj
*
* @return bool
*/
public static function isObject($obj): bool
{
if (!$obj) {
return false;
}
if ($obj[0] === '{' || $obj[0] === '[') {
return true;
}
return false;
}
/**
* Enable the debug toolbar
*/