diff --git a/src/Socialbox/Classes/StandardMethods/GetSession.php b/src/Socialbox/Classes/StandardMethods/GetSession.php new file mode 100644 index 0000000..f0035ae --- /dev/null +++ b/src/Socialbox/Classes/StandardMethods/GetSession.php @@ -0,0 +1,40 @@ +getSessionUuid() === null) + { + return $rpcRequest->produceError(StandardError::SESSION_REQUIRED); + } + + try + { + // Get the session + $session = SessionManager::getSession($request->getSessionUuid()); + } + catch(DatabaseOperationException $e) + { + throw new StandardException("There was an unexpected error while trying to retrieve the session", StandardError::INTERNAL_SERVER_ERROR, $e); + } + + + } + } \ No newline at end of file diff --git a/src/Socialbox/Classes/Utilities.php b/src/Socialbox/Classes/Utilities.php index b538531..a935c76 100644 --- a/src/Socialbox/Classes/Utilities.php +++ b/src/Socialbox/Classes/Utilities.php @@ -192,4 +192,26 @@ class Utilities { return preg_replace('/[^a-zA-Z0-9-_]/', '', $name); } + + /** + * Converts an array into a serialized string by joining the elements with a comma. + * + * @param array $list An array of elements that need to be converted to a comma-separated string. + * @return string A string representation of the array elements, joined by commas. + */ + public static function serializeList(array $list): string + { + return implode(',', $list); + } + + /** + * Converts a serialized string into an array by splitting the string at each comma. + * + * @param string $list A comma-separated string that needs to be converted to an array. + * @return array An array of string values obtained by splitting the input string. + */ + public static function unserializeList(string $list): array + { + return explode(',', $list); + } } \ No newline at end of file diff --git a/src/Socialbox/Enums/Flags/SessionFlags.php b/src/Socialbox/Enums/Flags/SessionFlags.php new file mode 100644 index 0000000..821c514 --- /dev/null +++ b/src/Socialbox/Enums/Flags/SessionFlags.php @@ -0,0 +1,21 @@ +uuid = $data['uuid']; - $this->peerUuid = $data['peer_uuid'] ?? null; - $this->authenticated = $data['authenticated'] ?? false; - $this->publicKey = $data['public_key']; - $this->created = $data['created']; - $this->lastRequest = $data['last_request']; + private string $uuid; + private ?string $peerUuid; + private bool $authenticated; + private string $publicKey; + private SessionState $state; + /** + * @var SessionFlags[] + */ + private array $flags; + private DateTime $created; + private ?DateTime $lastRequest; - if(SessionState::tryFrom($data['state']) == null) + /** + * Constructs a new instance using the provided data array. + * + * @param array $data An associative array containing the initialization data, + * which should include keys such as 'uuid', 'peer_uuid', + * 'authenticated', 'public_key', 'created', 'last_request', + * 'flags', and 'state'. + * + * @return void + */ + public function __construct(array $data) { - $this->state = SessionState::CLOSED; - } - else - { - $this->state = SessionState::from($data['state']); - } - } + $this->uuid = $data['uuid']; + $this->peerUuid = $data['peer_uuid'] ?? null; + $this->authenticated = $data['authenticated'] ?? false; + $this->publicKey = $data['public_key']; + $this->created = $data['created']; + $this->lastRequest = $data['last_request']; + $this->flags = Utilities::unserializeList($data['flags']); - public function getUuid(): string - { - return $this->uuid; - } + if(SessionState::tryFrom($data['state']) == null) + { + $this->state = SessionState::CLOSED; + } + else + { + $this->state = SessionState::from($data['state']); + } - public function getPeerUuid(): ?string - { - return $this->peerUuid; - } - - public function isAuthenticated(): bool - { - if($this->peerUuid === null) - { - return false; } - return $this->authenticated; - } + /** + * Retrieves the UUID. + * + * @return string The UUID of the object. + */ + public function getUuid(): string + { + return $this->uuid; + } - public function getPublicKey(): string - { - return $this->publicKey; - } + /** + * Retrieves the UUID of the peer. + * + * @return string|null The UUID of the peer or null if not set. + */ + public function getPeerUuid(): ?string + { + return $this->peerUuid; + } - public function getState(): SessionState - { - return $this->state; - } + /** + * Checks whether the user is authenticated. + * + * @return bool Returns true if the user is authenticated; otherwise, false. + */ + public function isAuthenticated(): bool + { + if($this->peerUuid === null) + { + return false; + } - public function getCreated(): DateTime - { - return $this->created; - } + if(in_array(SessionFlags::AUTHENTICATED, $this->flags)) + { + return true; + } - public function getLastRequest(): ?DateTime - { - return $this->lastRequest; - } + return $this->authenticated; + } - public static function fromArray(array $data): object - { - return new self($data); - } + /** + * Retrieves the public key associated with the instance. + * + * @return string Returns the public key as a string. + */ + public function getPublicKey(): string + { + return $this->publicKey; + } - public function toArray(): array - { - return [ - 'uuid' => $this->uuid, - 'peer_uuid' => $this->peerUuid, - 'authenticated' => $this->authenticated, - 'public_key' => $this->publicKey, - 'state' => $this->state->value, - 'created' => $this->created, - 'last_request' => $this->lastRequest, - ]; - } -} \ No newline at end of file + /** + * Retrieves the current session state. + * + * @return SessionState Returns the current state of the session. + */ + public function getState(): SessionState + { + return $this->state; + } + + /** + * Retrieves the creation date and time of the object. + * + * @return DateTime Returns a DateTime object representing when the object was created. + */ + public function getCreated(): DateTime + { + return $this->created; + } + + /** + * Retrieves the list of flags associated with the current instance. + * + * @return array Returns an array of flags. + */ + public function getFlags(): array + { + return $this->flags; + } + + /** + * Retrieves the timestamp of the last request made. + * + * @return DateTime|null The DateTime object representing the last request time, or null if no request has been made. + */ + public function getLastRequest(): ?DateTime + { + return $this->lastRequest; + } + + /** + * Creates a new instance of the class using the provided array data. + * + * @param array $data An associative array of data used to initialize the object properties. + * @return object Returns a newly created object instance. + */ + public static function fromArray(array $data): object + { + return new self($data); + } + + /** + * Converts the object's properties to an associative array. + * + * @return array An associative array representing the object's data, including keys 'uuid', 'peer_uuid', + * 'authenticated', 'public_key', 'state', 'flags', 'created', and 'last_request'. + */ + public function toArray(): array + { + return [ + 'uuid' => $this->uuid, + 'peer_uuid' => $this->peerUuid, + 'authenticated' => $this->authenticated, + 'public_key' => $this->publicKey, + 'state' => $this->state->value, + 'flags' => Utilities::serializeList($this->flags), + 'created' => $this->created, + 'last_request' => $this->lastRequest, + ]; + } + } \ No newline at end of file diff --git a/src/Socialbox/Objects/Standard/SessionState.php b/src/Socialbox/Objects/Standard/SessionState.php new file mode 100644 index 0000000..67819a5 --- /dev/null +++ b/src/Socialbox/Objects/Standard/SessionState.php @@ -0,0 +1,8 @@ +