Skip to content

Commit

Permalink
[Documents] Get Available Sites endpoint (#595)
Browse files Browse the repository at this point in the history
* feat: add endpoint to list available sites

* Apply php-cs-fixer changes

* fix: sonar

* apply review feedback

* Apply php-cs-fixer changes

* fix: renaming issue

---------

Co-authored-by: lukmzig <[email protected]>
  • Loading branch information
lukmzig and lukmzig authored Nov 29, 2024
1 parent 5464233 commit b2b826f
Show file tree
Hide file tree
Showing 16 changed files with 569 additions and 3 deletions.
33 changes: 31 additions & 2 deletions config/documents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@ services:
autoconfigure: true
public: false

# Secvices
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
Pimcore\Bundle\StudioBackendBundle\Document\Controller\:
resource: '../src/Document/Controller'
public: true
tags: [ 'controller.service_arguments' ]

#
# Services
#

Pimcore\Bundle\StudioBackendBundle\Document\Service\DocumentServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Document\Service\DocumentService
class: Pimcore\Bundle\StudioBackendBundle\Document\Service\DocumentService

Pimcore\Bundle\StudioBackendBundle\Document\Service\SiteServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Document\Service\SiteService


#
# Repositories
#

Pimcore\Bundle\StudioBackendBundle\Document\Repository\SiteRepositoryInterface:
class: Pimcore\Bundle\StudioBackendBundle\Document\Repository\SiteRepository


#
# Hydrators
#

Pimcore\Bundle\StudioBackendBundle\Document\Hydrator\SiteHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Document\Hydrator\SiteHydrator
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function __construct(
operationId: 'asset_video_image_thumbnail_stream',
description: 'asset_video_image_thumbnail_stream_description',
summary: 'asset_video_image_thumbnail_stream_summary',
tags: [Tags::Assets->name]
tags: [Tags::Assets->value]
)]
#[IdParameter(type: 'video')]
#[WidthParameter('Width of the video image thumbnail', 265)]
Expand Down
43 changes: 43 additions & 0 deletions src/Document/Attribute/Response/AvailableSitesJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Document\Attribute\Response;

use OpenApi\Attributes\Items;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Property;
use Pimcore\Bundle\StudioBackendBundle\Document\Schema\Site;

/**
* @internal
*/
final class AvailableSitesJson extends JsonContent
{
public function __construct()
{
parent::__construct(
required: ['items'],
properties: [
new Property(
'items',
type: 'array',
items: new Items(ref: Site::class)
),
],
type: 'object',
);
}
}
74 changes: 74 additions & 0 deletions src/Document/Controller/AvailableSitesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Document\Controller;

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Document\Attribute\Response\AvailableSitesJson;
use Pimcore\Bundle\StudioBackendBundle\Document\MappedParameter\ExcludeMainSiteParameter as MappedParameter;
use Pimcore\Bundle\StudioBackendBundle\Document\OpenApi\Attribute\Parameter\Query\ExcludeMainSiteParameter;
use Pimcore\Bundle\StudioBackendBundle\Document\Service\SiteServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\UserPermissions;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class AvailableSitesController extends AbstractApiController
{
public function __construct(
private readonly SiteServiceInterface $siteService,
SerializerInterface $serializer
) {
parent::__construct($serializer);
}

#[Route(
'/documents/sites/list-available',
name: 'pimcore_studio_api_sites_list_available',
methods: ['GET']
)]
#[IsGranted(UserPermissions::DOCUMENTS->value)]
#[Get(
path: self::PREFIX . '/documents/sites/list-available',
operationId: 'documents_list_available_sites',
description: 'documents_list_available_sites_description',
summary: 'documents_list_available_sites_summary',
tags: [Tags::Documents->value]
)]
#[ExcludeMainSiteParameter]
#[SuccessResponse(
description: 'documents_list_available_sites_success_response',
content: new AvailableSitesJson()
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
])]
public function getAvailableSites(
#[MapQueryString] MappedParameter $parameter
): JsonResponse {
return $this->jsonResponse(['items' => $this->siteService->getAvailableSites($parameter)]);
}
}
39 changes: 39 additions & 0 deletions src/Document/Event/PreResponse/SiteEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Document\Event\PreResponse;

use Pimcore\Bundle\StudioBackendBundle\Document\Schema\Site;
use Pimcore\Bundle\StudioBackendBundle\Event\AbstractPreResponseEvent;

final class SiteEvent extends AbstractPreResponseEvent
{
public const EVENT_NAME = 'pre_response.document.sites_list_available';

public function __construct(
private readonly Site $site
) {
parent::__construct($site);
}

/**
* Use this to get additional infos out of the response object
*/
public function getSite(): Site
{
return $this->site;
}
}
37 changes: 37 additions & 0 deletions src/Document/Hydrator/SiteHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Document\Hydrator;

use Pimcore\Bundle\StudioBackendBundle\Document\Schema\Site;
use Pimcore\Model\Site as SiteModel;

/**
* @internal
*/
final class SiteHydrator implements SiteHydratorInterface
{
public function hydrate(SiteModel $siteModel): Site
{
return new Site(
$siteModel->getId(),
$siteModel->getDomains(),
$siteModel->getMainDomain(),
$siteModel->getRootId(),
$siteModel->getRootPath(),
);
}
}
28 changes: 28 additions & 0 deletions src/Document/Hydrator/SiteHydratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Document\Hydrator;

use Pimcore\Bundle\StudioBackendBundle\Document\Schema\Site;
use Pimcore\Model\Site as SiteModel;

/**
* @internal
*/
interface SiteHydratorInterface
{
public function hydrate(SiteModel $siteModel): Site;
}
33 changes: 33 additions & 0 deletions src/Document/MappedParameter/ExcludeMainSiteParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Document\MappedParameter;

/**
* @internal
*/
final readonly class ExcludeMainSiteParameter
{
public function __construct(
private ?string $excludeMainSite = null
) {
}

public function getExcludeMainSite(): bool
{
return $this->excludeMainSite === 'true'; // TODO: symfony 7.1 will support bool type
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Document\OpenApi\Attribute\Parameter\Query;

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

#[Attribute(Attribute::TARGET_METHOD)]
final class ExcludeMainSiteParameter extends QueryParameter
{
public function __construct()
{
parent::__construct(
name: 'excludeMainSite',
description: 'Exclude main site from the list',
in: 'query',
schema: new Schema(type: 'boolean', example: false),
);
}
}
34 changes: 34 additions & 0 deletions src/Document/Repository/SiteRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Document\Repository;

use Pimcore\Model\Site;
use Pimcore\Model\Site\Listing;

/**
* @internal
*/
final class SiteRepository implements SiteRepositoryInterface
{
/**
* @return Site[]
*/
public function listSites(): array
{
return (new Listing())->load();
}
}
Loading

0 comments on commit b2b826f

Please sign in to comment.