Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing array payload and criteria #115

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
parameters:
level: 7
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
- identifier: missingType.generics
paths:
- src
- tests
Expand Down
3 changes: 3 additions & 0 deletions src/Attribute/Since.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Attribute;

/**
* @internal
*/
#[Attribute]
class Since
{
Expand Down
3 changes: 3 additions & 0 deletions src/Attribute/Until.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Attribute;

/**
* @internal
*/
#[Attribute]
class Until
{
Expand Down
3 changes: 3 additions & 0 deletions src/Exception/JsonDecodeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Exception;

/**
* @internal
*/
class JsonDecodeException extends Exception
{
}
3 changes: 3 additions & 0 deletions src/Exception/JsonEncodeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Exception;

/**
* @internal
*/
class JsonEncodeException extends Exception
{
}
3 changes: 3 additions & 0 deletions src/Exception/PropertyDoesNotExistException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Exception;

/**
* @internal
*/
class PropertyDoesNotExistException extends Exception
{
}
3 changes: 3 additions & 0 deletions src/Exception/ReadOnlyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Exception;

/**
* @internal
*/
class ReadOnlyException extends Exception
{
}
3 changes: 3 additions & 0 deletions src/Exception/SerializerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Exception;

/**
* @internal
*/
class SerializerException extends Exception
{
}
3 changes: 3 additions & 0 deletions src/Exception/TokenStorageException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Fschmtt\Keycloak\Exception;

/**
* @internal
*/
class TokenStorageException extends \RuntimeException
{
}
3 changes: 3 additions & 0 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Lcobucci\JWT\Token;
use Psr\Http\Message\ResponseInterface;

/**
* @internal
*/
class Client
{
public function __construct(
Expand Down
25 changes: 22 additions & 3 deletions src/Http/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
use Fschmtt\Keycloak\Collection\Collection;
use Fschmtt\Keycloak\Representation\Representation;

/**
* @internal
*/
class Command
{
public function __construct(
private readonly string $path,
private readonly Method $method,
/** @var array<string, string> */
private readonly array $parameters = [],
private readonly Representation|Collection|null $payload = null,
/** @var Representation|Collection|array<mixed>|null */
private readonly Representation|Collection|array|null $payload = null,
private readonly ?Criteria $criteria = null,
) {
}

Expand All @@ -32,15 +37,29 @@

$values = array_values($this->parameters);

return str_replace(
$path = str_replace(
$placeholders,
$values,
$this->path
);

return $path . $this->getQuery();
}

public function getPayload(): Representation|Collection|null
/**
* @return Representation|Collection|array<mixed>|null
*/
public function getPayload(): Representation|Collection|array|null
{
return $this->payload;
}

public function getQuery(): string
{
if (!$this->criteria) {
return '';
}

return '?' . http_build_query($this->criteria->jsonSerialize());

Check warning on line 63 in src/Http/Command.php

View check run for this annotation

Codecov / codecov/patch

src/Http/Command.php#L63

Added line #L63 was not covered by tests
}
}
17 changes: 13 additions & 4 deletions src/Http/CommandExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Fschmtt\Keycloak\Json\JsonEncoder;
use Fschmtt\Keycloak\Representation\Representation;

/**
* @internal
*/
class CommandExecutor
{
public function __construct(
Expand All @@ -31,19 +34,25 @@

protected function prepareBody(Command $command): ?string
{
if ($command->getPayload() === null) {
$payload = $command->getPayload();

if ($payload === null) {
return null;
}

$jsonEncoder = new JsonEncoder();

if ($command->getPayload() instanceof Representation) {
return $jsonEncoder->encode($this->propertyFilter->filter($command->getPayload()));
if (is_array($payload)) {
return $jsonEncoder->encode($payload);

Check warning on line 46 in src/Http/CommandExecutor.php

View check run for this annotation

Codecov / codecov/patch

src/Http/CommandExecutor.php#L46

Added line #L46 was not covered by tests
}

if ($payload instanceof Representation) {
return $jsonEncoder->encode($this->propertyFilter->filter($payload));
}

$representations = [];

foreach ($command->getPayload() as $representation) {
foreach ($payload as $representation) {
$representations[] = $this->propertyFilter->filter($representation);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Http/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use DateTimeInterface;
use Stringable;

/**
* @internal
*/
class Criteria
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Fschmtt\Keycloak\Http;

/**
* @internal
*/
enum Method: string
{
case GET = 'GET';
Expand Down
3 changes: 3 additions & 0 deletions src/Http/PropertyFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Fschmtt\Keycloak\Representation\Representation;
use ReflectionClass;

/**
* @internal
*/
class PropertyFilter
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Fschmtt\Keycloak\Http;

/**
* @internal
*/
class Query
{
public function __construct(
Expand Down
3 changes: 3 additions & 0 deletions src/Http/QueryExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Fschmtt\Keycloak\Json\JsonDecoder;
use Fschmtt\Keycloak\Serializer\Serializer;

/**
* @internal
*/
class QueryExecutor
{
public function __construct(
Expand Down
3 changes: 3 additions & 0 deletions src/Json/JsonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Fschmtt\Keycloak\Exception\JsonDecodeException;
use JsonException;

/**
* @internal
*/
class JsonDecoder
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Json/JsonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Fschmtt\Keycloak\Exception\JsonEncodeException;
use JsonException;

/**
* @internal
*/
class JsonEncoder
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/OAuth/TokenStorage/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Token;

/**
* @internal
*/
class Filesystem implements TokenStorageInterface
{
private string $accessTokenPath;
Expand Down
3 changes: 3 additions & 0 deletions src/OAuth/TokenStorage/InMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Fschmtt\Keycloak\OAuth\TokenStorageInterface;
use Lcobucci\JWT\Token;

/**
* @internal
*/
class InMemory implements TokenStorageInterface
{
private ?Token $accessToken = null;
Expand Down
13 changes: 11 additions & 2 deletions src/Resource/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,25 @@ public function removeRealmRoles(string $realm, string $userId, RoleCollection $
);
}

public function executeActionsEmail(string $realm, string $userId): void
/**
* @param list<string>|null $actions
*/
public function executeActionsEmail(string $realm, string $userId, ?array $actions = null, ?Criteria $criteria = null): void
{
$payload = $actions ? [
'actions' => $actions,
] : null;

fschmtt marked this conversation as resolved.
Show resolved Hide resolved
$this->commandExecutor->executeCommand(
new Command(
'/admin/realms/{realm}/users/{userId}/execute-actions-email',
Method::PUT,
[
'realm' => $realm,
'userId' => $userId,
]
],
$payload,
$criteria,
)
);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Serializer/ArraySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Fschmtt\Keycloak\Serializer;

/**
* @internal
*/
class ArraySerializer implements SerializerInterface
{
public function serializes(): string
Expand Down
3 changes: 3 additions & 0 deletions src/Serializer/BooleanSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Fschmtt\Keycloak\Serializer;

/**
* @internal
*/
class BooleanSerializer implements SerializerInterface
{
public function serializes(): string
Expand Down
3 changes: 3 additions & 0 deletions src/Serializer/CollectionSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Fschmtt\Keycloak\Collection\Collection;
use Fschmtt\Keycloak\Representation\Representation;

/**
* @internal
*/
class CollectionSerializer implements SerializerInterface
{
public function serializes(): string
Expand Down
3 changes: 3 additions & 0 deletions src/Serializer/EnumSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use BackedEnum;
use Fschmtt\Keycloak\Enum\Enum;

/**
* @internal
*/
class EnumSerializer implements SerializerInterface
{
public function serializes(): string
Expand Down
3 changes: 3 additions & 0 deletions src/Serializer/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Fschmtt\Keycloak\Serializer;

/**
* @internal
*/
class Factory
{
public function create(): Serializer
Expand Down
3 changes: 3 additions & 0 deletions src/Serializer/IntegerSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Fschmtt\Keycloak\Serializer;

/**
* @internal
*/
class IntegerSerializer implements SerializerInterface
{
public function serializes(): string
Expand Down
3 changes: 3 additions & 0 deletions src/Serializer/MapSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Fschmtt\Keycloak\Type\Map;

/**
* @internal
*/
class MapSerializer implements SerializerInterface
{
public function serializes(): string
Expand Down
3 changes: 3 additions & 0 deletions src/Serializer/RepresentationSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Fschmtt\Keycloak\Representation\Representation;

/**
* @internal
*/
class RepresentationSerializer implements SerializerInterface
{
public function serializes(): string
Expand Down
3 changes: 3 additions & 0 deletions src/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Fschmtt\Keycloak\Exception\SerializerException;
use Fschmtt\Keycloak\Representation\Representation;

/**
* @internal
*/
class Serializer implements SerializerInterface
{
private const NATIVE_TYPES = [
Expand Down
Loading