Skip to content

Commit

Permalink
Restructure properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed May 7, 2024
1 parent 5971fc6 commit 74c8ef6
Show file tree
Hide file tree
Showing 17 changed files with 299 additions and 40 deletions.
4 changes: 2 additions & 2 deletions config/properties.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ services:
Pimcore\Bundle\StudioBackendBundle\Property\Service\PropertyServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Property\Service\PropertyService

Pimcore\Bundle\StudioBackendBundle\Property\Hydrator\DataPropertyHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Property\Hydrator\DataPropertyHydrator
Pimcore\Bundle\StudioBackendBundle\Property\Hydrator\ElementPropertyHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Property\Hydrator\ElementPropertyHydrator

Pimcore\Bundle\StudioBackendBundle\Property\Hydrator\PredefinedPropertyHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Property\Hydrator\PredefinedPropertyHydrator
Expand Down
38 changes: 38 additions & 0 deletions src/OpenApi/Attributes/Request/ElementPropertyRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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\OpenApi\Attributes\Request;

use Attribute;
use OpenApi\Attributes\Items;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioBackendBundle\Property\Schema\UpdateElementProperty;

#[Attribute(Attribute::TARGET_METHOD)]
final class ElementPropertyRequestBody extends RequestBody
{
public function __construct()
{
parent::__construct(
required: true,
content: new JsonContent(
type: 'array',
items: new Items(ref: UpdateElementProperty::class)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Pimcore\Bundle\StudioBackendBundle\Property\Schema\UpdatePredefinedProperty;

#[Attribute(Attribute::TARGET_METHOD)]
final class PropertyRequestBody extends RequestBody
final class PredefinedPropertyRequestBody extends RequestBody
{
public function __construct()
{
Expand Down
21 changes: 13 additions & 8 deletions src/OpenApi/Config/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
)]
#[Tag(
name: Tags::Properties->name,
description: 'Get properties for elements'
description: 'Property operations to get/update/create/delete properties'
)]
#[Tag(
name: Tags::PropertiesForElement->value,
description: 'Property operations to get/update properties for an element'
)]
#[Tag(
name: Tags::Translation->name,
Expand All @@ -45,12 +49,13 @@
name: Tags::Versions->name,
description: 'Versions operations to get/list/publish/delete and cleanup versions'
)]
enum Tags
enum Tags: string
{
case Assets;
case Authorization;
case DataObjects;
case Properties;
case Translation;
case Versions;
case Assets = 'Assets';
case Authorization = 'Authorization';
case DataObjects = 'DataObjects';
case Properties = 'Properties';
case PropertiesForElement = 'Properties for Element';
case Translation = 'Translation';
case Versions = 'Versions';
}
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\Property\Controller;
namespace Pimcore\Bundle\StudioBackendBundle\Property\Controller\Element;

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
Expand All @@ -28,7 +28,7 @@
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnsupportedMediaTypeResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Property\Schema\DataProperty;
use Pimcore\Bundle\StudioBackendBundle\Property\Schema\ElementProperty;
use Pimcore\Bundle\StudioBackendBundle\Property\Service\PropertyHydratorServiceInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
Expand All @@ -46,20 +46,20 @@ public function __construct(
parent::__construct($serializer);
}

#[Route('/properties/{elementType}/{id}', name: 'pimcore_studio_api_get_properties', methods: ['GET'])]
#[Route('/properties/{elementType}/{id}', name: 'pimcore_studio_api_get_element_properties', methods: ['GET'])]
//#[IsGranted('STUDIO_API')]
#[Get(
path: self::API_PATH . '/properties/{elementType}/{id}',
operationId: 'getPropertiesByTypeAndId',
operationId: 'getPropertiesForElementByTypeAndId',
summary: 'Get properties for an element based on the element type and the element id',
security: self::SECURITY_SCHEME,
tags: [Tags::Properties->name]
tags: [Tags::PropertiesForElement->value]
)]
#[ElementTypeParameter]
#[IdParameter(type: 'element')]
#[SuccessResponse(
description: 'Element Properties data as json',
content: new ItemsJson(DataProperty::class)
content: new ItemsJson(ElementProperty::class)
)]
#[UnauthorizedResponse]
#[NotFoundResponse]
Expand Down
84 changes: 84 additions & 0 deletions src/Property/Controller/Element/PutController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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\Property\Controller\Element;

use OpenApi\Attributes\Put;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\ElementTypeParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Request\ElementPropertyRequestBody;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\ItemsJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\MethodNotAllowedResponse;
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\Error\UnsupportedMediaTypeResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Property\Request\UpdateElementProperties;
use Pimcore\Bundle\StudioBackendBundle\Property\Schema\ElementProperty;
use Pimcore\Bundle\StudioBackendBundle\Property\Service\PropertyHydratorServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Property\Service\PropertyServiceInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class PutController extends AbstractApiController

Check notice on line 44 in src/Property/Controller/Element/PutController.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Efferent coupling between objects

\[EA\] High efferent coupling (24).

Check notice on line 44 in src/Property/Controller/Element/PutController.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Efferent coupling between objects

\[EA\] High efferent coupling (24).
{
public function __construct(
SerializerInterface $serializer,
private readonly PropertyServiceInterface $propertyService,
private readonly PropertyHydratorServiceInterface $hydratorService,
) {
parent::__construct($serializer);
}

#[Route('/properties/{elementType}/{id}', name: 'pimcore_studio_api_update_element_properties', methods: ['PUT'])]
//#[IsGranted('STUDIO_API')]
#[Put(
path: self::API_PATH . '/properties/{elementType}/{id}',
operationId: 'updatePropertiesForElementByTypeAndId',
summary: 'Update properties for an element based on the element type and the element id',
security: self::SECURITY_SCHEME,
tags: [Tags::PropertiesForElement->value]
)]
#[ElementTypeParameter]
#[IdParameter(type: 'element')]
#[ElementPropertyRequestBody]
#[SuccessResponse(
description: 'Element Properties data as json',
content: new ItemsJson(ElementProperty::class)
)]
#[UnauthorizedResponse]
#[NotFoundResponse]
#[MethodNotAllowedResponse]
#[UnsupportedMediaTypeResponse]
#[UnprocessableContentResponse]
public function updateProperties(
string $elementType,
int $id,
#[MapRequestPayload] UpdateElementProperties $items
): JsonResponse
{
$this->propertyService->updateElementProperties($elementType, $id, $items);
return $this->jsonResponse($this->hydratorService->getHydratedPropertyForElement($elementType, $id));
}
}
4 changes: 2 additions & 2 deletions src/Property/Controller/PutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\PropertyNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Request\PropertyRequestBody;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Request\PredefinedPropertyRequestBody;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\BadRequestResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\MethodNotAllowedResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnauthorizedResponse;
Expand Down Expand Up @@ -61,7 +61,7 @@ public function __construct(
tags: [Tags::Properties->name]
)]
#[IdParameter(type: 'property', schema: new Schema(type: 'string', example: 'alpha-numerical'))]
#[PropertyRequestBody]
#[PredefinedPropertyRequestBody]
#[SuccessResponse(
description: 'Updated property',
content: new JsonContent(ref: PredefinedProperty::class, type: 'object')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@
namespace Pimcore\Bundle\StudioBackendBundle\Property\Hydrator;

use Pimcore\Bundle\StudioBackendBundle\Property\Extractor\PropertyDataExtractorInterface;
use Pimcore\Bundle\StudioBackendBundle\Property\Schema\DataProperty;
use Pimcore\Bundle\StudioBackendBundle\Property\Schema\ElementProperty;
use Pimcore\Model\Property;

/**
* @internal
*/
final readonly class DataPropertyHydrator implements DataPropertyHydratorInterface
final readonly class ElementPropertyHydrator implements ElementPropertyHydratorInterface
{
public function __construct(
private PropertyDataExtractorInterface $dataExtractor
) {
}

public function hydrate(Property $property): DataProperty
public function hydrate(Property $property): ElementProperty
{
$propertyData = $this->dataExtractor->extractData($property);

return new DataProperty(
return new ElementProperty(
$propertyData['name'],
$propertyData['modelData'] ?? $propertyData['data'],
$propertyData['type'],
$propertyData['inheritable'],
$propertyData['inherited'],
$propertyData['config'],
$propertyData['predefinedName'],
$propertyData['predefinedName'] ?? 'Custom',
$propertyData['description']
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

namespace Pimcore\Bundle\StudioBackendBundle\Property\Hydrator;

use Pimcore\Bundle\StudioBackendBundle\Property\Schema\DataProperty;
use Pimcore\Bundle\StudioBackendBundle\Property\Schema\ElementProperty;
use Pimcore\Model\Property;

/**
* @internal
*/
interface DataPropertyHydratorInterface
interface ElementPropertyHydratorInterface
{
public function hydrate(Property $property): DataProperty;
public function hydrate(Property $property): ElementProperty;
}
45 changes: 45 additions & 0 deletions src/Property/Request/UpdateElementProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\Property\Request;


use Pimcore\Bundle\StudioBackendBundle\Property\Schema\UpdateElementProperty;

/**
* @internal
*/
final readonly class UpdateElementProperties
{
private array $properties;
public function __construct(
array $items
) {
$this->properties = array_map(static function(array $propertyData) {
return new UpdateElementProperty(
$propertyData['key'],
$propertyData['data'],
$propertyData['type'],
$propertyData['inheritable'],
);
}, $items);
}

public function getProperties(): array
{
return $this->properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
title: 'DataProperty',
type: 'object'
)]
final readonly class DataProperty
final readonly class ElementProperty
{
public function __construct(
#[Property(description: 'name', type: 'string', example: 'Mister Proper')]
private string $name,
#[Property(description: 'key', type: 'string', example: 'key_of_the_property')]
private string $key,
#[Property(description: 'data', type: 'mixed', example: '123')]
private mixed $data,
#[Property(description: 'type', type: 'string', example: 'document')]
Expand All @@ -45,9 +45,9 @@ public function __construct(
) {
}

public function getName(): string
public function getKey(): string
{
return $this->name;
return $this->key;
}

public function getData(): mixed
Expand All @@ -60,12 +60,12 @@ public function getType(): string
return $this->type;
}

public function getIsInheritable(): bool
public function isInheritable(): bool
{
return $this->inheritable;
}

public function getIsInherited(): bool
public function isInherited(): bool
{
return $this->inherited;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Property/Schema/PredefinedProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getCtype(): string
return $this->ctype;
}

public function getInheritable(): bool
public function isInheritable(): bool
{
return $this->inheritable;
}
Expand Down
Loading

0 comments on commit 74c8ef6

Please sign in to comment.