Skip to content

Commit

Permalink
Fix style and improved readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Hectorhammett committed Nov 11, 2024
1 parent a57a184 commit 016dad1
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 50 deletions.
15 changes: 4 additions & 11 deletions src/ApplicationDefaultCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use Google\Auth\Subscriber\AuthTokenSubscriber;
use GuzzleHttp\Client;
use InvalidArgumentException;
use PHPUnit\TextUI\XmlConfiguration\Logging\Logging;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -338,22 +337,16 @@ public static function getDefaultLogger(): null|LoggerInterface
$loggingFlag = getenv(self::SDK_DEBUG_FLAG);

// Env var is not set
if (!is_string($loggingFlag)) {
if (is_array($loggingFlag)) {
trigger_error('The ' . self::SDK_DEBUG_FLAG . ' is set, but it is set to another value than false, true, 0 or 1. Logging is disabled');
return null;
}

if (!$loggingFlag) {
return null;
}

$loggingFlag = strtolower($loggingFlag);

// Env Var is not true
if ($loggingFlag !== 'true' && $loggingFlag !== '1') {
// Env var is set to a non valid value
if ($loggingFlag !== 'false' && $loggingFlag !== '0') {
trigger_error('The ' . self::SDK_DEBUG_FLAG . ' is set, but it is set to another value than false, true, 0 or 1. Logging is disabled');
if ($loggingFlag !== 'true') {
if ($loggingFlag !== 'false') {
trigger_error('The ' . self::SDK_DEBUG_FLAG . ' is set, but it is set to another value than false or true. Logging is disabled');
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/HttpHandler/Guzzle6HttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function async(RequestInterface $request, array $options = [])
$requestEvent = new LogEvent();

$requestEvent->method = $request->getMethod();
$requestEvent->url = $request->getUri()->__toString();
$requestEvent->url = (string) $request->getUri();
$requestEvent->headers = $request->getHeaders();
$requestEvent->payload = $request->getBody()->getContents();
$requestEvent->retryAttempt = $options['retryAttempt'] ?? null;
Expand Down
8 changes: 3 additions & 5 deletions src/HttpHandler/HttpHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ public static function build(
$client = new Client(['handler' => $stack]);
}

if ($logger === false) {
$logger = null;
} else {
$logger = $logger ?? ApplicationDefaultCredentials::getDefaultLogger();
}
$logger = ($logger === false)
? null
: $logger ?? ApplicationDefaultCredentials::getDefaultLogger();

$version = null;
if (defined('GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
Expand Down
17 changes: 11 additions & 6 deletions src/Logging/LoggingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,10 @@ private function logResponse(LogEvent $event): void
$stringifiedEvent = json_encode($debugEvent);

// There was an error stringifying the event, return to not break execution
if ($stringifiedEvent === false) {
return;
if ($stringifiedEvent !== false) {
$this->logger->debug($stringifiedEvent);
}

$this->logger->debug($stringifiedEvent);

if ($event->status) {
$infoEvent = [
'timestamp' => $event->timestamp,
Expand Down Expand Up @@ -136,12 +134,19 @@ private function logStatus(LogEvent $event): void
fn ($value) => !is_null($value)
);

$this->logger->info((string) json_encode($infoEvent));
$stringifiedEvent = json_encode($infoEvent);

// There was an error stringifying the event, return to not break execution
if ($stringifiedEvent === false) {
return;
}

$this->logger->info($stringifiedEvent);
}

/**
* @param array<mixed> $headers
* @return null|array<mixed, mixed>
* @return null|array<string, string|false>
*/
private function getJwtToken(array $headers): null|array
{
Expand Down
6 changes: 3 additions & 3 deletions src/Logging/StdOutLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ class StdOutLogger implements LoggerInterface
*/
public function __construct(string $level = LogLevel::DEBUG)
{
$this->level = $this->getLevelMap($level);
$this->level = $this->getLevelFromName($level);
}

/**
* {@inheritdoc}
*/
public function log($level, string|Stringable $message, array $context = []): void
{
if ($this->getLevelMap($level) < $this->level) {
if ($this->getLevelFromName($level) < $this->level) {
return;
}

Expand All @@ -72,7 +72,7 @@ public function log($level, string|Stringable $message, array $context = []): vo
* @return int
* @throws InvalidArgumentException
*/
private function getLevelMap(string $levelName): int
private function getLevelFromName(string $levelName): int
{
if (!array_key_exists($levelName, $this->levelMapping)) {
throw new InvalidArgumentException('The level supplied to the Logger is not valid');
Expand Down
4 changes: 0 additions & 4 deletions tests/ApplicationDefaultCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,10 +783,6 @@ public function testGetDefaultLoggerReturnStdOutLoggerIfEnvVarIsPresent()
putenv($this::SDK_DEBUG_FLAG . '=true');
$logger = ApplicationDefaultCredentials::getDefaultLogger();
$this->assertTrue($logger instanceof StdOutLogger);

putenv($this::SDK_DEBUG_FLAG . '=1');
$logger = ApplicationDefaultCredentials::getDefaultLogger();
$this->assertTrue($logger instanceof StdOutLogger);
}

public function testGetDefaultLoggerReturnsNullIfNotEnvVar()
Expand Down
25 changes: 5 additions & 20 deletions tests/Logging/StdOutLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,28 @@ public function testConstructsWithAnIncorrectLevelThrowsException()

public function testLoggingOnSameLevelWritesToStdOut()
{
ob_start();
$expectedString = 'test';
$this->expectOutputString($expectedString . "\n");

$logger = new StdOutLogger(LogLevel::DEBUG);
$expectedString = 'test';
$logger->debug($expectedString);
$buffer = ob_get_contents();

$this->assertEquals($expectedString . "\n", $buffer);

ob_end_clean();
}

public function testLoggingOnHigherLeverWritesToStdOut()
{
ob_start();
$expectedString = 'test';
$this->expectOutputString($expectedString. "\n");

$logger = new StdOutLogger(LogLevel::WARNING);
$expectedString = 'test';
$logger->error($expectedString);
$buffer = ob_get_contents();

$this->assertEquals($expectedString . "\n", $buffer);

ob_end_clean();
}

public function testLoggingOnLowerLeverDoesNotWriteToStdOut()
{
ob_start();
$this->expectOutputString('');

$logger = new StdOutLogger(LogLevel::WARNING);
$expectedString = 'test';
$logger->debug($expectedString);
$buffer = ob_get_contents();

$this->assertEmpty($buffer);

ob_end_clean();
}
}

0 comments on commit 016dad1

Please sign in to comment.