Skip to content

Commit

Permalink
Make consistent and introduce events
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed May 17, 2024
1 parent 04febb6 commit 76bbe2c
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 54 deletions.
8 changes: 4 additions & 4 deletions config/dependencies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ services:
public: true
tags: [ 'controller.service_arguments' ]

Pimcore\Bundle\StudioBackendBundle\Dependency\RepositoryInterface:
class: Pimcore\Bundle\StudioBackendBundle\Dependency\Repository
Pimcore\Bundle\StudioBackendBundle\Dependency\Repository\DependencyRepositoryInterface:
class: Pimcore\Bundle\StudioBackendBundle\Dependency\Repository\DependencyRepository

Pimcore\Bundle\StudioBackendBundle\Dependency\Service\DependencyHydratorServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Dependency\Service\DependencyHydratorService
Pimcore\Bundle\StudioBackendBundle\Dependency\Service\DependencyServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Dependency\Service\DependencyService

Pimcore\Bundle\StudioBackendBundle\Dependency\Hydrator\DependencyHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Dependency\Hydrator\DependencyHydrator
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query;
namespace Pimcore\Bundle\StudioBackendBundle\Dependency\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
Expand All @@ -34,7 +34,7 @@ public function __construct(
schema: new Schema(
type: 'string',
enum: DependencyMode::cases(),
example: null
example: DependencyMode::REQUIRED_BY->value
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Property;
namespace Pimcore\Bundle\StudioBackendBundle\Dependency\Attributes\Response\Property;

use OpenApi\Attributes\Items;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\Version;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Schema\Dependency;

/**
* @internal
Expand All @@ -34,7 +34,7 @@ public function __construct()
type: 'array',
items: new Items(
anyOf: [
new Schema(ref: Version::class),
new Schema(ref: Dependency::class),
]
)
);
Expand Down
12 changes: 6 additions & 6 deletions src/Dependency/Controller/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Attributes\Parameters\Query\DependencyModeParameter;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Attributes\Response\Property\DependencyCollection;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Request\DependencyParameters;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Service\DependencyHydratorServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\DependencyModeParameter;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Service\DependencyServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\ElementTypeParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\PageParameter;
Expand All @@ -31,7 +32,6 @@
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\NotFoundResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnauthorizedResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnprocessableContentResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Property\DependencyCollection;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
Expand All @@ -51,14 +51,14 @@ final class CollectionController extends AbstractApiController
public function __construct(
SerializerInterface $serializer,
private readonly SecurityServiceInterface $securityService,
private readonly DependencyHydratorServiceInterface $hydratorService,
private readonly DependencyServiceInterface $hydratorService,
) {
parent::__construct($serializer);
}

#[Route('/dependencies', name: 'pimcore_studio_api_dependencies', methods: ['GET'])]
//#[IsGranted('STUDIO_API')]
#[GET(
#[Get(
path: self::API_PATH . '/dependencies',
operationId: 'getDependencies',
description: 'Get paginated dependencies.
Expand All @@ -84,7 +84,7 @@ public function __construct(
#[UnprocessableContentResponse]
public function getDependencies(#[MapQueryString] DependencyParameters $parameters): JsonResponse
{
$result = $this->hydratorService->getHydratedDependencies(
$result = $this->hydratorService->getDependencies(
$parameters,
$this->securityService->getCurrentUser()
);
Expand Down
40 changes: 40 additions & 0 deletions src/Dependency/Event/DependencyEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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\Dependency\Event;

use Pimcore\Bundle\StudioBackendBundle\Dependency\Schema\Dependency;
use Pimcore\Bundle\StudioBackendBundle\Event\AbstractPreResponseEvent;

final class DependencyEvent extends AbstractPreResponseEvent
{
public const EVENT_NAME = 'pre_response.dependency';

public function __construct(
private readonly Dependency $dependency
)
{
parent::__construct($dependency);
}

/**
* Use this to get additional infos out of the response object
*/
public function getDependency(): Dependency
{
return $this->dependency;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Dependency;
namespace Pimcore\Bundle\StudioBackendBundle\Dependency\Repository;

use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\ElementProviderTrait;

/**
* @internal
*/
final readonly class Repository implements RepositoryInterface
final readonly class DependencyRepository implements DependencyRepositoryInterface
{
use ElementProviderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Dependency;
namespace Pimcore\Bundle\StudioBackendBundle\Dependency\Repository;

/**
* @internal
*/
interface RepositoryInterface
interface DependencyRepositoryInterface
{
public function listRequiresDependencies(string $elementType, int $elementId): array;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* @internal
*/
final readonly class ListingResult
final readonly class Collection
{
/**
* @param array $items
Expand Down
16 changes: 10 additions & 6 deletions src/Dependency/Schema/Dependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,28 @@

use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;
use Pimcore\Bundle\StudioBackendBundle\Util\Schema\AdditionalAttributesInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\AdditionalAttributesTrait;

#[Schema(
title: 'Dependency',
type: 'object'
)]
final readonly class Dependency
final class Dependency implements AdditionalAttributesInterface
{
use AdditionalAttributesTrait;

public function __construct(
#[Property(description: 'id', type: 'int')]
private int $id,
private readonly int $id,
#[Property(description: 'path', type: 'string', example: 'text')]
private string $path,
private readonly string $path,
#[Property(description: 'type', type: 'string', example: 'asset')]
private string $type,
private readonly string $type,
#[Property(description: 'subType', type: 'string', example: 'image')]
private string $subType,
private readonly string $subType,
#[Property(description: 'published', type: 'bool', example: 'true')]
private bool $published,
private readonly bool $published,
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,84 +16,91 @@

namespace Pimcore\Bundle\StudioBackendBundle\Dependency\Service;

use Pimcore\Bundle\StudioBackendBundle\Dependency\Event\DependencyEvent;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Hydrator\DependencyHydratorInterface;
use Pimcore\Bundle\StudioBackendBundle\Dependency\RepositoryInterface;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Repository\DependencyRepositoryInterface;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Request\DependencyParameters;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Result\Collection;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\ElementProviderTrait;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Result\ListingResult;
use Pimcore\Model\UserInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

final readonly class DependencyHydratorService implements DependencyHydratorServiceInterface
final readonly class DependencyService implements DependencyServiceInterface
{
use ElementProviderTrait;

public function __construct(
private RepositoryInterface $repository,
private DependencyRepositoryInterface $dependencyRepository,
private DependencyHydratorInterface $dependencyHydrator,
private EventDispatcherInterface $eventDispatcher,
) {
}
public function getHydratedDependencies(
public function getDependencies(
DependencyParameters $parameters,
UserInterface $user
): ListingResult
): Collection
{
return match ($parameters->getMode()) {
DependencyMode::REQUIRES => $this->getHydratedRequiredDependencies($parameters),
DependencyMode::REQUIRED_BY => $this->getHydratedRequiredByDependencies($parameters),
DependencyMode::REQUIRES => $this->getRequiredDependencies($parameters),
DependencyMode::REQUIRED_BY => $this->getRequiredByDependencies($parameters),
};
}

private function hydrateDependencyCollection(array $dependencies): array
private function getDependencyCollection(array $dependencies): array
{
$hydratedDependencies = [];

foreach($dependencies as $dependency) {
$hydratedDependency = $this->dependencyHydrator->hydrate($dependency);
if($hydratedDependency) {
$hydratedDependencies[] = $hydratedDependency;
$dependency = $this->dependencyHydrator->hydrate($dependency);
if($dependency) {
$this->eventDispatcher->dispatch(
new DependencyEvent($dependency),
DependencyEvent::EVENT_NAME
);
$hydratedDependencies[] = $dependency;
}
}

return $hydratedDependencies;
}

private function getHydratedRequiredDependencies(
private function getRequiredDependencies(
DependencyParameters $parameters
): ListingResult {
): Collection {

$dependencies = $this->repository->listRequiresDependencies(
$dependencies = $this->dependencyRepository->listRequiresDependencies(
$parameters->getElementType(),
$parameters->getElementId()
);

$dependencies = $this->hydrateDependencyCollection($dependencies);
$dependencies = $this->getDependencyCollection($dependencies);

return new ListingResult(
return new Collection(
$dependencies,
$parameters->getPage(),
$parameters->getPageSize(),
$this->repository->listRequiresDependenciesTotalCount(
$this->dependencyRepository->listRequiresDependenciesTotalCount(
$parameters->getElementType(),
$parameters->getElementId()
)
);
}

private function getHydratedRequiredByDependencies(
private function getRequiredByDependencies(
DependencyParameters $parameters
): ListingResult {
$dependencies = $this->repository->listRequiredByDependencies(
): Collection {
$dependencies = $this->dependencyRepository->listRequiredByDependencies(
$parameters->getElementType(),
$parameters->getElementId()
);

$dependencies = $this->hydrateDependencyCollection($dependencies);
$dependencies = $this->getDependencyCollection($dependencies);

return new ListingResult(
return new Collection(
$dependencies,
$parameters->getPage(),
$parameters->getPageSize(),
$this->repository->listRequiredByDependenciesTotalCount(
$this->dependencyRepository->listRequiredByDependenciesTotalCount(
$parameters->getElementType(),
$parameters->getElementId()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
namespace Pimcore\Bundle\StudioBackendBundle\Dependency\Service;

use Pimcore\Bundle\StudioBackendBundle\Dependency\Request\DependencyParameters;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Result\ListingResult;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Result\Collection;
use Pimcore\Model\UserInterface;

interface DependencyHydratorServiceInterface
interface DependencyServiceInterface
{
public function getHydratedDependencies(
public function getDependencies(
DependencyParameters $parameters,
UserInterface $user
): ListingResult;
): Collection;
}
1 change: 1 addition & 0 deletions src/Property/Event/PredefinedPropertyEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
final class PredefinedPropertyEvent extends AbstractPreResponseEvent
{
public const EVENT_NAME = 'pre_response.predefined_property';

public function __construct(
private readonly PredefinedProperty $predefinedProperty
)
Expand Down
1 change: 1 addition & 0 deletions src/Version/Hydrator/VersionHydratorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function getHydratedVersions(
$versionObjects = $list->load();
foreach ($versionObjects as $versionObject) {
$versions[] = $this->versionHydrator->hydrate($versionObject, $scheduledTasks);

}

return new ListingResult(
Expand Down

0 comments on commit 76bbe2c

Please sign in to comment.