Skip to content

Commit 7346b55

Browse files
authored
Merge pull request #224 from os2display/feature/2829-add-feedsource-post-put-delete-functionality2
2829: Adjustment
2 parents 936bb02 + ab60308 commit 7346b55

File tree

7 files changed

+25
-81
lines changed

7 files changed

+25
-81
lines changed

public/api-spec-v2.json

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8144,12 +8144,6 @@
81448144
"feedType": {
81458145
"type": "string"
81468146
},
8147-
"secrets": {
8148-
"type": "array",
8149-
"items": {
8150-
"type": "string"
8151-
}
8152-
},
81538147
"feeds": {
81548148
"type": "array",
81558149
"items": {
@@ -8207,12 +8201,6 @@
82078201
"feedType": {
82088202
"type": "string"
82098203
},
8210-
"secrets": {
8211-
"type": "array",
8212-
"items": {
8213-
"type": "string"
8214-
}
8215-
},
82168204
"feeds": {
82178205
"type": "array",
82188206
"items": {
@@ -8305,12 +8293,6 @@
83058293
"feedType": {
83068294
"type": "string"
83078295
},
8308-
"secrets": {
8309-
"type": "array",
8310-
"items": {
8311-
"type": "string"
8312-
}
8313-
},
83148296
"feeds": {
83158297
"type": "array",
83168298
"items": {
@@ -8481,12 +8463,6 @@
84818463
"feedType": {
84828464
"type": "string"
84838465
},
8484-
"secrets": {
8485-
"type": "array",
8486-
"items": {
8487-
"type": "string"
8488-
}
8489-
},
84908466
"feeds": {
84918467
"type": "array",
84928468
"items": {

public/api-spec-v2.yaml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5766,10 +5766,6 @@ components:
57665766
type: string
57675767
feedType:
57685768
type: string
5769-
secrets:
5770-
type: array
5771-
items:
5772-
type: string
57735769
feeds:
57745770
type: array
57755771
items:
@@ -5810,10 +5806,6 @@ components:
58105806
type: string
58115807
feedType:
58125808
type: string
5813-
secrets:
5814-
type: array
5815-
items:
5816-
type: string
58175809
feeds:
58185810
type: array
58195811
items:
@@ -5877,10 +5869,6 @@ components:
58775869
type: string
58785870
feedType:
58795871
type: string
5880-
secrets:
5881-
type: array
5882-
items:
5883-
type: string
58845872
feeds:
58855873
type: array
58865874
items:
@@ -5997,10 +5985,6 @@ components:
59975985
type: string
59985986
feedType:
59995987
type: string
6000-
secrets:
6001-
type: array
6002-
items:
6003-
type: string
60045988
feeds:
60055989
type: array
60065990
items:

src/Dto/FeedSource.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class FeedSource
1818
public string $description = '';
1919
public string $outputType = '';
2020
public string $feedType = '';
21-
public array $secrets = [];
2221
public array $feeds = [];
2322
public array $admin = [];
2423
public string $supportedFeedOutputType = '';

src/Entity/Tenant/FeedSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function setSupportedFeedOutputType(string $supportedFeedOutputType): sel
111111
*
112112
* @return array The JSON schema definition
113113
*/
114-
public function getSchema(): array
114+
public static function getSchema(): array
115115
{
116116
return [
117117
'$schema' => 'https://json-schema.org/draft/2020-12/schema',

src/Feed/EventDatabaseApiFeedType.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,7 @@ public function getSchema(): array
316316
'properties' => [
317317
'host' => [
318318
'type' => 'string',
319-
'format' => 'url',
320-
'pattern' => 'https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-zA-Z0-9()]{2,6}\\b([-a-zA-Z0-9()@:%_\\+~#?&//=]*)',
319+
'format' => 'uri',
321320
],
322321
],
323322
'required' => ['host'],

src/State/FeedSourceProcessor.php

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use ApiPlatform\Metadata\Operation;
1010
use ApiPlatform\Metadata\Put;
1111
use ApiPlatform\State\ProcessorInterface;
12-
use App\Dto\FeedSourceInput;
1312
use App\Entity\Interfaces\TenantScopedUserInterface;
1413
use App\Entity\Tenant\FeedSource;
1514
use App\Exceptions\UnknownFeedTypeException;
@@ -50,7 +49,6 @@ public function process($data, Operation $operation, array $uriVariables = [], a
5049

5150
/**
5251
* @throws UnknownFeedTypeException
53-
* @throws \JsonException
5452
*/
5553
protected function fromInput(mixed $object, Operation $operation, array $uriVariables, array $context): FeedSource
5654
{
@@ -61,6 +59,10 @@ protected function fromInput(mixed $object, Operation $operation, array $uriVari
6159
throw new InvalidArgumentException('object must by of type FeedSource');
6260
}
6361

62+
// Validate feed source
63+
$this->validateFeedSource($object, $operation);
64+
65+
// Update properties.
6466
$this->updateFeedSourceProperties($feedSource, $object);
6567

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

73-
// Validate feed source
74-
$this->validateFeedSource($object, $operation);
75-
7675
return $feedSource;
7776
}
7877

79-
protected function updateFeedSourceProperties(FeedSource $feedSource, FeedSourceInput $object): void
78+
/**
79+
* @throws UnknownFeedTypeException
80+
*/
81+
protected function updateFeedSourceProperties(FeedSource $feedSource, object $object): void
8082
{
81-
if (!empty($object->title)) {
82-
$feedSource->setTitle($object->title);
83-
}
84-
if (!empty($object->description)) {
85-
$feedSource->setDescription($object->description);
86-
}
87-
if (!empty($object->createdBy)) {
88-
$feedSource->setCreatedBy($object->createdBy);
89-
}
90-
if (!empty($object->modifiedBy)) {
91-
$feedSource->setModifiedBy($object->modifiedBy);
92-
}
83+
$feedSource->setTitle($object->title);
84+
$feedSource->setDescription($object->description);
85+
9386
if (!empty($object->secrets)) {
9487
$feedSource->setSecrets($object->secrets);
9588
}
96-
if (!empty($object->feedType)) {
97-
$feedSource->setFeedType($object->feedType);
98-
}
99-
$supportedFeedOutputType = $feedSource->getSupportedFeedOutputType();
100-
if (null !== $supportedFeedOutputType) {
101-
$feedSource->setSupportedFeedOutputType($supportedFeedOutputType);
102-
}
89+
90+
$feedSource->setFeedType($object->feedType);
91+
$feedType = $this->feedService->getFeedType($object->feedType);
92+
$feedSource->setSupportedFeedOutputType($feedType->getSupportedFeedOutputType());
10393
}
10494

10595
/**
106-
* @throws \JsonException
10796
* @throws UnknownFeedTypeException
10897
*/
10998
private function validateFeedSource(object $object, Operation $operation): void
11099
{
111100
$validator = $this->prepareValidator();
112101

113102
// Prepare base feed source validation schema
114-
$feedSourceValidationSchema = (new FeedSource())->getSchema();
103+
$feedSourceValidationSchema = FeedSource::getSchema();
115104

116105
// Validate base feed source
117106
$this->executeValidation($object, $validator, $feedSourceValidationSchema);
@@ -135,18 +124,15 @@ private function validateFeedSource(object $object, Operation $operation): void
135124
private function prepareValidator(): Validator
136125
{
137126
$schemaStorage = new SchemaStorage();
138-
$feedSourceValidationSchema = (object) (new FeedSource())->getSchema();
127+
$feedSourceValidationSchema = (object) FeedSource::getSchema();
139128
$schemaStorage->addSchema('file://contentSchema', $feedSourceValidationSchema);
140129

141130
return new Validator(new Factory($schemaStorage));
142131
}
143132

144-
/**
145-
* @throws \JsonException
146-
*/
147133
private function executeValidation(mixed $object, Validator $validator, ?array $schema = null): void
148134
{
149-
$validator->validate($object, $schema ?? (new FeedSource())->getSchema());
135+
$validator->validate($object, $schema ?? FeedSource::getSchema());
150136
if (!$validator->isValid()) {
151137
throw new InvalidArgumentException($this->getErrorMessage($validator));
152138
}

tests/Api/FeedSourceTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testCreateFeedSource(): void
6060
'json' => [
6161
'title' => 'Test feed source',
6262
'description' => 'This is a test feed source',
63-
'feedType' => 'App\Feed\EventDatabaseApiFeedType',
63+
'feedType' => \App\Feed\EventDatabaseApiFeedType::class,
6464
'secrets' => [
6565
'host' => 'https://www.test.dk',
6666
],
@@ -77,7 +77,7 @@ public function testCreateFeedSource(): void
7777
'@type' => 'FeedSource',
7878
'title' => 'Test feed source',
7979
'description' => 'This is a test feed source',
80-
'feedType' => 'App\Feed\EventDatabaseApiFeedType',
80+
'feedType' => \App\Feed\EventDatabaseApiFeedType::class,
8181
'secrets' => [
8282
'host' => 'https://www.test.dk',
8383
],
@@ -146,7 +146,7 @@ public function testCreateFeedSourceWithEventDatabaseFeedTypeWithoutRequiredSecr
146146
'json' => [
147147
'title' => 'Test feed source',
148148
'outputType' => 'This is a test output type',
149-
'feedType' => 'App\\Feed\\EventDatabaseApiFeedType',
149+
'feedType' => \App\Feed\EventDatabaseApiFeedType::class,
150150
'secrets' => [
151151
'test secret',
152152
],
@@ -171,7 +171,7 @@ public function testUpdateFeedSource(): void
171171
'title' => 'Updated title',
172172
'description' => 'Updated description',
173173
'outputType' => 'This is a test output type',
174-
'feedType' => 'App\Feed\EventDatabaseApiFeedType',
174+
'feedType' => \App\Feed\EventDatabaseApiFeedType::class,
175175
'secrets' => [
176176
],
177177
],
@@ -198,7 +198,7 @@ public function testDeleteFeedSource(): void
198198
'title' => 'Test feed source',
199199
'description' => 'This is a test feed source',
200200
'outputType' => 'This is a test output type',
201-
'feedType' => 'App\Feed\EventDatabaseApiFeedType',
201+
'feedType' => \App\Feed\EventDatabaseApiFeedType::class,
202202
'secrets' => [
203203
'host' => 'https://www.test.dk',
204204
],

0 commit comments

Comments
 (0)