Skip to content

Commit

Permalink
Expose analytics token setters
Browse files Browse the repository at this point in the history
  • Loading branch information
const-cloudinary committed Feb 1, 2023
1 parent c765dfe commit 27187ef
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
51 changes: 50 additions & 1 deletion src/Asset/Analytics/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Analytics
const ALGO_VERSION = 'A'; // The version of the algorithm
const SDK_CODE = 'A'; // Cloudinary PHP SDK

protected static $sdkCode = self::SDK_CODE;
protected static $sdkVersion = Cloudinary::VERSION;
protected static $techVersion = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;

Expand All @@ -44,7 +45,7 @@ public static function sdkAnalyticsSignature()
if (empty(static::$signature)) {
// Lazily create $signature
try {
static::$signature = static::ALGO_VERSION . static::SDK_CODE .
static::$signature = static::ALGO_VERSION . static::$sdkCode .
static::encodeVersion(static::$sdkVersion) .
static::encodeVersion(static::$techVersion);
} catch (OutOfRangeException $e) {
Expand All @@ -55,6 +56,54 @@ public static function sdkAnalyticsSignature()
return static::$signature;
}

/**
* Sets the SDK code.
*
* Used for integrations.
*
* @param string $sdkCode The SDK code to set.
*
* @return void
*
* @internal
*/
public static function sdkCode($sdkCode)
{
static::$sdkCode = $sdkCode;
}

/**
* Sets the SDK version.
*
* Used for integrations.
*
* @param string $sdkVersion The SDK version to set (MAJOR.MINOR.PATCH), for example: "1.0.0".
*
* @return void
*
* @internal
*/
public static function sdkVersion($sdkVersion)
{
static::$sdkVersion = $sdkVersion;
}

/**
* Sets the tech version.
*
* Used for integrations.
*
* @param string $techVersion The tech version to set (MAJOR.MINOR), for example: "1.0".
*
* @return void
*
* @internal
*/
public static function techVersion($techVersion)
{
static::$techVersion = $techVersion;
}

/**
* Encodes a semVer-like version string.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Asset/AnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ public function testSdkAnalyticsSignature()
MockAnalytics::sdkAnalyticsSignature()
);
}

public function testSdkAnalyticsSignatureWithIntegration()
{
MockAnalytics::sdkCode('W');
MockAnalytics::sdkVersion('2.0.0');
MockAnalytics::techVersion('9.5');

self::assertEquals(
'AWAACH9',
MockAnalytics::sdkAnalyticsSignature()
);
}
}

0 comments on commit 27187ef

Please sign in to comment.