Skip to content

Commit

Permalink
Make use of attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Apr 10, 2024
1 parent e1edd48 commit 9bb57ca
Show file tree
Hide file tree
Showing 17 changed files with 227 additions and 168 deletions.
22 changes: 22 additions & 0 deletions src/Attributes/Parameters/Path/IdParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Path;

use Attribute;
use OpenApi\Attributes\PathParameter;
use OpenApi\Attributes\Schema;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class IdParameter extends PathParameter
{
public function __construct(string $type = 'element')
{
parent::__construct(
name: 'id',
description: 'Id of the ' . $type,
in: 'path',
required: true,
schema: new Schema(type: 'integer', example: 83),
);
}
}
22 changes: 22 additions & 0 deletions src/Attributes/Parameters/Query/LimitParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Schema;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class LimitParameter extends QueryParameter
{
public function __construct()
{
parent::__construct(
name: 'limit',
description: 'Number of items per page',
in: 'query',
required: true,
schema: new Schema(type: 'integer', example: 10),
);
}
}
22 changes: 22 additions & 0 deletions src/Attributes/Parameters/Query/PageParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Schema;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class PageParameter extends QueryParameter
{
public function __construct()
{
parent::__construct(
name: 'page',
description: 'Page number',
in: 'query',
required: true,
schema: new Schema(type: 'integer', example: 1),
);
}
}
20 changes: 20 additions & 0 deletions src/Attributes/Request/CredentialsRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Pimcore\Bundle\StudioApiBundle\Attributes\Request;

use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioApiBundle\Dto\Credentials;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class CredentialsRequestBody extends RequestBody
{
public function __construct()
{
parent::__construct(
required: true,
content: new JsonContent(ref: Credentials::class)
);
}
}
20 changes: 20 additions & 0 deletions src/Attributes/Request/TokenRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Pimcore\Bundle\StudioApiBundle\Attributes\Request;

use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioApiBundle\Dto\Token\Refresh;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class TokenRequestBody extends RequestBody
{
public function __construct()
{
parent::__construct(
required: true,
content: new JsonContent(ref: Refresh::class)
);
}
}
20 changes: 20 additions & 0 deletions src/Attributes/Request/TranslationRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Pimcore\Bundle\StudioApiBundle\Attributes\Request;

use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioApiBundle\Dto\Translation;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class TranslationRequestBody extends RequestBody
{
public function __construct()
{
parent::__construct(
required: true,
content: new JsonContent(ref: Translation::class)
);
}
}
19 changes: 19 additions & 0 deletions src/Attributes/Response/SuccessResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Pimcore\Bundle\StudioApiBundle\Attributes\Response;

use Attribute;
use OpenApi\Attributes\Response;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class SuccessResponse extends Response
{
public function __construct(string $description = 'Success', mixed $content = null)
{
parent::__construct(
response: 200,
description: $description,
content: $content
);
}
}
21 changes: 21 additions & 0 deletions src/Attributes/Response/UnauthorizedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Pimcore\Bundle\StudioApiBundle\Attributes\Response;

use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Response;
use Pimcore\Bundle\StudioApiBundle\Dto\Unauthorized;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class UnauthorizedResponse extends Response
{
public function __construct()
{
parent::__construct(
response: 401,
description: 'Unauthorized',
content: new JsonContent(ref: Unauthorized::class)
);
}
}
52 changes: 12 additions & 40 deletions src/Controller/Api/Assets/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
namespace Pimcore\Bundle\StudioApiBundle\Controller\Api\Assets;

use OpenApi\Attributes\Get;
use OpenApi\Attributes\MediaType;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Response;
use OpenApi\Attributes\Schema;
use OpenApi\Attributes\JsonContent;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\LimitParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\PageParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioApiBundle\Attributes\Response\UnauthorizedResponse;
use Pimcore\Bundle\StudioApiBundle\Config\Parameters\Query\CollectionParameters;
use Pimcore\Bundle\StudioApiBundle\Controller\Api\AbstractApiController;
use Pimcore\Bundle\StudioApiBundle\Controller\Trait\PaginatedResponseTrait;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Dto\Unauthorized;
use Pimcore\Bundle\StudioApiBundle\Service\AssetSearchServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQuery;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProviderInterface;
Expand Down Expand Up @@ -58,44 +59,15 @@ public function __construct(
'auth_token' => [],
],
],
tags: ['Assets']
tags: ['Assets'],
)]
#[QueryParameter(
name: 'page',
description: 'Page number',
in: 'query',
required: true,
schema: new Schema(type: 'integer', example: 1),
example: 1
)]
#[QueryParameter(
name: 'limit',
description: 'Number of items per page',
in: 'query',
required: true,
schema: new Schema(type: 'integer', example: 1),
example: 10
)]
#[Response(
response: 200,
#[PageParameter]
#[LimitParameter]
#[SuccessResponse(
description: 'Paginated assets with total count as header param',
content:[
new MediaType(
mediaType: 'application/json',
schema: new Schema(ref: Asset::class, type: 'object')
),
]
)]
#[Response(
response: 401,
description: 'Unauthorized',
content:[
new MediaType(
mediaType: 'application/json',
schema: new Schema(ref: Unauthorized::class, type: 'object')
),
]
content: new JsonContent(ref: Asset::class)
)]
#[UnauthorizedResponse]
public function getAssets(#[MapQueryString] Collection $collection): JsonResponse
{

Expand Down
39 changes: 8 additions & 31 deletions src/Controller/Api/Assets/GetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
namespace Pimcore\Bundle\StudioApiBundle\Controller\Api\Assets;

use OpenApi\Attributes\Get;
use OpenApi\Attributes\MediaType;
use OpenApi\Attributes\PathParameter;
use OpenApi\Attributes\Response;
use OpenApi\Attributes\Schema;
use OpenApi\Attributes\JsonContent;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioApiBundle\Attributes\Response\UnauthorizedResponse;
use Pimcore\Bundle\StudioApiBundle\Controller\Api\AbstractApiController;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Dto\Unauthorized;
use Pimcore\Bundle\StudioApiBundle\Service\AssetSearchServiceInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
Expand Down Expand Up @@ -52,34 +51,12 @@ public function __construct(
],
tags: ['Assets']
)]
#[PathParameter(
name: 'id',
description: 'Id of the asset',
in: 'path',
required: true,
schema: new Schema(type: 'integer', example: 83),
example: 83
)]
#[Response(
response: 200,
#[IdParameter(type: 'asset')]
#[SuccessResponse(
description: 'Paginated assets with total count as header param',
content:[
new MediaType(
mediaType: 'application/json',
schema: new Schema(ref: Asset::class, type: 'object')
),
]
)]
#[Response(
response: 401,
description: 'Unauthorized',
content:[
new MediaType(
mediaType: 'application/json',
schema: new Schema(ref: Unauthorized::class, type: 'object')
),
]
content: new JsonContent(ref: Asset::class)
)]
#[UnauthorizedResponse]
public function getAssets(int $id): JsonResponse
{
return $this->jsonResponse($this->assetSearchService->getAssetById($id));
Expand Down
Loading

0 comments on commit 9bb57ca

Please sign in to comment.