Skip to content
Snippets Groups Projects
Commit d49b8519 authored by Tobias Gross-Vogt's avatar Tobias Gross-Vogt
Browse files

localdata files moved to LocalData dir/namespace, added check for valid shortName attribute

parent ad6f2efb
No related branches found
No related tags found
No related merge requests found
Pipeline #102695 passed
...@@ -2,11 +2,9 @@ ...@@ -2,11 +2,9 @@
declare(strict_types=1); declare(strict_types=1);
namespace Dbp\Relay\CoreBundle\Event; namespace Dbp\Relay\CoreBundle\LocalData;
use Dbp\Relay\CoreBundle\Entity\LocalDataAwareInterface;
use Dbp\Relay\CoreBundle\Exception\ApiError; use Dbp\Relay\CoreBundle\Exception\ApiError;
use Dbp\Relay\CoreBundle\Service\LocalDataAwareEventDispatcher;
use Symfony\Contracts\EventDispatcher\Event; use Symfony\Contracts\EventDispatcher\Event;
class LocalDataAwareEvent extends Event class LocalDataAwareEvent extends Event
......
...@@ -2,11 +2,10 @@ ...@@ -2,11 +2,10 @@
declare(strict_types=1); declare(strict_types=1);
namespace Dbp\Relay\CoreBundle\Service; namespace Dbp\Relay\CoreBundle\LocalData;
use ApiPlatform\Core\Exception\ResourceClassNotFoundException; use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
use ApiPlatform\Core\Metadata\Resource\Factory\AnnotationResourceMetadataFactory; use ApiPlatform\Core\Metadata\Resource\Factory\AnnotationResourceMetadataFactory;
use Dbp\Relay\CoreBundle\Event\LocalDataAwareEvent;
use Dbp\Relay\CoreBundle\Exception\ApiError; use Dbp\Relay\CoreBundle\Exception\ApiError;
use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
...@@ -23,14 +22,10 @@ class LocalDataAwareEventDispatcher ...@@ -23,14 +22,10 @@ class LocalDataAwareEventDispatcher
/** @var EventDispatcherInterface */ /** @var EventDispatcherInterface */
private $eventDispatcher; private $eventDispatcher;
/** @var string */ public function __construct(string $resourceClass, EventDispatcherInterface $eventDispatcher)
private $eventName;
public function __construct(string $resourceClass, EventDispatcherInterface $eventDispatcher, string $eventName)
{ {
$this->uniqueEntityName = self::getUniqueEntityName($resourceClass); $this->uniqueEntityName = self::getUniqueEntityName($resourceClass);
$this->eventDispatcher = $eventDispatcher; $this->eventDispatcher = $eventDispatcher;
$this->eventName = $eventName;
} }
/** /**
...@@ -65,11 +60,11 @@ class LocalDataAwareEventDispatcher ...@@ -65,11 +60,11 @@ class LocalDataAwareEventDispatcher
/** /**
* Dispatches the given event. * Dispatches the given event.
*/ */
public function dispatch(LocalDataAwareEvent $event): void public function dispatch(LocalDataAwareEvent $event, string $eventName): void
{ {
$event->setRequestedAttributes($this->requestedAttributes); $event->setRequestedAttributes($this->requestedAttributes);
$this->eventDispatcher->dispatch($event, $this->eventName); $this->eventDispatcher->dispatch($event, $eventName);
$remainingLocalDataAttributes = $event->getRemainingRequestedAttributes(); $remainingLocalDataAttributes = $event->getRemainingRequestedAttributes();
if (!empty($remainingLocalDataAttributes)) { if (!empty($remainingLocalDataAttributes)) {
...@@ -95,6 +90,8 @@ class LocalDataAwareEventDispatcher ...@@ -95,6 +90,8 @@ class LocalDataAwareEventDispatcher
$uniqueName = $resourceMetadata->getShortName() ?? ''; $uniqueName = $resourceMetadata->getShortName() ?? '';
if (empty($uniqueName)) { if (empty($uniqueName)) {
throw new ApiError(500, sprintf("'shortName' attribute missing in ApiResource annotation of resource class '%s'", $resourceClass)); throw new ApiError(500, sprintf("'shortName' attribute missing in ApiResource annotation of resource class '%s'", $resourceClass));
} elseif (str_contains($uniqueName, '.') || str_contains($uniqueName, ',')) {
throw new ApiError(500, sprintf("'shortName' attribute of resource class '%s' must not contain '.' or ',' characters: '%s'", $resourceClass, $uniqueName));
} }
return $uniqueName; return $uniqueName;
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
namespace Dbp\Relay\CoreBundle\Entity; namespace Dbp\Relay\CoreBundle\LocalData;
interface LocalDataAwareInterface interface LocalDataAwareInterface
{ {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
namespace Dbp\Relay\CoreBundle\Entity; namespace Dbp\Relay\CoreBundle\LocalData;
use ApiPlatform\Core\Annotation\ApiProperty; use ApiPlatform\Core\Annotation\ApiProperty;
use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Annotation\Groups;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment