Skip to content

Commit

Permalink
Modify constants to public static variables
Browse files Browse the repository at this point in the history
  • Loading branch information
yash30201 committed Sep 14, 2023
1 parent 9799903 commit 5caf63a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
43 changes: 21 additions & 22 deletions src/MetricsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,27 @@
*/
trait MetricsTrait
{
public const METRICS_HEADER_KEY = 'x-goog-api-client';
public static $metricsHeaderKey = 'x-goog-api-client';

// Auth request type
public const REQUEST_TYPE_ACCESS_TOKEN = 'auth-request-type/at';
public const REQUEST_TYPE_ID_TOKEN = 'auth-request-type/it';
public const REQUEST_TYPE_MDS_PING = 'auth-request-type/mds';
public const REQUEST_TYPE_REAUTH_START = 'auth-request-type/re-start';
public static $requestTypeAccessToken = 'auth-request-type/at';
public static $requestTypeIdToken = 'auth-request-type/it';
public static $requestTypeMdsPing = 'auth-request-type/mds';

// Credential type
public const CRED_TYPE_USER = 'cred-type/u';
public const CRED_TYPE_SA_ASSERTION = 'cred-type/sa';
public const CRED_TYPE_SA_JWT = 'cred-type/jwt';
public const CRED_TYPE_SA_MDS = 'cred-type/mds';
public const CRED_TYPE_SA_IMPERSONATE = 'cred-type/imp';
public static $credTypeUser = 'cred-type/u';
public static $credTypeSaAssertion = 'cred-type/sa';
public static $credTypeSaJwt = 'cred-type/jwt';
public static $credTypeSaMds = 'cred-type/mds';
public static $credTypeSaImpersonate = 'cred-type/imp';

// TODO: Find a way to get the auth version
// Auth library version
public const VERSION = '10.0.0';
public static $version = '10.0.0';

public function getPhpAndAuthLibVersion()
{
return 'gl-php/' . PHP_VERSION . ' auth/' . self::VERSION;
return 'gl-php/' . PHP_VERSION . ' auth/' . self::$version;
}

/**
Expand All @@ -59,7 +58,7 @@ public function getPhpAndAuthLibVersion()
*/
public function getTokenRequestMdsHeader(bool $isAccessTokenRequest)
{
return $this->getDefaults($isAccessTokenRequest) . ' ' . self::CRED_TYPE_SA_MDS;
return $this->getDefaults($isAccessTokenRequest) . ' ' . self::$credTypeSaMds;
}

/**
Expand All @@ -72,7 +71,7 @@ public function getTokenRequestMdsHeader(bool $isAccessTokenRequest)
*/
public function getTokenRequestSaAssertionHeader(bool $isAccessTokenRequest)
{
return $this->getDefaults($isAccessTokenRequest) . ' ' . self::CRED_TYPE_SA_ASSERTION;
return $this->getDefaults($isAccessTokenRequest) . ' ' . self::$credTypeSaAssertion;
}

/**
Expand All @@ -85,7 +84,7 @@ public function getTokenRequestSaAssertionHeader(bool $isAccessTokenRequest)
*/
public function getTokenRequestSaImpersonateHeader(bool $isAccessTokenRequest)
{
return $this->getDefaults($isAccessTokenRequest) . ' ' . self::CRED_TYPE_SA_IMPERSONATE;
return $this->getDefaults($isAccessTokenRequest) . ' ' . self::$credTypeSaImpersonate;
}

/**
Expand All @@ -98,7 +97,7 @@ public function getTokenRequestSaImpersonateHeader(bool $isAccessTokenRequest)
*/
public function getTokenRequestUserHeader(bool $isAccessTokenRequest)
{
return $this->getDefaults($isAccessTokenRequest) . ' ' . self::CRED_TYPE_USER;
return $this->getDefaults($isAccessTokenRequest) . ' ' . self::$credTypeUser;
}

/**
Expand All @@ -107,7 +106,7 @@ public function getTokenRequestUserHeader(bool $isAccessTokenRequest)
public function getMdsPingHeader()
{
return $this->getPhpAndAuthLibVersion()
. ' ' . self::REQUEST_TYPE_MDS_PING;
. ' ' . self::$requestTypeMdsPing;
}

/**
Expand All @@ -122,10 +121,10 @@ public function applyAuthMetricsHeaders(array $headers, string $metricsHeaderToA
{
if ($metricsHeaderToApply == '') {
return $headers;
} else if (isset($headers[self::METRICS_HEADER_KEY])) {
$headers[self::METRICS_HEADER_KEY][0] .= ' ' . $metricsHeaderToApply;
} else if (isset($headers[self::$metricsHeaderKey])) {
$headers[self::$metricsHeaderKey][0] .= ' ' . $metricsHeaderToApply;
} else {
$headers[self::METRICS_HEADER_KEY] = [$metricsHeaderToApply];
$headers[self::$metricsHeaderKey] = [$metricsHeaderToApply];
}
return $headers;
}
Expand All @@ -134,9 +133,9 @@ private function getDefaults(bool $forAccessToken = true)
{
$result = $this->getPhpAndAuthLibVersion();
if ($forAccessToken) {
$result .= ' ' . self::REQUEST_TYPE_ACCESS_TOKEN;
$result .= ' ' . self::$requestTypeAccessToken;
} else {
$result .= ' ' . self::REQUEST_TYPE_ID_TOKEN;
$result .= ' ' . self::$requestTypeIdToken;
}
return $result;
}
Expand Down
22 changes: 11 additions & 11 deletions tests/MetricsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function setUp(): void
$this->phpAndAuthVersion = 'gl-php/' . PHP_VERSION
. ' auth/' . self::VERSION;
$this->defaultForAccessTokenRequest = $this->phpAndAuthVersion
. ' ' . $this->impl::REQUEST_TYPE_ACCESS_TOKEN;
. ' ' . $this->impl::$requestTypeAccessToken;
$this->defaultForIdTokenRequest = $this->phpAndAuthVersion
. ' ' . $this->impl::REQUEST_TYPE_ID_TOKEN;
. ' ' . $this->impl::$requestTypeIdToken;
}

public function testGetPhpAndAuthLibVersion()
Expand Down Expand Up @@ -89,7 +89,7 @@ public function testGetTokenRequestHeaders(
public function testGetMdsPingHeader()
{
$this->assertEquals(
$this->phpAndAuthVersion . ' ' . $this->impl::REQUEST_TYPE_MDS_PING,
$this->phpAndAuthVersion . ' ' . $this->impl::$requestTypeMdsPing,
$this->impl->getMdsPingHeader()
);
}
Expand All @@ -98,14 +98,14 @@ public function getTokenRequestHeaderCases()
{
$impl = new MetricsTraitImplementation();
return [
[true, 'Mds', $impl::CRED_TYPE_SA_MDS],
[false, 'Mds', $impl::CRED_TYPE_SA_MDS],
[true, 'SaAssertion', $impl::CRED_TYPE_SA_ASSERTION],
[false, 'SaAssertion', $impl::CRED_TYPE_SA_ASSERTION],
[true, 'SaImpersonate', $impl::CRED_TYPE_SA_IMPERSONATE],
[false, 'SaImpersonate', $impl::CRED_TYPE_SA_IMPERSONATE],
[true, 'User', $impl::CRED_TYPE_USER],
[false, 'User', $impl::CRED_TYPE_USER]
[true, 'Mds', $impl::$credTypeSaMds],
[false, 'Mds', $impl::$credTypeSaMds],
[true, 'SaAssertion', $impl::$credTypeSaAssertion],
[false, 'SaAssertion', $impl::$credTypeSaAssertion],
[true, 'SaImpersonate', $impl::$credTypeSaImpersonate],
[false, 'SaImpersonate', $impl::$credTypeSaImpersonate],
[true, 'User', $impl::$credTypeUser],
[false, 'User', $impl::$credTypeUser]
];
}
}
Expand Down

0 comments on commit 5caf63a

Please sign in to comment.