Skip to content

Commit

Permalink
Updates for PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
spvickers committed Jul 31, 2023
1 parent 4c33db8 commit 57c4b82
Show file tree
Hide file tree
Showing 108 changed files with 8,214 additions and 9,510 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
}
],
"require": {
"php": ">=5.6.0",
"firebase/php-jwt": ">=5.5.0 <=6.0.0"
"php": ">=8.1",
"firebase/php-jwt": "^6.8"
},
"autoload": {
"psr-4": {
Expand Down
62 changes: 33 additions & 29 deletions src/AccessToken.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace ceLTIc\LTI;

Expand All @@ -22,54 +23,57 @@ class AccessToken
*
* @var string|null $token
*/
public $token = null;
public ?string $token = null;

/**
* Timestamp at which the token string expires.
*
* @var int|null $expires
*/
public $expires = null;
public ?int $expires = null;

/**
* Scope(s) for which the access token is valid.
*
* @var array $scopes
*/
public $scopes = array();
public array $scopes = [];

/**
* Platform for this token.
*
* @var Platform|null $platform
* @var Platform $platform
*/
private $platform = null;
private Platform $platform;

/**
* Timestamp for when the object was created.
*
* @var int|null $created
*/
public $created = null;
public ?int $created = null;

/**
* Timestamp for when the object was last updated.
*
* @var int|null $updated
*/
public $updated = null;
public ?int $updated = null;

/**
* Class constructor.
*
* @param Platform $platform Platform
* @param array|null $scopes Scopes for which the access token is valid
* @param string|null $token Access token string
* @param int|null $expires Time in seconds after which the token string will expire
* @param Platform $platform Platform
* @param array|null $scopes Scopes for which the access token is valid
* @param string|null $token Access token string
* @param int|null $expires Time in seconds after which the token string will expire
*/
public function __construct($platform, $scopes = null, $token = null, $expires = null)
public function __construct(Platform $platform, ?array $scopes = [], ?string $token = null, ?int $expires = null)
{
$this->platform = $platform;
if (is_null($scopes)) {
$scopes = [];
}
$this->scopes = $scopes;
if (!empty($token)) {
$this->token = $token;
Expand All @@ -89,27 +93,27 @@ public function __construct($platform, $scopes = null, $token = null, $expires =
*
* @return Platform Platform object for this resource link.
*/
public function getPlatform()
public function getPlatform(): Platform
{
return $this->platform;
}

/**
* Load a nonce value from the database.
*
* @return bool True if the nonce value was successfully loaded
* @return bool True if the nonce value was successfully loaded
*/
public function load()
public function load(): bool
{
return $this->platform->getDataConnector()->loadAccessToken($this);
}

/**
* Save a nonce value in the database.
*
* @return bool True if the nonce value was successfully saved
* @return bool True if the nonce value was successfully saved
*/
public function save()
public function save(): bool
{
sort($this->scopes);
return $this->platform->getDataConnector()->saveAccessToken($this);
Expand All @@ -118,11 +122,11 @@ public function save()
/**
* Check if a valid access token exists for a specific scope (or any scope if none specified).
*
* @param string $scope Access scope
* @param string $scope Access scope
*
* @return bool True if there is an unexpired access token for specified scope
* @return bool True if there is an unexpired access token for specified scope
*/
public function hasScope($scope = '')
public function hasScope(string $scope = ''): bool
{
if (substr($scope, -9) === '.readonly') {
$scope2 = substr($scope, 0, -9);
Expand All @@ -136,17 +140,17 @@ public function hasScope($scope = '')
/**
* Obtain a valid access token for a scope.
*
* @param string $scope Access scope
* @param bool $scopeOnly If true, a token is requested just for the specified scope
* @param string $scope Access scope
* @param bool $scopeOnly If true, a token is requested just for the specified scope
*
* @return AccessToken New access token
* @return AccessToken New access token
*/
public function get($scope = '', $scopeOnly = false)
public function get(string $scope = '', bool $scopeOnly = false): AccessToken
{
$url = $this->platform->accessTokenUrl;
if (!empty($url) && !empty(Tool::$defaultTool) && !empty(Tool::$defaultTool->rsaKey)) {
if ($scopeOnly) {
$scopesRequested = array($scope);
$scopesRequested = [$scope];
} else {
$scopesRequested = Tool::$defaultTool->requiredScopes;
if (substr($scope, -9) === '.readonly') {
Expand All @@ -163,11 +167,11 @@ public function get($scope = '', $scopeOnly = false)
do {
$method = 'POST';
$type = 'application/x-www-form-urlencoded';
$body = array(
$body = [
'grant_type' => 'client_credentials',
'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
'scope' => implode(' ', $scopesRequested)
);
];
if (!empty(Tool::$defaultTool)) {
Tool::$defaultTool->platform = $this->platform;
$body = Tool::$defaultTool->signServiceRequest($url, $method, $type, $body);
Expand Down Expand Up @@ -195,12 +199,12 @@ public function get($scope = '', $scopeOnly = false)
$retry = false;
} elseif (!empty($scope) && (count($scopesRequested) > 1)) { // Just ask for the single scope requested
$retry = true;
$scopesRequested = array($scope);
$scopesRequested = [$scope];
}
} while ($retry);
}
} else {
$this->scopes = null;
$this->scopes = [];
$this->token = null;
$this->expires = null;
$this->created = null;
Expand Down
27 changes: 14 additions & 13 deletions src/ApiHook/ApiContext.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
declare(strict_types=1);

namespace ceLTIc\LTI\ApiHook;

use ceLTIc\LTI\Service;
use ceLTIc\LTI\Context;
use ceLTIc\LTI\Enum\ToolSettingsMode;

/**
* Class to implement context services for a platform via its proprietary API
Expand All @@ -20,14 +21,14 @@ class ApiContext
*
* @var Context $context
*/
protected $context = null;
protected Context $context;

/**
* Class constructor.
*
* @param Context $context
*/
public function __construct($context)
public function __construct(Context $context)
{
$this->context = $context;
}
Expand All @@ -37,7 +38,7 @@ public function __construct($context)
*
* @return bool True if the API hook has been configured
*/
public function isConfigured()
public function isConfigured(): bool
{
return true;
}
Expand All @@ -47,44 +48,44 @@ public function isConfigured()
*
* @return bool True if the request was successful
*/
public function getGroups()
public function getGroups(): bool
{
return false;
}

/**
* Get Memberships.
*
* @param bool $withGroups True is group information is to be requested as well
* @param bool $withGroups True is group information is to be requested as well
*
* @return array|bool The array of UserResult objects if successful, otherwise false
*/
public function getMemberships($withGroups)
public function getMemberships(bool $withGroups): array|bool
{
return false;
}

/**
* Get Tool Settings.
*
* @param int|null $mode Mode for request (optional, default is current level only)
* @param bool $simple True if all the simple media type is to be used (optional, default is true)
* @param ToolSettingsMode|null $mode Mode for request (optional, default is current level only)
* @param bool $simple True if all the simple media type is to be used (optional, default is true)
*
* @return array|bool The array of settings if successful, otherwise false
*/
public function getToolSettings($mode = Service\ToolSettings::MODE_CURRENT_LEVEL, $simple = true)
public function getToolSettings(?ToolSettingsMode $mode = null, bool $simple = true): array|bool
{
return false;
}

/**
* Perform a Tool Settings service request.
*
* @param array $settings An associative array of settings (optional, default is none)
* @param array $settings An associative array of settings (optional, default is none)
*
* @return bool True if action was successful, otherwise false
* @return bool True if action was successful, otherwise false
*/
public function setToolSettings($settings = array())
public function setToolSettings(array $settings = []): bool
{
return false;
}
Expand Down
43 changes: 21 additions & 22 deletions src/ApiHook/ApiHook.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace ceLTIc\LTI\ApiHook;

Expand All @@ -15,51 +16,51 @@ trait ApiHook
/**
* User Id hook name.
*/
public static $USER_ID_HOOK = "UserId";
public static string $USER_ID_HOOK = "UserId";

/**
* Context Id hook name.
*/
public static $CONTEXT_ID_HOOK = "ContextId";
public static string $CONTEXT_ID_HOOK = "ContextId";

/**
* Course Groups service hook name.
*/
public static $GROUPS_SERVICE_HOOK = "Groups";
public static string $GROUPS_SERVICE_HOOK = "Groups";

/**
* Memberships service hook name.
*/
public static $MEMBERSHIPS_SERVICE_HOOK = "Memberships";
public static string $MEMBERSHIPS_SERVICE_HOOK = "Memberships";

/**
* Outcomes service hook name.
*/
public static $OUTCOMES_SERVICE_HOOK = "Outcomes";
public static string $OUTCOMES_SERVICE_HOOK = "Outcomes";

/**
* Tool Settings service hook name.
*/
public static $TOOL_SETTINGS_SERVICE_HOOK = "ToolSettings";
public static string $TOOL_SETTINGS_SERVICE_HOOK = "ToolSettings";

/**
* Access Token service hook name.
*/
public static $ACCESS_TOKEN_SERVICE_HOOK = "AccessToken";
public static string $ACCESS_TOKEN_SERVICE_HOOK = "AccessToken";

/**
* API hook class names.
*/
private static $API_HOOKS = array();
private static array $API_HOOKS = [];

/**
* Register the availability of an API hook.
*
* @param string $hookName Name of hook
* @param string $hookName Name of hook
* @param string $familyCode Family code for current platform
* @param string $className Name of implementing class
* @param string $className Name of implementing class
*/
public static function registerApiHook($hookName, $familyCode, $className)
public static function registerApiHook(string $hookName, string $familyCode, string $className): void
{
$objectClass = get_class();
self::$API_HOOKS["{$objectClass}-{$hookName}-{$familyCode}"] = $className;
Expand All @@ -68,12 +69,12 @@ public static function registerApiHook($hookName, $familyCode, $className)
/**
* Get the class name for an API hook.
*
* @param string $hookName Name of hook
* @param string $hookName Name of hook
* @param string $familyCode Family code for current platform
*
* @return string|null Class name
*/
private static function getApiHook($hookName, $familyCode)
private static function getApiHook(string $hookName, string $familyCode): ?string
{
$class = self::class;
return self::$API_HOOKS["{$class}-{$hookName}-{$familyCode}"];
Expand All @@ -85,9 +86,9 @@ private static function getApiHook($hookName, $familyCode)
* @param string $hookName Name of hook
* @param string $familyCode Family code for current platform
*
* @return bool True if the API hook is registered
* @return bool True if the API hook is registered
*/
private static function hasApiHook($hookName, $familyCode)
private static function hasApiHook(string $hookName, string $familyCode): bool
{
$class = self::class;
return isset(self::$API_HOOKS["{$class}-{$hookName}-{$familyCode}"]);
Expand All @@ -96,13 +97,13 @@ private static function hasApiHook($hookName, $familyCode)
/**
* Check if an API hook is registered and configured.
*
* @param string $hookName Name of hook
* @param string $familyCode Family code for current platform
* @param Platform|Context|ResourceLink $sourceObject Source object for which hook is to be used
* @param string $hookName Name of hook
* @param string $familyCode Family code for current platform
* @param Platform|Context|ResourceLink $sourceObject Source object for which hook is to be used
*
* @return bool True if the API hook is registered and configured
* @return bool True if the API hook is registered and configured
*/
private static function hasConfiguredApiHook($hookName, $familyCode, $sourceObject)
private static function hasConfiguredApiHook(string $hookName, string $familyCode, $sourceObject): bool
{
$ok = false;
$class = self::class;
Expand All @@ -116,5 +117,3 @@ private static function hasConfiguredApiHook($hookName, $familyCode, $sourceObje
}

}

?>
Loading

0 comments on commit 57c4b82

Please sign in to comment.