99use ApiPlatform \Metadata \Operation ;
1010use ApiPlatform \Metadata \Put ;
1111use ApiPlatform \State \ProcessorInterface ;
12- use App \Dto \FeedSourceInput ;
1312use App \Entity \Interfaces \TenantScopedUserInterface ;
1413use App \Entity \Tenant \FeedSource ;
1514use 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 }
0 commit comments