Changeset View
Changeset View
Standalone View
Standalone View
src/app/Utils.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use Carbon\Carbon; | use Carbon\Carbon; | ||||
use Illuminate\Support\Facades\Auth; | use Illuminate\Support\Facades\Auth; | ||||
use Ramsey\Uuid\Uuid; | use Ramsey\Uuid\Uuid; | ||||
/** | /** | ||||
* Small utility functions for App. | * Small utility functions for App. | ||||
*/ | */ | ||||
class Utils | class Utils | ||||
{ | { | ||||
/** | /** | ||||
* Count the number of lines in a file. | |||||
* | |||||
* Useful for progress bars. | |||||
* | |||||
* @param string $file The filepath to count the lines of. | |||||
* | |||||
* @return int | |||||
*/ | |||||
public static function countLines($file) | |||||
{ | |||||
$fh = fopen($file, 'rb'); | |||||
$numLines = 0; | |||||
while (!feof($fh)) { | |||||
$numLines += substr_count(fread($fh, 8192), "\n"); | |||||
} | |||||
fclose($fh); | |||||
return $numLines; | |||||
} | |||||
/** | |||||
* Return the country ISO code for an IP address. | |||||
* | |||||
* @return string | |||||
*/ | |||||
public static function countryForIP($ip) | |||||
{ | |||||
$query = " | |||||
SELECT country FROM ip4nets | |||||
WHERE INET_ATON(net_number) <= INET_ATON(?) | |||||
AND INET_ATON(net_broadcast) >= INET_ATON(?) | |||||
ORDER BY INET_ATON(net_number), net_mask DESC LIMIT 1 | |||||
"; | |||||
$nets = \Illuminate\Support\Facades\DB::select($query, [$ip, $ip]); | |||||
if (sizeof($nets) > 0) { | |||||
machniak: What if there's no record found? What if country is unset? I don't see a fallback. | |||||
return $nets[0]->country; | |||||
} | |||||
return 'CH'; | |||||
} | |||||
/** | |||||
* Return the country ISO code for the current request. | |||||
*/ | |||||
public static function countryForRequest() | |||||
{ | |||||
$request = \request(); | |||||
if (!$request) { | |||||
return 'CH'; | |||||
} | |||||
$ip = $request->ip(); | |||||
return self::countryForIP($ip); | |||||
} | |||||
/** | |||||
* Shortcut to creating a progress bar of a particular format with a particular message. | |||||
* | |||||
* @param \Symfony\Component\Console\Output\OutputInterface $output | |||||
* @param int $count | |||||
* @param string $message | |||||
* | |||||
* @return \Symfony\Component\Console\Helper\ProgressBar | |||||
*/ | |||||
public static function createProgressBar($output, $count, $message = null) | |||||
{ | |||||
$bar = $output->createProgressBar($count); | |||||
$bar->setFormat( | |||||
'%current:7s%/%max:7s% [%bar%] %percent:3s%% %elapsed:7s%/%estimated:-7s% %message% ' | |||||
); | |||||
if ($message) { | |||||
$bar->setMessage($message . " ..."); | |||||
} | |||||
$bar->start(); | |||||
return $bar; | |||||
} | |||||
/** | |||||
* Return the number of days in the month prior to this one. | * Return the number of days in the month prior to this one. | ||||
* | * | ||||
* @return int | * @return int | ||||
*/ | */ | ||||
public static function daysInLastMonth() | public static function daysInLastMonth() | ||||
{ | { | ||||
$start = new Carbon('first day of last month'); | $start = new Carbon('first day of last month'); | ||||
$end = new Carbon('last day of last month'); | $end = new Carbon('last day of last month'); | ||||
▲ Show 20 Lines • Show All 130 Lines • Show Last 20 Lines |
What if there's no record found? What if country is unset? I don't see a fallback.