Skip to content

Commit

Permalink
DISPLAY-1030: Media provider
Browse files Browse the repository at this point in the history
  • Loading branch information
rimi-itk committed Oct 5, 2023
1 parent a2cef3f commit 57bab60
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions config/api_platform/theme.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ resources:

operations:
ApiPlatform\Metadata\Get:
provider: App\State\ThemeProvider
openapi_context:
description: Retrieves a Theme resource.
summary: Retrieve a Theme resource.
Expand Down
5 changes: 5 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ services:
arguments:
$itemExtensions: !tagged_iterator api_platform.doctrine.orm.query_extension.item

App\State\ThemeProvider:
tags: [ { name: 'api_platform.state_provider', priority: 2 } ]
arguments:
$itemExtensions: !tagged_iterator api_platform.doctrine.orm.query_extension.item

# Enable ULID helper CLI commands.
Symfony\Component\Uid\Command\InspectUlidCommand: ~
Symfony\Component\Uid\Command\InspectUuidCommand: ~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

namespace App\DataProvider;
namespace App\State;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Entity\Tenant\Theme;
use App\Entity\User;
use App\Exceptions\ItemDataProviderException;
Expand All @@ -15,22 +17,36 @@
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Uid\Ulid;

final class ThemeItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
/**
* A Theme state provider.
*
* @see https://api-platform.com/docs/v2.7/core/state-providers/
*
* @template T of Theme
*/
final class ThemeProvider implements ProviderInterface
{
public function __construct(
private Security $security,
private SlideRepository $slideRepository,
private ThemeRepository $themeRepository,
private ValidationUtils $validationUtils,
private iterable $itemExtensions = []
private iterable $itemExtensions
) {}

public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
/**
* {@inheritdoc}
*/
public function provide(Operation $operation, array $uriVariables = [], array $context = [])
{
return Theme::class === $resourceClass;
if ($operation instanceof Get) {
return $this->provideItem(Theme::class, $uriVariables['id'], $operation, $context);
}

return null;
}

public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?Theme
private function provideItem(string $resourceClass, $id, Operation $operation, array $context): ?Theme
{
$user = $this->security->getUser();
if (is_null($user)) {
Expand All @@ -53,8 +69,10 @@ public function getItem(string $resourceClass, $id, string $operationName = null

// Filter the query-builder with tenant extension.
foreach ($this->itemExtensions as $extension) {
$identifiers = ['id' => $id];
$extension->applyToItem($queryBuilder, $queryNameGenerator, $resourceClass, $identifiers, $operationName, $context);
if ($extension instanceof QueryItemExtensionInterface) {
$identifiers = ['id' => $id];
$extension->applyToItem($queryBuilder, $queryNameGenerator, $resourceClass, $identifiers, $operation, $context);
}
}

// Get result. If there is a result this is returned.
Expand Down

0 comments on commit 57bab60

Please sign in to comment.