From: Eduardo Date: Sun, 26 Nov 2023 15:04:26 +0000 (+0100) Subject: Coding Style: tryng to follow PSR-12 X-Git-Url: http://git.edufdez.es/?a=commitdiff_plain;h=HEAD;p=g-sheets-php.git Coding Style: tryng to follow PSR-12 --- diff --git a/g-sheet-sync.php b/g-sheet-sync.php index c9f471e..c945043 100644 --- a/g-sheet-sync.php +++ b/g-sheet-sync.php @@ -10,12 +10,12 @@ require_once "vendor/autoload.php"; /** * usefull: https://www.nidup.io/blog/manipulate-google-sheets-in-php-with-api */ -class GSheetSync { - +class GSheetSync +{ private $service; private $spreadsheetId; - function __construct(string $credentialsPath, string $spreadsheetId) + public function __construct(string $credentialsPath, string $spreadsheetId) { $client = new \Google_Client(); $client->setApplicationName('Google Sheets API'); @@ -24,7 +24,7 @@ class GSheetSync { $client->setAuthConfig($credentialsPath); - $this->service = new \Google\Service\Sheets($client); + $this->service = new \Google\Service\Sheets($client); $this->spreadsheetId = $spreadsheetId; } @@ -32,10 +32,11 @@ class GSheetSync { * @param string $range the name of the Sheet to get all the rows, extra args to pick. EX: 'Sheet1!A1:F10' * @return string | false JSON or false if failure */ - function get(string $range): string | false { + public function get(string $range): string | false + { // get all the rows of a sheet $response = $this->service->spreadsheets_values->get($this->spreadsheetId, $range); - $rows = $response->getValues(); + $rows = $response->getValues(); // Remove the first one that contains headers $headers = array_shift($rows); @@ -43,8 +44,9 @@ class GSheetSync { $array = []; foreach ($rows as $row) { # fill blank spaces at end if needed - while (count($headers) > count($row)) + while (count($headers) > count($row)) { $row[] = null; + } $array[] = array_combine($headers, $row); } @@ -57,7 +59,8 @@ class GSheetSync { * @param string $sheet sheet of the document, where to append the new row * @param array $arr columns to append at the end of the document */ - function put(string $sheet, array $row): void { + public function put(string $sheet, array $row): void + { $valueRange = new \Google\Service\Sheets\ValueRange(); $valueRange->setValues([$row]); diff --git a/index.php b/index.php index 222d510..2a17599 100644 --- a/index.php +++ b/index.php @@ -27,7 +27,7 @@ switch ($_SERVER['REQUEST_METHOD']) { $filename = generateFilename($_ENV["sheetId"], $sheet); if (fileExpired($filename)) { $sheetSync = new GSheetSync($_ENV["credentialsPath"], $_ENV["sheetId"]); - $res = $sheetSync->get($sheet); + $res = $sheetSync->get($sheet); $etag = md5($res); header('ETag: ' . $etag); @@ -48,7 +48,7 @@ switch ($_SERVER['REQUEST_METHOD']) { $filename = generateFilename($_ENV["sheetId"], $sheet); if (fileExpired($filename)) { $sheetSync = new GSheetSync($_ENV["credentialsPath"], $_ENV["sheetId"]); - $res = $sheetSync->get($sheet); + $res = $sheetSync->get($sheet); // dont make the user wait, print the json $etag = md5($res); @@ -99,10 +99,14 @@ function fileExpired(string $filename): bool $filetime = filemtime($filename); // if file does not exist counts as expired - if (!$filetime) return true; + if (!$filetime) { + return true; + } // if file older than TTL minutes IS expired - if ($filetime < time() - TTL) return true; + if ($filetime < time() - TTL) { + return true; + } // else file is NOT expired return false; @@ -118,7 +122,6 @@ function hasFileContentChanged(string $oldFilePath, string $newFileString): bool return !(md5_file($oldFilePath) == md5($newFileString)); } - /** * Sends an 304 Not Modified header if the ETag matches the sent one */ @@ -131,8 +134,9 @@ function sendNotModifiedIfSameETag(string $etag, bool $die = true) if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) { http_response_code(304); header('HTTP/1.1 304 Not Modified'); - if ($die) + if ($die) { exit(); + } } } }