Skip to content

Commit

Permalink
Merge pull request #224 from os2display/feature/2829-add-feedsource-p…
Browse files Browse the repository at this point in the history
…ost-put-delete-functionality2

2829: Adjustment
  • Loading branch information
tuj authored Nov 20, 2024
2 parents 936bb02 + ab60308 commit 7346b55
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 81 deletions.
24 changes: 0 additions & 24 deletions public/api-spec-v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -8144,12 +8144,6 @@
"feedType": {
"type": "string"
},
"secrets": {
"type": "array",
"items": {
"type": "string"
}
},
"feeds": {
"type": "array",
"items": {
Expand Down Expand Up @@ -8207,12 +8201,6 @@
"feedType": {
"type": "string"
},
"secrets": {
"type": "array",
"items": {
"type": "string"
}
},
"feeds": {
"type": "array",
"items": {
Expand Down Expand Up @@ -8305,12 +8293,6 @@
"feedType": {
"type": "string"
},
"secrets": {
"type": "array",
"items": {
"type": "string"
}
},
"feeds": {
"type": "array",
"items": {
Expand Down Expand Up @@ -8481,12 +8463,6 @@
"feedType": {
"type": "string"
},
"secrets": {
"type": "array",
"items": {
"type": "string"
}
},
"feeds": {
"type": "array",
"items": {
Expand Down
16 changes: 0 additions & 16 deletions public/api-spec-v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5766,10 +5766,6 @@ components:
type: string
feedType:
type: string
secrets:
type: array
items:
type: string
feeds:
type: array
items:
Expand Down Expand Up @@ -5810,10 +5806,6 @@ components:
type: string
feedType:
type: string
secrets:
type: array
items:
type: string
feeds:
type: array
items:
Expand Down Expand Up @@ -5877,10 +5869,6 @@ components:
type: string
feedType:
type: string
secrets:
type: array
items:
type: string
feeds:
type: array
items:
Expand Down Expand Up @@ -5997,10 +5985,6 @@ components:
type: string
feedType:
type: string
secrets:
type: array
items:
type: string
feeds:
type: array
items:
Expand Down
1 change: 0 additions & 1 deletion src/Dto/FeedSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class FeedSource
public string $description = '';
public string $outputType = '';
public string $feedType = '';
public array $secrets = [];
public array $feeds = [];
public array $admin = [];
public string $supportedFeedOutputType = '';
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Tenant/FeedSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function setSupportedFeedOutputType(string $supportedFeedOutputType): sel
*
* @return array The JSON schema definition
*/
public function getSchema(): array
public static function getSchema(): array
{
return [
'$schema' => 'https://json-schema.org/draft/2020-12/schema',
Expand Down
3 changes: 1 addition & 2 deletions src/Feed/EventDatabaseApiFeedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,7 @@ public function getSchema(): array
'properties' => [
'host' => [
'type' => 'string',
'format' => 'url',
'pattern' => 'https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-zA-Z0-9()]{2,6}\\b([-a-zA-Z0-9()@:%_\\+~#?&//=]*)',
'format' => 'uri',
],
],
'required' => ['host'],
Expand Down
50 changes: 18 additions & 32 deletions src/State/FeedSourceProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface;
use App\Dto\FeedSourceInput;
use App\Entity\Interfaces\TenantScopedUserInterface;
use App\Entity\Tenant\FeedSource;
use App\Exceptions\UnknownFeedTypeException;
Expand Down Expand Up @@ -50,7 +49,6 @@ public function process($data, Operation $operation, array $uriVariables = [], a

/**
* @throws UnknownFeedTypeException
* @throws \JsonException
*/
protected function fromInput(mixed $object, Operation $operation, array $uriVariables, array $context): FeedSource
{
Expand All @@ -61,6 +59,10 @@ protected function fromInput(mixed $object, Operation $operation, array $uriVari
throw new InvalidArgumentException('object must by of type FeedSource');
}

// Validate feed source
$this->validateFeedSource($object, $operation);

// Update properties.
$this->updateFeedSourceProperties($feedSource, $object);

// Set tenant
Expand All @@ -70,48 +72,35 @@ protected function fromInput(mixed $object, Operation $operation, array $uriVari
}
$feedSource->setTenant($user->getActiveTenant());

// Validate feed source
$this->validateFeedSource($object, $operation);

return $feedSource;
}

protected function updateFeedSourceProperties(FeedSource $feedSource, FeedSourceInput $object): void
/**
* @throws UnknownFeedTypeException
*/
protected function updateFeedSourceProperties(FeedSource $feedSource, object $object): void
{
if (!empty($object->title)) {
$feedSource->setTitle($object->title);
}
if (!empty($object->description)) {
$feedSource->setDescription($object->description);
}
if (!empty($object->createdBy)) {
$feedSource->setCreatedBy($object->createdBy);
}
if (!empty($object->modifiedBy)) {
$feedSource->setModifiedBy($object->modifiedBy);
}
$feedSource->setTitle($object->title);
$feedSource->setDescription($object->description);

if (!empty($object->secrets)) {
$feedSource->setSecrets($object->secrets);
}
if (!empty($object->feedType)) {
$feedSource->setFeedType($object->feedType);
}
$supportedFeedOutputType = $feedSource->getSupportedFeedOutputType();
if (null !== $supportedFeedOutputType) {
$feedSource->setSupportedFeedOutputType($supportedFeedOutputType);
}

$feedSource->setFeedType($object->feedType);
$feedType = $this->feedService->getFeedType($object->feedType);
$feedSource->setSupportedFeedOutputType($feedType->getSupportedFeedOutputType());
}

/**
* @throws \JsonException
* @throws UnknownFeedTypeException
*/
private function validateFeedSource(object $object, Operation $operation): void
{
$validator = $this->prepareValidator();

// Prepare base feed source validation schema
$feedSourceValidationSchema = (new FeedSource())->getSchema();
$feedSourceValidationSchema = FeedSource::getSchema();

// Validate base feed source
$this->executeValidation($object, $validator, $feedSourceValidationSchema);
Expand All @@ -135,18 +124,15 @@ private function validateFeedSource(object $object, Operation $operation): void
private function prepareValidator(): Validator
{
$schemaStorage = new SchemaStorage();
$feedSourceValidationSchema = (object) (new FeedSource())->getSchema();
$feedSourceValidationSchema = (object) FeedSource::getSchema();
$schemaStorage->addSchema('file://contentSchema', $feedSourceValidationSchema);

return new Validator(new Factory($schemaStorage));
}

/**
* @throws \JsonException
*/
private function executeValidation(mixed $object, Validator $validator, ?array $schema = null): void
{
$validator->validate($object, $schema ?? (new FeedSource())->getSchema());
$validator->validate($object, $schema ?? FeedSource::getSchema());
if (!$validator->isValid()) {
throw new InvalidArgumentException($this->getErrorMessage($validator));
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Api/FeedSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testCreateFeedSource(): void
'json' => [
'title' => 'Test feed source',
'description' => 'This is a test feed source',
'feedType' => 'App\Feed\EventDatabaseApiFeedType',
'feedType' => \App\Feed\EventDatabaseApiFeedType::class,
'secrets' => [
'host' => 'https://www.test.dk',
],
Expand All @@ -77,7 +77,7 @@ public function testCreateFeedSource(): void
'@type' => 'FeedSource',
'title' => 'Test feed source',
'description' => 'This is a test feed source',
'feedType' => 'App\Feed\EventDatabaseApiFeedType',
'feedType' => \App\Feed\EventDatabaseApiFeedType::class,
'secrets' => [
'host' => 'https://www.test.dk',
],
Expand Down Expand Up @@ -146,7 +146,7 @@ public function testCreateFeedSourceWithEventDatabaseFeedTypeWithoutRequiredSecr
'json' => [
'title' => 'Test feed source',
'outputType' => 'This is a test output type',
'feedType' => 'App\\Feed\\EventDatabaseApiFeedType',
'feedType' => \App\Feed\EventDatabaseApiFeedType::class,
'secrets' => [
'test secret',
],
Expand All @@ -171,7 +171,7 @@ public function testUpdateFeedSource(): void
'title' => 'Updated title',
'description' => 'Updated description',
'outputType' => 'This is a test output type',
'feedType' => 'App\Feed\EventDatabaseApiFeedType',
'feedType' => \App\Feed\EventDatabaseApiFeedType::class,
'secrets' => [
],
],
Expand All @@ -198,7 +198,7 @@ public function testDeleteFeedSource(): void
'title' => 'Test feed source',
'description' => 'This is a test feed source',
'outputType' => 'This is a test output type',
'feedType' => 'App\Feed\EventDatabaseApiFeedType',
'feedType' => \App\Feed\EventDatabaseApiFeedType::class,
'secrets' => [
'host' => 'https://www.test.dk',
],
Expand Down

0 comments on commit 7346b55

Please sign in to comment.